【opencv学习】bar进度条改变颜色

#include 
#include 
using namespace cv;
using namespace std;

#define WINNNAME "窗口"
int cb = 0, cg = 0, cr = 0;

void changeColor(int pos, void* usrdata)//回调函数
{
	Mat img = *(Mat*)usrdata;//获取修改后的图像并解引用
	cb = getTrackbarPos("Blue", WINNNAME);//获取拖拉后的值
	cg = getTrackbarPos("Green", WINNNAME);
	cr = getTrackbarPos("Red", WINNNAME);
	for (int i = 0; i < img.rows; i++)
	{
		for (int j = 0; j < img.cols; j++)
		{
			img.at<Vec3b>(i, j)[0] = cb;
			img.at<Vec3b>(i, j)[1] = cg;
			img.at<Vec3b>(i, j)[2] = cr;
		}
	}
	imshow(WINNNAME, img);
	waitKey(0);
}

int main()
{
	Mat img(512, 512, CV_8UC3, Scalar(0, 255, 0));
	imshow(WINNNAME, img);
	createTrackbar("Blue", WINNNAME, 0, 255, changeColor, &img);//函数的作用是现实bar,当拖动进度条时,回调函数改变传给新的cb,cb付给img,img打印
	createTrackbar("Green", WINNNAME, 0, 255, changeColor, &img);
	createTrackbar("Red", WINNNAME, 0, 255, changeColor, &img);
	waitKey(0);
	destroyAllWindows();
}

Mat img = (Mat)usrdata;
该行是将传递给回调函数的usrdata参数转换为Mat类型的指针,并通过解引用操作符*将其赋值给img变量。这样做的目的是获取到传递给回调函数的图像数据,以便对图像进行操作。

你可能感兴趣的:(opencv,学习,人工智能)