opencv保存选择图像中的区域

在自己建立行人检测的图像库时用到,参考别人的修改了一下:

  1 #include "opencv2/core/core.hpp"  
  2 #include "opencv2/highgui/highgui.hpp"  
  3 #include "opencv2/imgproc/imgproc.hpp"  
  4 #include <stdio.h>
  5 using namespace cv;
  6 
  7 IplImage* org = 0;
  8 IplImage* img = 0; 
  9 IplImage* tmp = 0; 
 10 IplImage* dst = 0; 
 11 static int n=0;
 12 static char savename[20];
 13 void on_mouse( int event, int x, int y, int flags, void* ustc)
 14 {
 15     static CvPoint pre_pt = {-1,-1};
 16     static CvPoint cur_pt = {-1,-1};
 17     CvFont font;
 18     cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
 19     char temp[16];
    char lenth_and_height[30];//用于查看选区的宽度和高度


 20 
 21     if( event == CV_EVENT_LBUTTONDOWN )
 22     {
 23         cvCopy(org,img);
 24         sprintf(temp,"(%d,%d)",x,y);
 25         pre_pt = cvPoint(x,y);
 26         cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
 27         cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
 28         cvShowImage( "img", img );
 29         cvCopy(img,tmp);
 30     }
 31     else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
 32     {
 33         cvCopy(tmp,img);
 34         sprintf(temp,"(%d,%d)",x,y);
 35         cur_pt = cvPoint(x,y);        
 36         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
 37         cvShowImage( "img", img );
 38     }
 39     else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
 40     {
 41         cvCopy(tmp,img);
 42         sprintf(temp,"(%d,%d)",x,y);
 43         cur_pt = cvPoint(x,y);        
 44         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
 45         cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
 1         sprintf(lenth_and_height,"(Width:%d,Height:%d)",abs(cur_pt.x-pre_pt.x),abs(cur_pt.y-pre_pt.y));
 2         /*-----------------------------------------------------------------------------
 3          *
 4          *
 5          *  为了方便随时查看自己选区的宽度和高度,特意加了一个点,该点计算为取矩形窗左上点和右下点的中点
 6          *
 7          *
 8          *-----------------------------------------------------------------------------*/
 9         CvPoint origin;
10         origin.x=(pre_pt.x+cur_pt.x)/2;
11         origin.y=(pre_pt.y+cur_pt.y)/2;
12         cvPutText(img,lenth_and_height, origin, &font, cvScalar(0,0, 0, 255));


 46         cvShowImage( "img", img );
 47     }
 48     else if( event == CV_EVENT_LBUTTONUP )
 49     {
 50         cvCopy(tmp,img);
 51         sprintf(temp,"(%d,%d)",x,y);
 52         cur_pt = cvPoint(x,y);        
 53         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
 54         cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
 55         cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
 56         cvShowImage( "img", img );
 57         cvCopy(img,tmp);
 58         int width=abs(pre_pt.x-cur_pt.x);
 59         int height=abs(pre_pt.y-cur_pt.y);
 60         if(width==0 || height==0)
 61         {
 62             cvDestroyWindow("dst");
 63             return;
 64         }
 65         dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);
 66         CvRect rect;
 67         if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)
 68         {
 69             rect=cvRect(pre_pt.x,pre_pt.y,width,height);
 70         }
 71         else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)
 72         {
 73             rect=cvRect(cur_pt.x,pre_pt.y,width,height);
 74         }
 75         else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)
 76         {
 77             rect=cvRect(cur_pt.x,cur_pt.y,width,height);
 78         }
 79         else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)
 80         {
 81             rect=cvRect(pre_pt.x,cur_pt.y,width,height);
 82         }
 83         cvSetImageROI(org,rect);
 84         cvCopy(org,dst);
 85         cvResetImageROI(org);
 86         cvDestroyWindow("dst");
 87         cvNamedWindow("dst",1);
 88         cvShowImage("dst",dst);
 89         
 90         sprintf(savename,"save%03d.jpg",n);
 91 
 92         cvSaveImage(savename,dst);
 93         n++;
 94     }
 95 }
 96 int main(int argc, char *argv[])
 97 {
 98     
 99     
100     org=cvLoadImage(argv[1],1);
101     img=cvCloneImage(org);
102     tmp=cvCloneImage(org);
103     cvNamedWindow("img",1);
104     cvSetMouseCallback( "img", on_mouse, 0 );
105 
106     cvShowImage("img",img);
107     cvWaitKey(0); 
108     cvDestroyAllWindows();
109     cvReleaseImage(&org);
110     cvReleaseImage(&img);
111     cvReleaseImage(&tmp);
112     cvReleaseImage(&dst);
113     return 0;
114 }

运行效果:

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/yuliyang/p/3368848.html