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;
}
相关推荐
5pace10 分钟前
机器学习(西瓜书)第 14 章 概率图模型
人工智能·机器学习
Linux猿13 分钟前
828华为云征文 | 云服务器Flexus X实例:部署 AgentOps,全方位监测智能体
人工智能·华为云·agent·autogen·flexus云服务器x实例·华为云服务器·agentops
Rense130 分钟前
开源RK3588 AI Module7,并与Jetson Nano生态兼容的低功耗AI模块
人工智能·开源
FL16238631291 小时前
[数据集][目标检测]不同颜色的安全帽检测数据集VOC+YOLO格式7574张5类别
人工智能·yolo·目标检测
QuantumYou1 小时前
计算机视觉 对比学习 串烧二
人工智能·学习·计算机视觉
阿利同学1 小时前
热成像目标检测数据集
人工智能·目标检测·机器学习·目标跟踪·数据集·热成像目标检测数据集·机器学习 深度学习
拥抱AGI1 小时前
我说大模型微调没啥技术含量,谁赞成谁反对?
人工智能·学习·语言模型·大模型学习·大模型入门·大模型教程
aqymnkstkw1 小时前
2024年【电气试验】考试题库及电气试验模拟试题
大数据·c语言·人工智能·嵌入式硬件·安全
2401_8504404971 小时前
激发AI创造力:掌握Prompt提示词的高效提问方法
人工智能·prompt
Terry Cao 漕河泾2 小时前
SRT3D: A Sparse Region-Based 3D Object Tracking Approach for the Real World
人工智能·计算机视觉·3d·目标跟踪