opencv——inRange

#include 
#include 

using namespace cv;

void callback1(int pos, void*)
{
	std::cout << "hmin" << pos << std::endl;
}
void callback2(int pos, void*)
{
	std::cout << "smin" << pos << std::endl;
}
void callback3(int pos, void*)
{
	std::cout << "vmin" << pos << std::endl;
}
void callback4(int pos, void*)
{
	std::cout << "hmax" << pos << std::endl;
}
void callback5(int pos, void*)
{
	std::cout << "smax" << pos << std::endl;
}
void callback6(int pos, void*)
{
	std::cout << "vmax" << pos << std::endl;
}
int main()
{
	Mat src, hsv, dst;
	int hmin = 0;
	int smin = 0;
	int vmin = 0;
	int hmax = 255;
	int smax = 255;
	int vmax = 255;

	src = imread("../data/baboon.jpg");
	imshow("src", src);
	cvtColor(src, hsv, CV_RGB2HSV);
	createTrackbar("hmin", "src", &hmin, 256, callback1);
	createTrackbar("smin", "src", &smin, 256, callback2);
	createTrackbar("vmin", "src", &vmin, 256, callback3);
	createTrackbar("hmax", "src", &hmax, 256, callback4);
	createTrackbar("smax", "src", &smax, 256, callback5);
	createTrackbar("vmax", "src", &vmax, 256, callback6);
	while (1)//循环包裹,刷新图像
	{
		inRange(hsv, Scalar(hmin, smin, vmin), Scalar(hmax, smax, vmax), dst);
		imshow("dst", dst);

		int c = waitKey(10);
		if (c == 'q')break;
	}
	
}

以上是代码,主要练习inRange函数

createTrackbar可以while循环刷新图像,替代回调函数。

你可能感兴趣的:(opencv)