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;
}
相关推荐
裤裤兔9 分钟前
医学影像深度学习知识点总结
人工智能·深度学习·机器学习·医学影像·医学图像
free-elcmacom11 分钟前
机器学习进阶<8>PCA主成分分析
人工智能·python·机器学习·pca
亚马逊云开发者40 分钟前
Q CLI助力合合信息实现Aurora的升级运营
人工智能
涛涛北京1 小时前
【强化学习实验】- 策略梯度算法
人工智能·算法
Fairy要carry1 小时前
2025/12/15英语打卡
人工智能
weixin_446260851 小时前
《从零开始构建智能体》—— 实践与理论结合的智能体入门指南
人工智能
新加坡内哥谈技术1 小时前
Claude 代理技能:从第一性原理出发的深度解析
人工智能
长空任鸟飞_阿康1 小时前
FastAPI 入门指南
人工智能
Pyeako1 小时前
机器学习之KNN算法
人工智能·算法·机器学习
Mxsoft6191 小时前
我发现知识图谱节点关系缺失致诊断不准,自动关系抽取补全救场
人工智能