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

时间:2023-03-09 02:14:26
opencv保存选择图像中的区域(二)
 /*
* =====================================================================================
*
* Filename: resize.cpp
* Environment:
* Description: 图像list为参数输入程序,resize后的图像在picture_resize下
*
*
* Version: 1.0
* Created: 2013/10/15 12:23:51
* Author: yuliyang
I*
* Mail: wzyuliyang911@gmail.com
* Blog: http://www.cnblogs.com/yuliyang
*
* =====================================================================================
*/ #include "opencv\cv.h"
#include "opencv\highgui.h"
#include <math.h>
#include "windows.h"
#include "fstream"
#include <cstdlib>
#include <string.h>
using namespace std;
static int n=;
static char savename_resize[];
int main(int argc, char* argv[])
{ /*-----------------------------------------------------------------------------
* 创建目标目录,保存resize图像
* DOS命令
*-----------------------------------------------------------------------------*/
system("md .\\picture_resize");
IplImage *src = ; //源图像指针
IplImage *dst = ; //目标图像指针
CvSize dst_cvsize; //目标图像尺寸
dst_cvsize.width=;
dst_cvsize.height=;
string buf;
ifstream svm_data(argv[]);//训练样本图片的路径都写在这个txt文件中,使用bat批处理文件可以得到这个txt文件
while (svm_data)
{
if (getline(svm_data,buf))
{ printf("processing %s...\n",buf.c_str());
src = cvLoadImage(buf.c_str());
dst = cvCreateImage( dst_cvsize, src->depth, src->nChannels); //构造目标图象
cvResize(src, dst, CV_INTER_LINEAR); //缩放源图像到目标图像
sprintf(savename_resize,".//picture_resize//resize%03dr.bmp",n);
cvSaveImage(savename_resize,dst);
n++;
cvReleaseImage(&src); //释放源图像占用的内存
cvReleaseImage(&dst); //释放目标图像占用的内存
}
} svm_data.close(); /*-----------------------------------------------------------------------------
* bat批处理:
*
* 在num.txt文件中的偶数行插入数字0,分类器的label
*
setlocal enabledelayedexpansion
for /f "delims= " %%a in (num.txt) do (
set /a line =0
echo %%a >>0.txt
echo !line! >>0.txt
) 将目录下的所以文件名写入num.txt文件里
dir /b/s/p/w *.bmp > num.txt 将所有0.txt - 9.txt内的内容全部写入到hb.txt
@echo off
set d=d:\nums
pushd %d%
del hb.tmp 2>nul
for /f "tokens=*" %%i in ('dir/b/s *.txt') do type "%%i">>hb.txt
ren hb.tmp hb.txt
popd *
*-----------------------------------------------------------------------------*/ /* the first command line parameter must be image file name */
// src = cvLoadImage(argv[1]);
// dst_cvsize.width = src->width * scale; //目标图像的宽为源图象宽的scale倍
// dst_cvsize.height = src->height * scale; //目标图像的高为源图象高的scale倍
// return ;
}
 /*
* =====================================================================================
*
* Filename: select2save.cpp
* Description: 获取感兴趣的选区,自动创建目录和resize大小
* 环境:opencv2.4.4和vs2010
*
*
* Version: 2
* Created: 2013/10/14 19:52:09
* Author: yuliyang
* Weibo: @礼杨_HDU
* Mail: wzyuliyang911@gmail.com
* Blog: http://www.cnblogs.com/yuliyang
*
* =====================================================================================
*/
#include "opencv\cv.h"
/*-----------------------------------------------------------------------------
* 很奇怪的的是cvresize()在这个头文件里,加了它才有定义,否则编译会说函数未定义
*-----------------------------------------------------------------------------*/
#include "opencv\highgui.h"
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <stdio.h>
#include <math.h>
using namespace cv; IplImage* org = ;
IplImage* img = ;
IplImage* tmp = ;
IplImage* dst = ;
IplImage* dst_resize = ;
static int n=;
static char savename[];
static char savename_resize[];
void on_mouse( int event, int x, int y, int flags, void* ustc)
{
static CvPoint pre_pt = {-,-};
static CvPoint cur_pt = {-,-};
CvFont font;
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.5, 0.5, , , CV_AA);
char temp[];
char lenth_and_height[]; 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(,, , ));
cvCircle( img, pre_pt, ,cvScalar(,,,) ,CV_FILLED, CV_AA, );
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(,, , ));
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(,, , ));
cvRectangle(img, pre_pt, cur_pt, cvScalar(,,,), , , );
sprintf(lenth_and_height,"(Width:%d,Height:%d)",abs(cur_pt.x-pre_pt.x),abs(cur_pt.y-pre_pt.y));
/*-----------------------------------------------------------------------------
*
*
* 为了方便随时查看自己选区的宽度和高度,特意加了一个点,该点计算为取矩形窗左上点和右下点的中点
*
*
*-----------------------------------------------------------------------------*/
CvPoint origin;
origin.x=(pre_pt.x+cur_pt.x)/;
origin.y=(pre_pt.y+cur_pt.y)/;
cvPutText(img,lenth_and_height, origin, &font, cvScalar(,, , ));
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(,, , ));
cvCircle( img, cur_pt, ,cvScalar(,,,) ,CV_FILLED, CV_AA, );
cvRectangle( img, pre_pt, cur_pt, cvScalar(,,,), , , );
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== || height==)
{
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); /*-----------------------------------------------------------------------------
* 定义保存图像的大小
*-----------------------------------------------------------------------------*/
CvSize dst_cvsize;
dst_cvsize.width=;
dst_cvsize.height=;
dst_resize = cvCreateImage( dst_cvsize, org->depth, org->nChannels);
cvResize(org, dst_resize, CV_INTER_LINEAR); cvResetImageROI(org);
cvDestroyWindow("dst");
//cvNamedWindow("dst",1);
//cvShowImage("dst",dst); /*-----------------------------------------------------------------------------
* 保存未resize的图像
*-----------------------------------------------------------------------------*/
sprintf(savename,".\\picture\\save%03d.bmp",n);
/*-----------------------------------------------------------------------------
* 保存在resize文件夹下
*-----------------------------------------------------------------------------*/
sprintf(savename_resize,".\\picture_resize\\save%03dr.bmp",n); cvSaveImage(savename,dst);
cvSaveImage(savename_resize,dst_resize);
n++;
}
}
int main(int argc, char *argv[])
{ /*-----------------------------------------------------------------------------
* 用DOS命令创建文件夹,用于分类resize和未resize的原图像
*-----------------------------------------------------------------------------*/
system("md .\\picture_resize");
system("md .\\picture");
org=cvLoadImage(argv[],);
img=cvCloneImage(org);
tmp=cvCloneImage(org);
cvNamedWindow("img",);
cvSetMouseCallback( "img", on_mouse, ); cvShowImage("img",img);
cvWaitKey();
cvDestroyAllWindows();
cvReleaseImage(&org);
cvReleaseImage(&img);
cvReleaseImage(&tmp);
cvReleaseImage(&dst);
return ;
}

相关文章