OpenCV鼠标绘制矩形和截取矩形区域图像
本文实例为大家分享了OpenCV鼠标绘制截取矩形区域图像的具体代码,供大家参考,具体内容如下
在opencv中利用鼠标绘制矩形,代码如下:
#include#include #include #pragmacomment(lib,"cv.lib") #pragmacomment(lib,"cxcore.lib") #pragmacomment(lib,"highgui.lib") IplImage*src=0; IplImage*dst=0; voidon_mouse(intevent,intx,inty,intflags,void*ustc) { staticCvPointpre_pt={-1,-1}; staticCvPointcur_pt={-1,-1}; CvFontfont; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,0.5,0.5,0,1,CV_AA); chartemp[16]; if(event==CV_EVENT_LBUTTONDOWN) { cvCopy(dst,src); sprintf(temp,"(%d,%d)",x,y); pre_pt=cvPoint(x,y); cvPutText(src,temp,pre_pt,&font,cvScalar(0,0,0,255)); cvCircle(src,pre_pt,3,cvScalar(255,0,0,0),CV_FILLED,CV_AA,0); cvShowImage("src",src); cvCopy(src,dst); } elseif(event==CV_EVENT_MOUSEMOVE&&!(flags&CV_EVENT_FLAG_LBUTTON)) { cvCopy(dst,src); sprintf(temp,"(%d,%d)",x,y); cur_pt=cvPoint(x,y); cvPutText(src,temp,cur_pt,&font,cvScalar(0,0,0,255)); cvShowImage("src",src); } elseif(event==CV_EVENT_MOUSEMOVE&&(flags&CV_EVENT_FLAG_LBUTTON)) { cvCopy(dst,src); sprintf(temp,"(%d,%d)",x,y); cur_pt=cvPoint(x,y); cvPutText(src,temp,cur_pt,&font,cvScalar(0,0,0,255)); cvRectangle(src,pre_pt,cur_pt,cvScalar(0,255,0,0),1,8,0); cvShowImage("src",src); } elseif(event==CV_EVENT_LBUTTONUP) { sprintf(temp,"(%d,%d)",x,y); cur_pt=cvPoint(x,y); cvPutText(src,temp,cur_pt,&font,cvScalar(0,0,0,255)); cvCircle(src,cur_pt,3,cvScalar(255,0,0,0),CV_FILLED,CV_AA,0); cvRectangle(src,pre_pt,cur_pt,cvScalar(0,255,0,0),1,8,0); cvShowImage("src",src); cvCopy(src,dst); } } intmain() { src=cvLoadImage("lena.jpg",1); dst=cvCloneImage(src); cvNamedWindow("src",1); cvSetMouseCallback("src",on_mouse,0); cvShowImage("src",src); cvWaitKey(0); cvDestroyAllWindows(); cvReleaseImage(&src); cvReleaseImage(&dst); return0;
在opencv中利用鼠标绘制矩形并截取该矩形区域的图像
测试代码如下:
#include#include #include #pragmacomment(lib,"cv.lib") #pragmacomment(lib,"cxcore.lib") #pragmacomment(lib,"highgui.lib") IplImage*org=0; IplImage*img=0; IplImage*tmp=0; IplImage*dst=0; voidon_mouse(intevent,intx,inty,intflags,void*ustc) { staticCvPointpre_pt={-1,-1}; staticCvPointcur_pt={-1,-1}; CvFontfont; cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,0.5,0.5,0,1,CV_AA); chartemp[16]; if(event==CV_EVENT_LBUTTONDOWN) { cvCopy(org,img); sprintf(temp,"(%d,%d)",x,y); pre_pt=cvPoint(x,y); cvPutText(img,temp,pre_pt,&font,cvScalar(0,0,0,255)); cvCircle(img,pre_pt,3,cvScalar(255,0,0,0),CV_FILLED,CV_AA,0); cvShowImage("img",img); cvCopy(img,tmp); } elseif(event==CV_EVENT_MOUSEMOVE&&!(flags&CV_EVENT_FLAG_LBUTTON)) { cvCopy(tmp,img); sprintf(temp,"(%d,%d)",x,y); cur_pt=cvPoint(x,y); cvPutText(img,temp,cur_pt,&font,cvScalar(0,0,0,255)); cvShowImage("img",img); } elseif(event==CV_EVENT_MOUSEMOVE&&(flags&CV_EVENT_FLAG_LBUTTON)) { cvCopy(tmp,img); sprintf(temp,"(%d,%d)",x,y); cur_pt=cvPoint(x,y); cvPutText(img,temp,cur_pt,&font,cvScalar(0,0,0,255)); cvRectangle(img,pre_pt,cur_pt,cvScalar(0,255,0,0),1,8,0); cvShowImage("img",img); } elseif(event==CV_EVENT_LBUTTONUP) { cvCopy(tmp,img); sprintf(temp,"(%d,%d)",x,y); cur_pt=cvPoint(x,y); cvPutText(img,temp,cur_pt,&font,cvScalar(0,0,0,255)); cvCircle(img,cur_pt,3,cvScalar(255,0,0,0),CV_FILLED,CV_AA,0); cvRectangle(img,pre_pt,cur_pt,cvScalar(0,255,0,0),1,8,0); cvShowImage("img",img); cvCopy(img,tmp); intwidth=abs(pre_pt.x-cur_pt.x); intheight=abs(pre_pt.y-cur_pt.y); if(width==0||height==0) { cvDestroyWindow("dst"); return; } dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels); CvRectrect; if(pre_pt.x cur_pt.x&&pre_pt.y cur_pt.x&&pre_pt.y>cur_pt.y) { rect=cvRect(cur_pt.x,cur_pt.y,width,height); } elseif(pre_pt.x cur_pt.y) { rect=cvRect(pre_pt.x,cur_pt.y,width,height); } cvSetImageROI(org,rect); cvCopy(org,dst); cvResetImageROI(org); cvDestroyWindow("dst"); cvNamedWindow("dst",1); cvShowImage("dst",dst); cvSaveImage("dst.jpg",dst); } } intmain() { org=cvLoadImage("lena.jpg",1); img=cvCloneImage(org); tmp=cvCloneImage(org); cvNamedWindow("img",1); cvSetMouseCallback("img",on_mouse,0); cvShowImage("img",img); cvWaitKey(0); cvDestroyAllWindows(); cvReleaseImage(&org); cvReleaseImage(&img); cvReleaseImage(&tmp); cvReleaseImage(&dst); return0; }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。