下面的例子可以实现在连续多张图像上截取并保存图像的指定区域
#include "stdafx.h"
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/ml/ml.hpp>
using namespace std;
using namespace cv;
#define SamNO 13233 //需处理的图像数量
int _tmain(int argc, _TCHAR* argv[])
{
string ImgName;
string DstName;
int temp=1;
char name[10];
ifstream finPos("faceList.txt");
for(int num=0; num<SamNO && getline(finPos,ImgName); num++)//getline函数从finPos中读取图像名字放到ImgName,遇到“\n”时停止
{
cout<<"第 "<<num<<" 帧"<<endl;
cout<<"处理:"<<ImgName<<endl;
DstName=ImgName;
ImgName = "E:\\DeepLearning\\testImg\\face_all\\" + ImgName;//加上正样本的路径名,
Mat src = imread(ImgName);//读取图片
namedWindow("src",CV_WINDOW_AUTOSIZE); //显示原图
imshow("src",src);
int width = src.cols;
int height = src.rows;
int w = width/2; //指定截取的宽、高
int h = height/2;
Mat dst = src(Rect(0,0,w,h)); //实现截取功能,Rect()的前两个参数为左上角起始点坐标,后连个为宽、高
DstName = "E:\\DeepLearning\\testImg\\partface\\new" + DstName;;
imwrite(DstName,dst); //保存图像
namedWindow("dst",CV_WINDOW_AUTOSIZE); //显示截取的图像
imshow("dst",dst);
cvWaitKey(0);
}
return 0;
}
下面为效果图:src为原图(大小250*250),dst为截取得到的图像(大小125*125)