服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C/C++ - OpenCV使用鼠标响应裁剪图像

OpenCV使用鼠标响应裁剪图像

2021-07-17 18:02lindamtd C/C++

这篇文章主要为大家详细介绍了OpenCV实现鼠标响应裁剪图像,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

一、代码部分:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#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使用鼠标响应实现图像裁剪已经实现。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/lindamtd/article/details/75203514

延伸 · 阅读

精彩推荐