OpenCV 阈值法

1.概述

在深度学习出现之前,图像中的阈值法处理主要有二值阈值法、自适应阈值法、Ostu阈值法。

2.理论对比

3.代码实现

cpp 复制代码
#include <iostream>
#include <opencv2/opencv.hpp>

int main(int argc, char** argv) {
    if(argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <image_path>" << std::endl;
        return -1;
    }

    // Load the image
    cv::Mat image = cv::imread(argv[1], cv::IMREAD_GRAYSCALE);
    if(image.empty()) {
        std::cerr << "Error: Couldn't read the image. Check the path and try again." << std::endl;
        return -1;
    }
    cv::imshow("Original Image", image);

    // Binary Thresholding
    cv::Mat binaryThresholded;
    cv::threshold(image, binaryThresholded, 127, 255, cv::THRESH_BINARY);
    cv::imshow("Binary Thresholding", binaryThresholded);

    // Adaptive Thresholding
    cv::Mat adaptiveThresholded;
    cv::adaptiveThreshold(image, adaptiveThresholded, 255, cv::ADAPTIVE_THRESH_MEAN_C, cv::THRESH_BINARY, 11, 2);
    cv::imshow("Adaptive Thresholding", adaptiveThresholded);

    // Otsu's Thresholding
    cv::Mat otsuThresholded;
    cv::threshold(image, otsuThresholded, 0, 255, cv::THRESH_BINARY | cv::THRESH_OTSU);
    cv::imshow("Otsu's Thresholding", otsuThresholded);

    // Wait for a key press and then close
    cv::waitKey(0);
    
    return 0;
}
相关推荐
miofly2 分钟前
DeepSeek 开源 Super-agent 架构:AI 主动发现 Bug 并提交 PR
人工智能
kyrie_sakura7 分钟前
大模型学习20 大模型学习 - LangChain3 - Agents
人工智能·学习·langchain
zhangfeng113311 分钟前
2026 书生国智科探挑战赛暨飞翔杯 AI Agent/Skills 开发大赛全景介绍
人工智能
DevMemo27 分钟前
2026 AI外呼系统技术选型:全链路延迟预算与集成成本实测,10个品牌的架构真相
人工智能·架构
ZhuoAIStage30 分钟前
契约与闸门打架该改谁?用 AI 智能体 preflight 把配置漂移挡在发版前——以舞台工艺审图专家包闸门冲突为例
人工智能·闸门·舞台工艺·配置漂移·专家包·preflight
Flynn Liang34 分钟前
大模型怎么自己调函数?Spring AI 2.0 Tool Calling 自动循环原理 + 完整时序图
java·人工智能·spring·agent·函数调用·springai·toolcalling
深频率36 分钟前
苹果AI入华:11月4日开测,千问只是个选项
人工智能
一枕槐安48741 分钟前
大模型 API 调用封装:统一适配 GPT、文心一言、通义千问
人工智能·gpt·文心一言·大模型api·流式输出·统一封装·多模态路由
neocheng_5221 小时前
校招时间有限,先补英语、计算机还是AI应用能力?从目标岗位判断优先级
人工智能