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;
}
相关推荐
csdn_aspnet5 小时前
如何用 C# 和 Gemma 3 在本地构建一个真正能完成工作的 AI 代理的
人工智能·ai·c#·gemma
啊哈哈哈哈哈啊哈哈5 小时前
边缘计算与轮廓检测
人工智能·opencv·计算机视觉
cskywit5 小时前
从DFL到无NMS推理:一文拆解YOLO26背后的工程取舍与数学原理
人工智能·机器学习
PPHT-H5 小时前
【人工智能笔记】第四十四节:OpenClaw封神工具 openclaw-free-openai-proxy 免费AI模型批量调用,零token费+稳到不翻车!
人工智能·深度学习·openclaw·免费openai·ai服务代理
yiyu07165 小时前
3分钟搞懂深度学习AI:实操篇:RNN
人工智能·深度学习
uzong5 小时前
CoPaw是什么?-- 2026年开源的国产个人AI助手
人工智能·后端
海盗儿6 小时前
TensorRT-LLM 框架与源码分析
人工智能
无心水6 小时前
【任务调度:框架】11、分布式任务调度进阶:高可用、幂等性、性能优化三板斧
人工智能·分布式·后端·性能优化·架构·2025博客之星·分布式调度框架
码森林6 小时前
小龙虾居然比你更健忘?OpenClaw 记忆系统指南,让它永远记住你
人工智能·ai编程·全栈
ghie90906 小时前
维纳滤波器语音增强MATLAB实现
人工智能·matlab·语音识别