欢迎来到代码驿站!

C代码

当前位置:首页 > 软件编程 > C代码

OpenCV使用鼠标响应裁剪图像

时间:2020-11-18 00:08:46|栏目:C代码|点击:

给定一幅图像,将其中的某一部分兴趣区域裁剪出来,这在PS中很好实现,但是使用openCV如何实现呢?因此本文主要介绍openCV使用鼠标响应来裁剪图像:

一、代码部分:

#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;
}

二、程序运行效果图:

将鼠标放在原图上的某一点,会显示相应点的位置坐标。至此,openCV使用鼠标响应实现图像裁剪已经实现。

上一篇:Opencv LBPH人脸识别算法详解

栏    目:C代码

下一篇:关于C++友元类的实现讲解

本文标题:OpenCV使用鼠标响应裁剪图像

本文地址:http://www.codeinn.net/misctech/23618.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有