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;
}
相关推荐
落痕的寒假2 小时前
[论文总结] 深度学习在农业领域应用论文笔记14
论文阅读·人工智能·深度学习
神探阿航2 小时前
图像噪声处理技术:让图像更清晰的艺术
图像处理·人工智能·计算机视觉
yuyuyue2493 小时前
lstm预测
人工智能·机器学习
纠结哥_Shrek4 小时前
自然语言处理-词嵌入 (Word Embeddings)
人工智能·自然语言处理
Zfox_4 小时前
DeepSeek R1本地化部署 Ollama + Chatbox 打造最强 AI 工具
人工智能·ai·大模型教程·deepseek
CodeLinghu4 小时前
Agentic Automation:基于Agent的企业认知架构重构与数字化转型跃迁---我的AI经典战例
人工智能·重构·架构
银行数字化转型导师坚鹏4 小时前
数字化转型导师坚鹏:AI大模型DEEPSEEK重构人工智能格局的里程碑
人工智能·ai·重构·deepseek
X.AI6665 小时前
【大模型LLM面试合集】大语言模型架构_MHA_MQA_GQA
人工智能·语言模型·自然语言处理
智识世界Intelligence5 小时前
DeepSeek的崛起与全球科技市场的震荡
人工智能
弥树子5 小时前
使用 PyTorch 实现逻辑回归并评估模型性能
人工智能·pytorch·逻辑回归