OpenCV鼠标如何绘制矩形和截取矩形区域图像-创新互联
这篇文章将为大家详细讲解有关OpenCV鼠标如何绘制矩形和截取矩形区域图像,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。
我们提供的服务有:成都网站设计、做网站、微信公众号开发、网站优化、网站认证、龙安ssl等。为近千家企事业单位解决了网站和推广的问题。提供周到的售前咨询和贴心的售后服务,是有科学管理、有技术的龙安网站制作公司具体内容如下
在opencv中利用鼠标绘制矩形,代码如下:
#include#include #include #pragma comment( lib, "cv.lib" ) #pragma comment( lib, "cxcore.lib" ) #pragma comment( lib, "highgui.lib" ) IplImage* src = 0; IplImage* dst = 0; void on_mouse( int event, int x, int y, int flags, void* ustc) { static CvPoint pre_pt = {-1,-1}; static CvPoint cur_pt = {-1,-1}; CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); char temp[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); } else if( 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 ); } else if( 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 ); } else if( 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); } } int main() { 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); return 0;
在opencv中利用鼠标绘制矩形并截取该矩形区域的图像
测试代码如下:
#include#include #include #pragma comment( lib, "cv.lib" ) #pragma comment( lib, "cxcore.lib" ) #pragma comment( lib, "highgui.lib" ) IplImage* org = 0; IplImage* img = 0; IplImage* tmp = 0; IplImage* dst = 0; void on_mouse( int event, int x, int y, int flags, void* ustc) { static CvPoint pre_pt = {-1,-1}; static CvPoint cur_pt = {-1,-1}; CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA); char temp[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); } else if( 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 ); } else if( 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 ); } else if( 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); int width=abs(pre_pt.x-cur_pt.x); int height=abs(pre_pt.y-cur_pt.y); if(width==0 || height==0) { cvDestroyWindow("dst"); return; } dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels); CvRect rect; 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); } else if(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); } } int main() { 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); return 0; }
关于“OpenCV鼠标如何绘制矩形和截取矩形区域图像”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。
另外有需要云服务器可以了解下创新互联建站www.cdcxhl.com,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。
当前标题:OpenCV鼠标如何绘制矩形和截取矩形区域图像-创新互联
浏览路径:http://hbruida.cn/article/dicipd.html