OpenCV入门——阈值的检测与确定

直接上代码

当将THRESH_BINARY修改为THRESH_OTSU时,算法会自动确定阈值的大小,TrackBar失去作用... :)

#include
#include

using namespace cv;
using namespace std;

int threshold_value = 120;
int threshold_max = 255;
int type_value = 2;
int type_max = 4;
const char *output_title = "binary_image";
Mat img, dst, gray_img;

void Threshold(int, void*)
{
	cvtColor(img, gray_img, COLOR_BGR2GRAY);
	threshold(gray_img, dst, threshold_value, threshold_max, THRESH_BINARY);//根据直方图来计算阈值
	imshow(output_title, dst);
}
int main()
{
	char INPUT[] = "input";
	char OUTPUT[] = "output";
	char UP_IMG[] = "up image";

	img = imread("D:\\OpenCV\\images\\14.jpg");
	if (img.empty())
	{
		cout << "image loading failed..." << endl;
		return -1;
	}
	namedWindow(INPUT, WINDOW_AUTOSIZE);
	namedWindow(output_title, WINDOW_AUTOSIZE);
	imshow(INPUT, img);

	createTrackbar("Threshold Value:", output_title, &threshold_value, threshold_max, Threshold);
	Threshold(0, 0);

	waitKey(0);
	return 0;
}

 效果图

OpenCV入门——阈值的检测与确定_第1张图片

 

你可能感兴趣的:(OpenCV,opencv,阈值检测,阈值自动检测,trackBar阈值检测)