ImageFormat.cpp
本程序是处理摄像头输入的bmp图像,输出图片是是224x224像素的PNG格式
处理后可以成功被神经网络调用了
压缩到指定像素的图片的办法
cvCreateImage()和cvResize();
参考链接:cvResize() 函数实现:https://www.cnblogs.com/scut-fm/p/3746614.html
遇到的问题:
1.对于IplImage * img 类型的图像,cvSaveImage(ImgName3, out)无法保存,本程序转为mat格式保存(具体原理仍然不懂)
参考链接为:https://blog.csdn.net/omodao1/article/details/80833385
遗留的问题:
1.还没有处理到灰度和二值图
2.处理后的图片比例畸变,应该还要先裁剪关键区域再压缩
代码
#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() {
cout << "正在调用OpenCv压缩图像..." << endl;
for (int i = 1; i < 13; i++) {
string ImgName, ImgName2;
ImgName = "D:\\Master_project_TJH\\test_wzy\\ImageExample\\" + to_string(i) + ".bmp";
const char* ImgName1 = ImgName.data();
IplImage * img = cvLoadImage(ImgName1);
// 创建一个新的图像内存块,以存放调整后的结果。
IplImage * out = cvCreateImage(
cvSize(224, 224),
img->depth,
img->nChannels
);
// 图像尺寸调整函数。
cvResize(img,out);
ImgName2 = "D:\\Master_project_TJH\\test_wzy\\ImageExample\\" + to_string(i) + ".png";
const char* ImgName3 = ImgName2.data();
//cvSaveImage(ImgName3, out);//报错
Mat src;
src = cvarrToMat(out);
imwrite(ImgName2, src);
cout << "压缩照片成功" << endl;
waitKey(0);
cvReleaseImage(&img);
cvReleaseImage(&out);
}
}