OpenCV使用鼠標(biāo)響應(yīng)裁剪圖像
給定一幅圖像,將其中的某一部分興趣區(qū)域裁剪出來,這在PS中很好實(shí)現(xiàn),但是使用openCV如何實(shí)現(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;
}
二、程序運(yùn)行效果圖:

將鼠標(biāo)放在原圖上的某一點(diǎn),會顯示相應(yīng)點(diǎn)的位置坐標(biāo)。至此,openCV使用鼠標(biāo)響應(yīng)實(shí)現(xiàn)圖像裁剪已經(jīng)實(shí)現(xiàn)。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C++入門之vector的底層實(shí)現(xiàn)詳解
這篇文章主要為大家介紹了C++入門之vector的底層實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2021-11-11
詳解設(shè)計(jì)模式中的模板方法模式及在C++中的使用
這篇文章主要介紹了設(shè)計(jì)模式中的模板方法模式及在C++中的使用,模板方法將邏輯封裝到一個(gè)類中,并采取組合(委托)的方式解決這個(gè)問題,需要的朋友可以參考下2016-03-03
opencv+arduino實(shí)現(xiàn)物體點(diǎn)追蹤效果
這篇文章主要為大家詳細(xì)介紹了opencv+arduino實(shí)現(xiàn)物體點(diǎn)追蹤效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01
C語言數(shù)據(jù)結(jié)構(gòu)之二分法查找詳解
二分查找算法是在有序數(shù)組中用到的較為頻繁的一種算法,在未接觸二分查找算法時(shí),最通用的一種做法是,對數(shù)組進(jìn)行遍歷,跟每個(gè)元素進(jìn)行比較,其時(shí)間為O(n),但二分查找算法更優(yōu)2022-02-02

