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;
}
相关推荐
云卓SKYDROID3 分钟前
无人机载重模块技术要点分析
人工智能·无人机·科普·高科技·云卓科技
云卓SKYDROID5 分钟前
无人机RTK技术要点与难点分析
人工智能·无人机·科普·高科技·云卓科技
麻雀无能为力1 小时前
CAU数据挖掘 支持向量机
人工智能·支持向量机·数据挖掘·中国农业大学计算机
智能汽车人1 小时前
Robot---能打羽毛球的机器人
人工智能·机器人·强化学习
埃菲尔铁塔_CV算法1 小时前
基于 TOF 图像高频信息恢复 RGB 图像的原理、应用与实现
人工智能·深度学习·数码相机·算法·目标检测·计算机视觉
ζั͡山 ั͡有扶苏 ั͡✾1 小时前
AI辅助编程工具对比分析:Cursor、Copilot及其他主流选择
人工智能·copilot·cursor
东临碣石821 小时前
【AI论文】数学推理能否提升大型语言模型(LLM)的通用能力?——探究大型语言模型推理能力的可迁移性
人工智能·语言模型·自然语言处理
未来智慧谷2 小时前
微软医疗AI诊断系统发布 多智能体协作实现疑难病例分析
人工智能·microsoft·医疗ai
野生技术架构师2 小时前
简述MCP的原理-AI时代的USB接口
人工智能·microsoft
Allen_LVyingbo2 小时前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗