ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

OpenCV-C++ 图像基本阈值操作

2021-04-11 22:05:27  阅读:250  来源: 互联网

标签:阈值 int C++ OpenCV THRESH threshold thresh 灰度


阈值类型

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

  • 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);
}

标签:阈值,int,C++,OpenCV,THRESH,threshold,thresh,灰度
来源: https://www.cnblogs.com/chenzhen0530/p/14645673.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有