opencv保存选择图像中的区域(二)

 1 /*
 2  * =====================================================================================
 3  *
 4  *       Filename:  resize.cpp
 5  *      Environment:    
 6  *    Description:  图像list为参数输入程序,resize后的图像在picture_resize下
 7  *
 8  *
 9  *        Version:  1.0
10  *        Created:  2013/10/15 12:23:51
11  *         Author:  yuliyang
12 I*
13  *             Mail:  wzyuliyang911@gmail.com
14  *             Blog:  http://www.cnblogs.com/yuliyang
15  *
16  * =====================================================================================
17  */
18 
19 #include "opencvcv.h"
20 #include "opencvhighgui.h"
21 #include <math.h>
22 #include "windows.h"
23 #include "fstream"
24 #include <cstdlib>
25 #include <string.h>
26 using namespace std;
27 static int n=0;
28 static char savename_resize[50];
29 int main(int argc, char* argv[])
30 {
31 
32     /*-----------------------------------------------------------------------------
33      *  创建目标目录,保存resize图像
34      *  DOS命令
35      *-----------------------------------------------------------------------------*/
36     system("md .\picture_resize");
37     IplImage *src = 0;            //源图像指针
38     IplImage *dst = 0;            //目标图像指针
39     CvSize dst_cvsize;            //目标图像尺寸
40     dst_cvsize.width=64;
41     dst_cvsize.height=128;
42     string buf;    
43     ifstream svm_data(argv[1]);//训练样本图片的路径都写在这个txt文件中,使用bat批处理文件可以得到这个txt文件   
44     while (svm_data)
45     {
46         if (getline(svm_data,buf))
47         {
48             
49              printf("processing %s...
",buf.c_str());
50              src = cvLoadImage(buf.c_str());
51              dst = cvCreateImage( dst_cvsize, src->depth, src->nChannels);    //构造目标图象
52              cvResize(src, dst, CV_INTER_LINEAR);    //缩放源图像到目标图像
53              sprintf(savename_resize,".//picture_resize//resize%03dr.bmp",n);
54              cvSaveImage(savename_resize,dst);
55              n++;
56              cvReleaseImage(&src);    //释放源图像占用的内存
57              cvReleaseImage(&dst);    //释放目标图像占用的内存
58         }
59     }
60     
61     svm_data.close();
62     
63     /*-----------------------------------------------------------------------------
64      *  bat批处理:
65      *
66      * 在num.txt文件中的偶数行插入数字0,分类器的label
67      *
68      setlocal enabledelayedexpansion
69      for /f "delims=  " %%a in (num.txt) do (
70      set /a line =0
71      echo %%a >>0.txt
72      echo !line! >>0.txt
73      )
74 
75      将目录下的所以文件名写入num.txt文件里
76      dir /b/s/p/w *.bmp > num.txt
77 
78      将所有0.txt - 9.txt内的内容全部写入到hb.txt
79      @echo off
80      set d=d:
ums
81      pushd %d%
82      del hb.tmp 2>nul
83      for /f "tokens=*" %%i in ('dir/b/s *.txt') do type "%%i">>hb.txt
84      ren hb.tmp hb.txt
85      popd
86      
87      *  
88      *-----------------------------------------------------------------------------*/
89     
90     /* the first command line parameter must be image file name */
91     // src = cvLoadImage(argv[1]);
92     // dst_cvsize.width = src->width * scale;        //目标图像的宽为源图象宽的scale倍
93     // dst_cvsize.height = src->height * scale;    //目标图像的高为源图象高的scale倍
94     //
95         
96     return 0;
97 }
  1 /*
  2  * =====================================================================================
  3  *
  4  *       Filename:  select2save.cpp
  5  *    Description:  获取感兴趣的选区,自动创建目录和resize大小
  6  *    环境:opencv2.4.4和vs2010
  7  *
  8  *
  9  *        Version:  2
 10  *        Created:  2013/10/14 19:52:09
 11  *         Author:  yuliyang
 12  *            Weibo:    @礼杨_HDU
 13  *             Mail:  wzyuliyang911@gmail.com
 14  *             Blog:  http://www.cnblogs.com/yuliyang
 15  *
 16  * =====================================================================================
 17  */
 18 #include "opencvcv.h"
 19 /*-----------------------------------------------------------------------------
 20  *  很奇怪的的是cvresize()在这个头文件里,加了它才有定义,否则编译会说函数未定义
 21  *-----------------------------------------------------------------------------*/
 22 #include "opencvhighgui.h"
 23 #include "opencv2/core/core.hpp"  
 24 #include "opencv2/highgui/highgui.hpp"  
 25 #include "opencv2/imgproc/imgproc.hpp"  
 26 #include <stdio.h>
 27 #include <math.h>
 28 using namespace cv;
 29 
 30 IplImage* org = 0;
 31 IplImage* img = 0; 
 32 IplImage* tmp = 0; 
 33 IplImage* dst = 0; 
 34 IplImage* dst_resize = 0; 
 35 static int n=0;
 36 static char savename[50];
 37 static char savename_resize[50];
 38 void on_mouse( int event, int x, int y, int flags, void* ustc)
 39 {
 40     static CvPoint pre_pt = {-1,-1};
 41     static CvPoint cur_pt = {-1,-1};
 42     CvFont font;
 43     cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, 0, 1, CV_AA);
 44     char temp[16];
 45     char lenth_and_height[30];
 46 
 47     if( event == CV_EVENT_LBUTTONDOWN )
 48     {
 49         cvCopy(org,img);
 50         sprintf(temp,"(%d,%d)",x,y);
 51         pre_pt = cvPoint(x,y);
 52         cvPutText(img,temp, pre_pt, &font, cvScalar(0,0, 0, 255));
 53         cvCircle( img, pre_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
 54         cvShowImage( "img", img );
 55         cvCopy(img,tmp);
 56     }
 57     else if( event == CV_EVENT_MOUSEMOVE && !(flags & CV_EVENT_FLAG_LBUTTON))
 58     {
 59         cvCopy(tmp,img);
 60         sprintf(temp,"(%d,%d)",x,y);
 61         cur_pt = cvPoint(x,y);        
 62         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
 63         cvShowImage( "img", img );
 64     }
 65     else if( event == CV_EVENT_MOUSEMOVE && (flags & CV_EVENT_FLAG_LBUTTON))
 66     {
 67         cvCopy(tmp,img);
 68         sprintf(temp,"(%d,%d)",x,y);
 69         cur_pt = cvPoint(x,y);        
 70         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
 71         cvRectangle(img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
 72         sprintf(lenth_and_height,"(Width:%d,Height:%d)",abs(cur_pt.x-pre_pt.x),abs(cur_pt.y-pre_pt.y));
 73         /*-----------------------------------------------------------------------------
 74          *
 75          *
 76          *  为了方便随时查看自己选区的宽度和高度,特意加了一个点,该点计算为取矩形窗左上点和右下点的中点
 77          *
 78          *
 79          *-----------------------------------------------------------------------------*/
 80         CvPoint origin;
 81         origin.x=(pre_pt.x+cur_pt.x)/2;
 82         origin.y=(pre_pt.y+cur_pt.y)/2;
 83         cvPutText(img,lenth_and_height, origin, &font, cvScalar(0,0, 0, 255));
 84         cvShowImage( "img", img );
 85     }
 86     else if( event == CV_EVENT_LBUTTONUP )
 87     {
 88         cvCopy(tmp,img);
 89         sprintf(temp,"(%d,%d)",x,y);
 90         cur_pt = cvPoint(x,y);        
 91         cvPutText(img,temp, cur_pt, &font, cvScalar(0,0, 0, 255));
 92         cvCircle( img, cur_pt, 3,cvScalar(255,0,0,0) ,CV_FILLED, CV_AA, 0 );
 93         cvRectangle( img, pre_pt, cur_pt, cvScalar(0,255,0,0), 1, 8, 0 );
 94         cvShowImage( "img", img );
 95         cvCopy(img,tmp);
 96         int width=abs(pre_pt.x-cur_pt.x);
 97         int height=abs(pre_pt.y-cur_pt.y);
 98         if(width==0 || height==0)
 99         {
100             cvDestroyWindow("dst");
101             return;
102         }
103         dst=cvCreateImage(cvSize(width,height),org->depth,org->nChannels);
104         CvRect rect;
105         if(pre_pt.x<cur_pt.x && pre_pt.y<cur_pt.y)
106         {
107             rect=cvRect(pre_pt.x,pre_pt.y,width,height);
108         }
109         else if(pre_pt.x>cur_pt.x && pre_pt.y<cur_pt.y)
110         {
111             rect=cvRect(cur_pt.x,pre_pt.y,width,height);
112         }
113         else if(pre_pt.x>cur_pt.x && pre_pt.y>cur_pt.y)
114         {
115             rect=cvRect(cur_pt.x,cur_pt.y,width,height);
116         }
117         else if(pre_pt.x<cur_pt.x && pre_pt.y>cur_pt.y)
118         {
119             rect=cvRect(pre_pt.x,cur_pt.y,width,height);
120         }
121         cvSetImageROI(org,rect);
122         cvCopy(org,dst);
123     
124         /*-----------------------------------------------------------------------------
125          *  定义保存图像的大小
126          *-----------------------------------------------------------------------------*/
127         CvSize dst_cvsize;            
128         dst_cvsize.width=64;
129         dst_cvsize.height=128;
130         dst_resize = cvCreateImage( dst_cvsize, org->depth, org->nChannels);
131         cvResize(org, dst_resize, CV_INTER_LINEAR);
132         
133         cvResetImageROI(org);
134         cvDestroyWindow("dst");
135         //cvNamedWindow("dst",1);
136         //cvShowImage("dst",dst);
137         
138         /*-----------------------------------------------------------------------------
139          *  保存未resize的图像
140          *-----------------------------------------------------------------------------*/
141         sprintf(savename,".\picture\save%03d.bmp",n);
142         /*-----------------------------------------------------------------------------
143          *  保存在resize文件夹下
144          *-----------------------------------------------------------------------------*/
145         sprintf(savename_resize,".\picture_resize\save%03dr.bmp",n);
146 
147         cvSaveImage(savename,dst);
148         cvSaveImage(savename_resize,dst_resize);
149         n++;
150     }
151 }
152 int main(int argc, char *argv[])
153 {
154     
155     /*-----------------------------------------------------------------------------
156      *  用DOS命令创建文件夹,用于分类resize和未resize的原图像
157      *-----------------------------------------------------------------------------*/
158     system("md .\picture_resize");
159     system("md .\picture");
160     org=cvLoadImage(argv[1],1);
161     img=cvCloneImage(org);
162     tmp=cvCloneImage(org);
163     cvNamedWindow("img",1);
164     cvSetMouseCallback( "img", on_mouse, 0 );
165 
166     cvShowImage("img",img);
167     cvWaitKey(0); 
168     cvDestroyAllWindows();
169     cvReleaseImage(&org);
170     cvReleaseImage(&img);
171     cvReleaseImage(&tmp);
172     cvReleaseImage(&dst);
173     return 0;
174 }
View Code
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/yuliyang/p/3370002.html