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),會(huì)顯示相應(yīng)點(diǎn)的位置坐標(biāo)。至此,openCV使用鼠標(biāo)響應(yīng)實(shí)現(xiàn)圖像裁剪已經(jīng)實(shí)現(xiàn)。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:MFC實(shí)現(xiàn)連連看游戲之消子算法
欄 目:C語言
下一篇:OpenCV利用霍夫變換進(jìn)行直線檢測(cè)
本文標(biāo)題:OpenCV使用鼠標(biāo)響應(yīng)裁剪圖像
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/548.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 01-10使用OpenGL實(shí)現(xiàn)3D立體顯示的程序代碼
- 01-10使用C++實(shí)現(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()的深入探討


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(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語言中對(duì)數(shù)函數(shù)的表達(dá)式 c語言中對(duì)
- 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ù)求
隨機(jī)閱讀
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 04-02jquery與jsp,用jquery


