1. C/C++

OpenCV-第十二话-图像阈值

一.写在前面

阈值是什么?

简单点说是把图像分割的标尺,这个标尺是根据什么产生的。

阈值产生算法?阈值类型。(Binary segmentation)

二.分类

阈值类型一阈值二值化(threshold binary)

 

阈值类型一阈值反二值化(threshold binary Inverted)

 

阈值类型一截断 (truncate)

阈值类型一阈值取零 (threshold to zero)

阈值类型一阈值反取零 (threshold to zero inverted)

 

三.对应API常量

四.代码实现

/*
OpenCV 基本阈值操作学习
Michael Jiang<sencom1997@outlook.com>
2019年7月25日13:13:46
*/

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

using namespace cv;
Mat src, dst, gray;

int threshold_value = 127;
int threshold_max = 255;

void threshold_demo(int, void*)
{
	//色彩转换
	cvtColor(src, gray, COLOR_BGR2GRAY);
	threshold(gray, dst, threshold_value, threshold_max, THRESH_BINARY);
	imshow("dst", dst);
}

int main()
{
	//读取图像
	src = imread("D:/linus.jpg", IMREAD_COLOR);

	namedWindow("src", WINDOW_AUTOSIZE);
	namedWindow("dst", WINDOW_AUTOSIZE);

	//判断读取是否成功
	if (src.empty()) {
		printf("pic load failed!\n");
		return -1;
	}

	//创建滑动条
	createTrackbar("Threshold Value:", "dst", &threshold_value, threshold_max, threshold_demo);

	//显示窗口
	imshow("src", src);
	
	waitKey(0);
	return 0;
}