国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C > 正文

OpenCV使用鼠標(biāo)響應(yīng)裁剪圖像

2020-01-26 13:34:27
字體:
供稿:網(wǎ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 accordinglyvoid 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í)有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 白城市| 明水县| 舒兰市| 嘉兴市| 神农架林区| 巴彦淖尔市| 满洲里市| 通山县| 东光县| 栾川县| 乌鲁木齐市| 朔州市| 合肥市| 格尔木市| 桂林市| 宣汉县| 枣强县| 姜堰市| 子洲县| 寻乌县| 徐水县| 蓬安县| 大足县| 岚皋县| 东至县| 英吉沙县| 延川县| 凭祥市| 万安县| 南陵县| 大姚县| 福海县| 桓仁| 光山县| 抚顺市| 阜宁县| 陵川县| 昌图县| 称多县| 尉氏县| 密山市|