截取图片和压缩图片(Mat格式)
参考文献:
1.resize()的用法: https://blog.csdn.net/i_chaoren/article/details/545646632.截取图片:Rect area(550, 200, 680, 680); //Rect area [x, y, width, heigth];(x, y) 为裁剪区域的起始点坐标; (width, heigth) 为所裁剪区域的尺寸信息
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<opencv2/opencv.hpp>
#include<string>
//输出图片是是224x224像素的PNG格式
//对于IplImage * img 类型的图像,cvSaveImage(ImgName3, out)无法保存,本程序转为mat格式保存
using namespace cv;
using namespace std;
void PictureResize();
void PictureResize1();
int main() {
//PictureResize();
PictureResize1();
return 0;
}
void PictureResize1() {
for (int i = 1; i < 13; i++) {
string ImgName, ImgName2;
ImgName = "D:\\Master_project_TJH\\test_wzy\\ImageExample\\" + to_string(i) + ".bmp";
Mat img = imread(ImgName);
cout << "读取成功." << endl;
//截取图片
Rect area(550, 200, 680, 680);//Rect area [x, y, width, heigth];(x, y) 为裁剪区域的起始点坐标; (width, heigth) 为所裁剪区域的尺寸信息
Mat img_region = img(area);
//压缩图片
Mat img_final;
resize(img_region, img_final, Size(224, 224), 0, 0, INTER_LINEAR);
ImgName2 = "D:\\Master_project_TJH\\test_wzy\\ImageExample\\" + to_string(i) + ".png";
imwrite(ImgName2, img_final);
}
}