您好,登錄后才能下訂單哦!
本文實例為大家分享了OpenCV實現(xiàn)鼠標(biāo)框選并顯示框選區(qū)域的具體代碼,供大家參考,具體內(nèi)容如下
鼠標(biāo)在圖像上框選目標(biāo):
1. 用到鼠標(biāo)回調(diào)函數(shù)
2. 回調(diào)函數(shù)里面分三部分
(1)鼠標(biāo)按下左鍵
(2)鼠標(biāo)按下的同時移動左鍵
(3)鼠標(biāo)左鍵彈起
3. 圖像顯示的刷新
單目標(biāo):
#include <cv.h> #include <highgui.h> #include <stdio.h> IplImage* src = 0; IplImage* tmp = 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)&&(flags) )//鼠標(biāo)左鍵按下時 { sprintf(temp,"(%d,%d)",x,y);//格式化字符串 pre_pt = cvPoint(x,y);//獲取當(dāng)前點坐標(biāo)值 cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));//在圖像是打印字符 cvCircle( src, pre_pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在圖像上畫圓 cvShowImage( "src", src ); //cvCopy(src,tmp);//這句有沒有,就是單目標(biāo)和多目標(biāo)的問題 } else if( (event == CV_EVENT_MOUSEMOVE) && (flags & CV_EVENT_LBUTTONDOWN)) {//鼠標(biāo)移動并且鼠標(biāo)左鍵按下 sprintf(temp,"(%d,%d)",x,y);//格式化字符串 cur_pt = cvPoint(x,y);//獲取當(dāng)前點坐標(biāo)值 cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));//在圖像是打印字符 cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 2, 8, 0 );//在圖像上畫矩形 cvShowImage( "src", src ); cvCopy(tmp,src);//將img復(fù)制到臨時圖像tmp上,用于實時顯示 } else if( event == CV_EVENT_LBUTTONUP ) {//鼠標(biāo)左鍵彈起 sprintf(temp,"(%d,%d)",x,y);//字體格式化 cur_pt = cvPoint(x,y);//獲取當(dāng)前點坐標(biāo)值 cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));//在圖像是打印字符 cvCircle( src, cur_pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在圖像上畫圓 cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 2, 8, 0 );//在圖像上畫矩形 cvShowImage( "src", src ); } } int main() { src=cvLoadImage("lena.jpg",1);//讀入圖像 tmp=cvCloneImage(src);//復(fù)制圖像到臨時圖像上 cvNamedWindow("src",1);//新建窗口 cvSetMouseCallback( "src", on_mouse, 0 );//注冊鼠標(biāo)響應(yīng)回調(diào)函數(shù) cvShowImage("src",src);//顯示圖像 cvWaitKey(0);//等待按鍵按下 cvDestroyAllWindows();//銷毀所有窗口 cvReleaseImage(&src);//釋放圖像 cvReleaseImage(&tmp);//釋放圖像 return 0; }
多目標(biāo):
#include <cv.h> #include <highgui.h> #include <stdio.h> IplImage* src = 0; IplImage* tmp = 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)&&(flags) )//鼠標(biāo)左鍵按下時 { sprintf(temp,"(%d,%d)",x,y);//格式化字符串 pre_pt = cvPoint(x,y);//獲取當(dāng)前點坐標(biāo)值 cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));//在圖像是打印字符 cvCircle( src, pre_pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在圖像上畫圓 cvShowImage( "src", src ); cvCopy(src,tmp);//這句有沒有,就是單目標(biāo)和多目標(biāo)的問題 } else if( (event == CV_EVENT_MOUSEMOVE) && (flags & CV_EVENT_LBUTTONDOWN)) {//鼠標(biāo)移動并且鼠標(biāo)左鍵按下 sprintf(temp,"(%d,%d)",x,y);//格式化字符串 cur_pt = cvPoint(x,y);//獲取當(dāng)前點坐標(biāo)值 cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));//在圖像是打印字符 cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 2, 8, 0 );//在圖像上畫矩形 cvShowImage( "src", src ); cvCopy(tmp,src);//將img復(fù)制到臨時圖像tmp上,用于實時顯示 } else if( event == CV_EVENT_LBUTTONUP ) {//鼠標(biāo)左鍵彈起 sprintf(temp,"(%d,%d)",x,y);//字體格式化 cur_pt = cvPoint(x,y);//獲取當(dāng)前點坐標(biāo)值 cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));//在圖像是打印字符 cvCircle( src, cur_pt, 2,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );//在圖像上畫圓 cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 2, 8, 0 );//在圖像上畫矩形 cvShowImage( "src", src ); } } int main() { src=cvLoadImage("lena.jpg",1);//讀入圖像 tmp=cvCloneImage(src);//復(fù)制圖像到臨時圖像上 cvNamedWindow("src",1);//新建窗口 cvSetMouseCallback( "src", on_mouse, 0 );//注冊鼠標(biāo)響應(yīng)回調(diào)函數(shù) cvShowImage("src",src);//顯示圖像 cvWaitKey(0);//等待按鍵按下 cvDestroyAllWindows();//銷毀所有窗口 cvReleaseImage(&src);//釋放圖像 cvReleaseImage(&tmp);//釋放圖像 return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。