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;
}
相关推荐
维核科技6 分钟前
本地优先的 AI:不联网也能跑的模型和智能体
人工智能
七夜zippoe7 分钟前
Function Calling 深度解析:从参数定义到错误处理的完整实践
人工智能·ai·agent·function·calling
m0_734571766 小时前
深入理解人工智能 chatGPT的软件架构
人工智能
飞塔老梅子7 小时前
09. Codex 节省Token ❀ 老梅子学AI
人工智能·ai·token·模型·codex
HyperAI超神经7 小时前
扰动实验+迁移学习,MIT团队推出IRIS,用「指纹」重建单细胞信号传导历史
人工智能·深度学习·机器学习·迁移学习
前沿在线7 小时前
启元机器人亮相外滩大会,探索个人机器人健康服务新场景
人工智能·ai·大模型
haishikeji696_7 小时前
现成无人机管理系统源码|支持私有化部署、二次开发、政企投标、自动机库对接
人工智能·无人机·低空经济·无人机管理系统·飞控管理系统
汇智信科7 小时前
煤矿视频智能分析系统:基于 AI 视觉实现井下安全隐患实时监测与闭环管控
人工智能
h78813947 小时前
录音容易纪要难?录音转文字 + AI 纪要实测,提升会议效率
人工智能·powerpoint