zoukankan      html  css  js  c++  java
  • OpenCV-C++ 图像基本阈值操作

    阈值类型

    阈值产生的算法,阈值类型

    • THRESH_BINARY表示大于thresh的取maxval,否则取0;
    • THRESH_BINARY_INV表示大于thresh的取0,否则取maxvalue;
    • THRESH_TRUNC表示大于threshthreshold,否则不改变灰度值;
    • THRESH_TOZERO表示大于thresh的不改变灰度值,否则取0;
    • THRESH_TOZERO_INV表示大于thresh取0,窦泽不改变灰度值;
    • THRESH_OTSU表示使用otsu自动计算阈值;
    • THRESH_TRIANGLE表示使用Triangle自动计算阈值;

    代码示例如下:

    #include <iostream>
    #include <opencv2/opencv.hpp>
    
    using namespace std;
    using namespace cv;
    
    
    char output_title[] = "output window";
    int threshold_value = 127;
    const int MAX_THRESHOLD = 255;
    
    int threshold_type = 2;
    const int MAX_THRESHOLD_TYPE = 4;
    
    void Threshold_Demo(int, void*);
    
    Mat src, srcGray, dst;
    
    int main(){
    
        // 读取图像
        src = imread("/home/chen/dataset/lena.jpg");
        if (src.empty()){
            cout << "could not load image." << endl;
            return -1;
        }
        namedWindow("src", WINDOW_AUTOSIZE);
        imshow("src", src);
    
        // 创建
        namedWindow(output_title, WINDOW_AUTOSIZE);
        createTrackbar("Threshold: ", output_title, &threshold_value, MAX_THRESHOLD, Threshold_Demo);
        createTrackbar("Type: ", output_title, &threshold_type, MAX_THRESHOLD_TYPE, Threshold_Demo);
        Threshold_Demo(0, 0);
    
        waitKey(0);
    
        return 0;
    }
    
    
    void Threshold_Demo(int, void*){
        cvtColor(src, srcGray, COLOR_BGR2GRAY);
        // printf("%d", THRESH_BINARY);  // 0
        // printf("%d", THRESH_BINARY_INV); // 1
        // printf("%d", THRESH_TRUNC);  // 2
        // printf("%d", THRESH_TOZERO); // 3
        // printf("%d", THRESH_TOZERO_INV);  // 4
        threshold(srcGray, dst, threshold_value, MAX_THRESHOLD, threshold_type);
        // threshold(srcGray, dst, 0, 255, THRESH_OTSU | threshold_type);  // 自动计算阈值
        // threshold(srcGray, dst, 0, 255, THRESH_TRIANGLE | threshold_type);  // 自动计算阈值
    
        imshow(output_title, dst);
    }
    
  • 相关阅读:
    汽车金融评分卡
    Lending Club 数据做数据分析&评分卡
    时间序列分析和预测 (转载)
    距离计算公式总结(转载)
    机器学习常用算法与辅助函数公式
    金融领域常用的数据分析方法
    常用模型评估方法总结
    A--集成算法的实现
    A--随机森林(RF)的 sciklit-learn 实现
    A--Scikit-Learn 实现决策树
  • 原文地址:https://www.cnblogs.com/chenzhen0530/p/14645673.html
Copyright © 2011-2022 走看看