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;
}
相关推荐
言之。几秒前
微软AutoGen:多智能体AI开发新利器
人工智能
开发者导航9 分钟前
【开发者导航】支持多存储方式的开源文件列表程序:OpenList
人工智能·学习·阿里云·信息可视化
GISer_Jing13 分钟前
前端学习总结——AI&主流前沿方向篇
前端·人工智能·学习
用户51914958484523 分钟前
cURL Kerberos FTP整数溢出漏洞分析与修复
人工智能·aigc
文火冰糖的硅基工坊23 分钟前
[嵌入式系统-108]:定昌电子DC-A588电路板介绍,一款基于瑞芯微RK3588芯片的高性能嵌入式AI边缘计算工控主机
人工智能·物联网·边缘计算
视***间32 分钟前
边缘计算重塑监控系统:从 “被动录像” 到 “主动智能” 的变革---视程空间
大数据·人工智能·边缘计算·ai算力·视程空间
song1502653729834 分钟前
视觉检测设备-AI视觉质量检测方案提升效率
人工智能·计算机视觉·视觉检测
励志成为美貌才华为一体的女子1 小时前
每日学习内容简单汇总记录
人工智能
编程小白_正在努力中1 小时前
大语言模型后训练:解锁潜能的关键路径
人工智能·大语言模型
37手游后端团队1 小时前
揭秘ChatGPT“打字机”效果:深入理解SSE流式传输技术
人工智能·后端