opencv 取点交互 函数

//取点交互相关begin   
#define  PtNUM 50
cv::Point BorderPtArr[1][PtNUM];
int ptCount = 0;

//mouse event callback
void mouseEvent(int event, int x, int y, int flags, void *param )
{
	if (event==CV_EVENT_LBUTTONDOWN) 
	{
		BorderPtArr[0][ptCount%PtNUM] = cv::Point(x,y);
		ptCount++;
		//cout<<" Pt Num = "<<ptCount<<" "<< cv::Point(x,y) <<endl;
	}
}

void GetImgBorderPts( Mat pRGBFrame)
{
	//
	Mat ImgTemp;
	namedWindow("borderImg");

	imshow("borderImg", pRGBFrame);

	HWND wnd = (HWND )cvGetWindowHandle("borderImg");
	SetForegroundWindow(wnd);

	Mat maskImg = Mat::zeros(pRGBFrame.size(), CV_8UC3);

	bool isGetMask = false;
	ptCount = 0;
	while (1)
	{	
		pRGBFrame.copyTo(ImgTemp);
		setMouseCallback("borderImg",mouseEvent);
		if (ptCount >0)
		{
			circle(ImgTemp,BorderPtArr[0][0],2,CV_RGB(225,225,0),8);
			for (int i=1;i<ptCount;i++)
			{
				if (i==ptCount-1)
					circle(ImgTemp,BorderPtArr[0][i],2,CV_RGB(255,0,0),8);
				else
					circle(ImgTemp,BorderPtArr[0][i],2,CV_RGB(255,0,0),8);
				line(ImgTemp,BorderPtArr[0][i-1],BorderPtArr[0][i],CV_RGB(255,255,0),2);
			}
			line(ImgTemp,BorderPtArr[0][ptCount-1],BorderPtArr[0][0],CV_RGB(255,255,0),1,8);
		}

		if (isGetMask)
		{
			bitwise_and(ImgTemp,maskImg,ImgTemp);

			char strTemp[128];
			sprintf_s(strTemp," Preview Image ROI");
			putText(ImgTemp,(string)strTemp,
				cvPoint(20,pRGBFrame.rows/2-20),
				FONT_HERSHEY_SIMPLEX,
				0.5,
				CV_RGB(0,255,0),1);

			sprintf_s(strTemp,"Press Key 'S'  to Start to Tracking....");
			putText(ImgTemp,(string)strTemp,
				cvPoint(20,pRGBFrame.rows/2),
				FONT_HERSHEY_SIMPLEX,
				0.5,
				CV_RGB(0,255,0),1);
		}
		else
		{
			char strTemp[128];
			sprintf_s(strTemp,"Set Image Border Points of ROI");
			putText(ImgTemp,(string)strTemp,
				cvPoint(20,pRGBFrame.rows/2-20),
				FONT_HERSHEY_SIMPLEX,
				0.5,
				CV_RGB(0,255,0),1);

			sprintf_s(strTemp,"Press Key 'F'  to Finish Seting....");
			putText(ImgTemp,(string)strTemp,
				cvPoint(20,pRGBFrame.rows/2),
				FONT_HERSHEY_SIMPLEX,
				0.5,
				CV_RGB(0,255,0),1);

			sprintf_s(strTemp,"Press Key 'R'  to ReSet....");
			putText(ImgTemp,(string)strTemp,
				cvPoint(20,pRGBFrame.rows/2+20),
				FONT_HERSHEY_SIMPLEX,
				0.5,
				CV_RGB(0,255,0),1);

			sprintf_s(strTemp,"Press Key 'G'  to Finish Seting....For NO Border Video.");
			putText(ImgTemp,(string)strTemp,
				cvPoint(20,pRGBFrame.rows/2+40),
				FONT_HERSHEY_SIMPLEX,
				0.5,
				CV_RGB(0,255,0),1);
		}

		imshow("borderImg", ImgTemp);

		int key=waitKey(10);

		if (key=='s' || key=='S')
		{//esc   

			destroyWindow("borderImg");
			break;   
		}

		if (key=='f' || key=='F')
		{  
			if (ptCount <4)
			{
				int ww = pRGBFrame.cols;
				int hh = pRGBFrame.rows;
				int dd = 8;
				int ydd = 50;
				BorderPtArr[0][0] = cv::Point(dd,dd+ydd);
				BorderPtArr[0][1] = cv::Point(ww-dd,dd+ydd);
				BorderPtArr[0][2] = cv::Point(ww-dd,hh-dd -ydd);
				BorderPtArr[0][3] = cv::Point(dd,hh-dd-ydd);
				ptCount = 4;

				const cv::Point* ppt[1] = {BorderPtArr[0]};
				int npt[] = {ptCount};
				fillPoly( maskImg, ppt, npt,1,Scalar( 255, 255, 255 ),8);
				isGetMask = true;
			}
			else
			{
				const cv::Point* ppt[1] = {BorderPtArr[0]};
				int npt[] = {ptCount};
				fillPoly( maskImg, ppt, npt,1,Scalar( 255, 255, 255 ),8);
			}
			isGetMask = true;
		}

		if (key=='g' || key=='G')
		{  
			int ww = pRGBFrame.cols;
			int hh = pRGBFrame.rows;
			int dd = 8;
			BorderPtArr[0][0] = cv::Point(dd,dd);
			BorderPtArr[0][1] = cv::Point(ww-dd,dd);
			BorderPtArr[0][2] = cv::Point(ww-dd,hh-dd);
			BorderPtArr[0][3] = cv::Point(dd,hh-dd);
			ptCount = 4;

			const cv::Point* ppt[1] = {BorderPtArr[0]};
			int npt[] = {ptCount};
			fillPoly( maskImg, ppt, npt,1,Scalar( 255, 255, 255 ),8);
			isGetMask = true;
		}

		if (key=='r' || key=='R')
		{  
			ptCount = 0;
		}
	}
}
/取点交互相关end  
</pre><pre class="cpp" name="code">调用示例:
</pre><pre class="cpp" name="code"><pre class="cpp" name="code">VideoCapture cap;
				
		cap.open("H:\\video\\SZ\\20141210_070833.avi" );

		Mat frame;
		Mat resizeImg;
		namedWindow("video");

		int nframeNum = 0;

		bool isGetBorder = false;
		
		while(1)
		{
			cap>>frame;
			if (!frame.data) break;

			nframeNum++;

			if (isGetBorder == false)
			{
				imshow("video",frame);

				GetImgBorderPts(frame);
				isGetBorder = true;
			}
			
			imshow("video",middleFrame);
			

			int key = waitKey(1);
			if ((char)key == ' ')
			{
				waitKey(0);
			}

			if ((char)key == 'q' || (char)key == 'Q')
			{
				break;
			}
		}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小新识图

你的鼓励是我最大的分享动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值