三 (3.2 imgproc) 图像阈值操作

2018-11-24  本文已影响0人  交大小丑

阈值操作原理:

什么是阈值?

阈值操作类型:

OpenCV中提供了阈值(threshold)函数: threshold这个函数有5种阈值化类型,在接下来的章节中将会具体介绍。

阈值类型1: 二进制阈值化
阈值类型2: 反二进制阈值化
阈值类型3: 截断阈值化
阈值类型4: 阈值化为0
阈值类型5: 反阈值化为0

代码实例:

#include <opencv2/opencv.hpp>
#include <iostream>
#include <math.h>

using namespace cv;
Mat src, gray_src, dst;
int threshold_value = 127;
int threshold_max = 255;
int type_value = 2;
int type_max = 4;
const char* output_title = "binary image";
void Threshold_Demo(int, void*);
int main(int argc, char** argv) {
    src = imread("D:/vcprojects/images/test.png");
    if (!src.data) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input image", CV_WINDOW_AUTOSIZE);
    namedWindow(output_title, CV_WINDOW_AUTOSIZE);
    imshow("input image", src);
    
    createTrackbar("Threshold Value:", output_title, &threshold_value, threshold_max, Threshold_Demo);
    createTrackbar("Type Value:", output_title, &type_value, type_max, Threshold_Demo);
    Threshold_Demo(0, 0);

    waitKey(0);
    return 0;
}

void Threshold_Demo(int, void*) {
    cvtColor(src, gray_src, CV_BGR2GRAY);
    threshold(src, dst, 0, 255, THRESH_TRIANGLE | type_value);
    imshow(output_title, dst);
}
上一篇下一篇

猜你喜欢

热点阅读