OpenCV在连续多张图像上截取指定区域

下面的例子可以实现在连续多张图像上截取并保存图像的指定区域
#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)

                                                       

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值