opencv3 计算反向投影-鼠标事件

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

using namespace cv;
using namespace std;

vector<Mat> modeHImage;
Mat findImage;
int binValue = 254;

Mat modeImage;

bool g_bDrawingBox = false;
Rect g_rectangle;
RNG &g_rng = theRNG();
Mat modeROIImage;
Point startPoint;
Mat tempImage;

void onMouse(int event, int x, int y, int flag, void *param)
{
	Mat &img = *(cv::Mat*)param;

	switch (event)
	{
		//移动鼠标的时候
	case CV_EVENT_MOUSEMOVE:
	{
							   if (g_bDrawingBox)
							   {
								   g_rectangle.width = x - g_rectangle.x;
								   g_rectangle.height = y - g_rectangle.y;

								   tempImage.copyTo(modeImage);
								   rectangle(modeImage, startPoint
									   , Point(startPoint.x + g_rectangle.width, startPoint.y + g_rectangle.height)
									   , Scalar(g_rng.uniform(0, 255), g_rng.uniform(0, 255), g_rng.uniform(0, 255)));
								   imshow("【模版图像】", modeImage);
							   }
	}
		break;
		//点击鼠标左键时
	case CV_EVENT_LBUTTONDOWN:
	{
								 g_bDrawingBox = true;
								 g_rectangle = Rect(x, y, 0, 0);
								 startPoint = Point(x, y);
	}
		break;
		//松开鼠标左键时
	case CV_EVENT_LBUTTONUP:
	{
							   g_bDrawingBox = false;

							   modeROIImage = modeImage(Rect(startPoint.x, startPoint.y, x - startPoint.x, y - startPoint.y));
							   imshow("【感兴趣区域】", modeROIImage);
							   tempImage.copyTo(modeImage);
							   //计算模版图像的直方图
							   首先得得到HSV空间的H通道,所以先进行图像空间的转换
							   //Mat modeHsvImage;
							   //cvtColor(modeROIImage, modeHsvImage, CV_BGR2HSV);
							   //split(modeHsvImage, modeHImage);

							   //计算  得到的H通道的直方图
							   MatND modeHist;
							   int channels[] = { 0, 1, 2 };
							   int histSize[] = { 256, 256, 256 };
							   float BRanges[] = { 0, 255 };
							   float GRanges[] = { 0, 255 };
							   float RRanges[] = { 0, 255 };
							   const float *ranges[] = { BRanges, GRanges, RRanges };
							   calcHist(&modeROIImage, 1, channels, Mat(), modeHist, 3, histSize, ranges, true, false);
							   /*normalize(modeHist, modeHist, 1.0);*/

							   //利用得到的 模版图像的直方图 计算需要查找的图像的反向投影
							   Mat backImage;
							   backImage.create(findImage.size(), CV_8UC1);
							   backImage = Scalar::all(0);
							   calcBackProject(&findImage, 1, channels, modeHist, backImage, ranges, 255.0);

							   imshow("【反向投影计算得到的图像】", backImage);
	}
		break;
	}
}

int main()
{
	modeImage = imread("green2.jpg");
	modeImage.copyTo(tempImage);
	findImage = imread("green1.jpg");
	namedWindow("【模版图像】", 0);
	imshow("【模版图像】", modeImage);
	namedWindow("【查询图像】", 0);
	imshow("【查询图像】", findImage);

	对需要查询的图形进行颜色减缩操作
	//Mat_<Vec3b>::iterator it = findImage.begin<Vec3b>();
	//Mat_<Vec3b>::iterator itend = findImage.end<Vec3b>();
	//while (it != itend)
	//{
	//	(*it)[0] = (*it)[0] / 64 * 64 + 64 / 2;
	//	(*it)[1] = (*it)[1] / 64 * 64 + 64 / 2;
	//	(*it)[2] = (*it)[2] / 64 * 64 + 64 / 2;

	//	++it;
	//}

	setMouseCallback("【模版图像】", onMouse, (void *)&modeImage);

	modeROIImage.create(modeImage.size(), modeImage.type());
	modeImage.copyTo(modeROIImage);
	char key;
	while (1)
	{
		key = waitKey(1);
		if (key == 27)
			break;
	}

	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值