opencv 取点交互 函数

来源:互联网 发布:上海11选5遗漏数据查询 编辑:程序博客网 时间:2024/06/03 21:35
//////////////////////////取点交互相关begin   #define  PtNUM 50cv::Point BorderPtArr[1][PtNUM];int ptCount = 0;//mouse event callbackvoid 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);elsecircle(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 0
原创粉丝点击