#include <opencv2/opencv.hpp>
#include <cstdio>
#include <cstdlib>
#include <Windows.h>
#include <iostream>
#include <fstream>
typedef struct location
{
int x;
int y;
};
int main()
{
// 载入图像
char pstrImageName[100];
char pstrlocationName[100];
for (int fileinput = 1; fileinput <= 139; fileinput++)
{
sprintf(pstrImageName, "E:\\VS项目管理\\generateFace\\FrontScenedistribute\\images\\%d.bmp", fileinput);//debug 路径名字写不对则图片不输出。
sprintf(pstrlocationName, "E:\\VS项目管理\\generateFace\\FrontScenedistribute\\images\\annotations\\%d.txt", fileinput);
IplImage *pSrcImage = cvLoadImage(pstrImageName, CV_LOAD_IMAGE_UNCHANGED);
ifstream in(pstrlocationName);
if (pSrcImage && in)
{
char str[255];
int filename = 0;
while (in.getline(str, 255)){//输入一行;回车即停止
stringstream ss(str);
location r;
ss >> r.x;
ss >> r.y;
int width = 50;
int height = 60;
CvRect cvRect;
cvRect.x = max(r.x - width / 2, 0);
cvRect.y = max(r.y - width / 2, 0);
cvRect.width = min(r.x + width / 2, pSrcImage->width) - cvRect.x;
cvRect.height = min(r.y + height / 2, pSrcImage->height) - cvRect.y;
width = cvRect.width;
height = cvRect.height;
CvSize size = cvSize(width, height);//区域大小
IplImage* pDest = cvCreateImage(size, pSrcImage->depth, pSrcImage->nChannels);//创建目标图像
cvSetImageROI(pSrcImage, cvRect);//设置源图像ROI
cvResize(pSrcImage, pDest);//缩放img图像,并将数据拷贝到img1 cvResize(src, dst, CV_INTER_LINEAR); //缩放源图像到目标图像
//cvCopy(pSrcImage, pDest); //复制图像
cvResetImageROI(pSrcImage);//这句是必须的,在img1的ROI区域显示img
char str[200] = { 0 };
/*if (filename == 0)
{
IplImage *exist = NULL;
do{
filename++;
sprintf(str, "E:\\VS项目管理\\generateFace\\FrontScenedistribute\\images\\positive\\%d_%d_positive.bmp", fileinput,filename);
exist = cvLoadImage(str, CV_LOAD_IMAGE_UNCHANGED);
} while (exist);
cvReleaseImage(&exist);
cvSaveImage(str, pDest);//保存目标图像
}
else*/
{
filename++;
sprintf(str, "E:\\VS项目管理\\generateFace\\FrontScenedistribute\\images\\positive\\%d_%d_positive.bmp", fileinput, filename);
cvSaveImage(str, pDest);//保存目标图像
}
cvReleaseImage(&pDest);
}
cvReleaseImage(&pSrcImage);
}
}
return 0;
}