日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

鼠标绘制矩形

發(fā)布時間:2025/4/16 编程问答 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 鼠标绘制矩形 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

程序之一,在OpenCV中利用鼠標繪制矩形

#include <cv.h> #include <highgui.h> #include <stdio.h>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;} //查看鼠標畫出的矩形的大小,并用改大小表示這一區(qū)域的圖像dst = cvCreateImage(cvSize(width, height), org->depth, org->nChannels);CvRect rect; if (pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y){rect = cvRect(pre_pt.x, pre_pt.y, width, height);}else if (pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y){rect = cvRect(cur_pt.x, pre_pt.y, width, height);}else if (pre_pt.x>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.x && pre_pt.y>cur_pt.y){rect = cvRect(pre_pt.x, cur_pt.y, width, height);}cvSetImageROI(org, rect); //取圖像感興趣區(qū)域的圖像,rect里的圖像cvCopy(org, dst); cvResetImageROI(org);cvDestroyWindow("dst");cvNamedWindow("dst", 1);cvShowImage("dst", dst);cvSaveImage("dst.jpg", dst);} } int main() {org = cvLoadImage("D:6.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中利用鼠標繪制矩形并截取該矩形區(qū)域的圖像

[c-sharp]?view plaincopy
  • #include?<cv.h>??
  • #include?<highgui.h>??
  • #include?<stdio.h>??
  • #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.y)??
  • ????????{??
  • ????????????rect=cvRect(pre_pt.x,pre_pt.y,width,height);??
  • ????????}??
  • ????????else?if(pre_pt.x>cur_pt.x?&&?pre_pt.y<cur_pt.y)??
  • ????????{??
  • ????????????rect=cvRect(cur_pt.x,pre_pt.y,width,height);??
  • ????????}??
  • ????????else?if(pre_pt.x>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.x?&&?pre_pt.y>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;??
  • }??
  • ?

    效果圖如下

    ?

    總結

    以上是生活随笔為你收集整理的鼠标绘制矩形的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。