服务器之家

服务器之家 > 正文

OpenCV鼠标绘制矩形和截取矩形区域图像

时间:2021-07-19 09:30     来源/作者:chongshangyunxiao321

本文实例为大家分享了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
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )
IplImage* src = 0; 
IplImage* dst = 0; 
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(dst,src);
    sprintf(temp,"(%d,%d)",x,y);
    pre_pt = cvPoint(x,y);
    cvPutText(src,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
    cvCircle( src, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
    cvShowImage( "src", src );
    cvCopy(src,dst);
  }
  else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
  {
    cvCopy(dst,src);
    sprintf(temp,"(%d,%d)",x,y);
    cur_pt = cvPoint(x,y);   
    cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
    cvShowImage( "src", src );
  }
  else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
  {
    cvCopy(dst,src);
    sprintf(temp,"(%d,%d)",x,y);
    cur_pt = cvPoint(x,y);   
    cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
    cvRectangle(src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
    cvShowImage( "src", src );
  }
  else if( event == CV_EVENT_LBUTTONUP )
  {
    sprintf(temp,"(%d,%d)",x,y);
    cur_pt = cvPoint(x,y);   
    cvPutText(src,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
    cvCircle( src, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
    cvRectangle( src, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
    cvShowImage( "src", src );
    cvCopy(src,dst);
  }
}
int main()
{
  src=cvLoadImage("lena.jpg",1);
  dst=cvCloneImage(src);
  cvNamedWindow("src",1);
  cvSetMouseCallback( "src", on_mouse, 0 );
 
  cvShowImage("src",src);
  cvWaitKey(0); 
  cvDestroyAllWindows();
  cvReleaseImage(&src);
  cvReleaseImage(&dst);
  return 0;

在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
#include <cv.h>
#include <highgui.h>
#include <stdio.h>
#pragma comment( lib, "cv.lib" )
#pragma comment( lib, "cxcore.lib" )
#pragma comment( lib, "highgui.lib" )
IplImage* org = 0;
IplImage* img = 0; 
IplImage* tmp = 0; 
IplImage* dst = 0; 
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);
    cvSaveImage("dst.jpg",dst);
  }
}
int main()
{
  org=cvLoadImage("lena.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;
}

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

原文链接:https://blog.csdn.net/chongshangyunxiao321/article/details/50993910

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部