OpenCV使用鼠標(biāo)響應(yīng)裁剪圖像
給定一幅圖像,將其中的某一部分興趣區(qū)域裁剪出來,這在PS中很好實現(xiàn),但是使用openCV如何實現(xiàn)呢?因此本文主要介紹openCV使用鼠標(biāo)響應(yīng)來裁剪圖像:
一、代碼部分:
#include "stdafx.h" #include "cv.h" #include <highgui.h> #include <stdio.h> IplImage* org = 0; IplImage* img = 0; IplImage* tmp = 0; IplImage* dst = 0; //The mouse cuts the image accordingly 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); cvWaitKey(0); cvSaveImage("..\\post_img\\71253.jpg",dst); } } int _tmain(int argc, _TCHAR* argv[]) { org=cvLoadImage("..\\image_norm\\71253.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; }
二、程序運行效果圖:
將鼠標(biāo)放在原圖上的某一點,會顯示相應(yīng)點的位置坐標(biāo)。至此,openCV使用鼠標(biāo)響應(yīng)實現(xiàn)圖像裁剪已經(jīng)實現(xiàn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 01-10使用OpenGL實現(xiàn)3D立體顯示的程序代碼
- 01-10使用C++實現(xiàn)全排列算法的方法詳解
- 01-10深入Main函數(shù)中的參數(shù)argc,argv的使用詳解
- 01-10C++大數(shù)模板(推薦)
- 01-10淺談C/C++中的static與extern關(guān)鍵字的使用詳解
- 01-10基于C語言fflush()函數(shù)的使用詳解
- 01-10linux c 查找使用庫的cflags與libs的方法詳解
- 01-10深入sizeof的使用詳解
- 01-10基于c中使用ftruncate()前需要fflush(),使用后需要rewind()的深入探討


閱讀排行
本欄相關(guān)
- 04-02c語言函數(shù)調(diào)用后清空內(nèi)存 c語言調(diào)用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達(dá)
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達(dá)式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調(diào)用函數(shù)求fibo C語言調(diào)用函數(shù)求
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 04-02jquery與jsp,用jquery