当图片名字有数字规律时,批量处理方式。

①srcImage 图片名字有规律

opencv之批量转换灰度图并保存-LMLPHP

②将srcImage文件下的图片,转换为灰度图并保存入grayImage文件夹。

opencv之批量转换灰度图并保存-LMLPHP

 #include <iostream>
#include <opencv2/opencv.hpp>
#include <string>
using namespace cv;
using namespace std; int main()
{
string fileName, grayFile; for(int i = ; i <= ; i++)
for(int j = ; j <= ; j++)
{
//int 转换为 string
stringstream ss1,ss2;
string str1, str2;
ss1 << i;
ss1 >> str1;
ss2 << j;
ss2 >> str2; fileName = "srcImage/Tester_" + str1 + "TrainingPosepose_" + str2 + ".jpg";
grayFile = "grayImage/Gray_Tester_" + str1 + "TrainingPosepose_" + str2 + ".jpg";
//cout << fileName << endl; Mat srcImage = imread(fileName), grayImage;
cvtColor(srcImage,grayImage,CV_BGR2GRAY); imwrite( grayFile, grayImage);
}
system("pause");
return ;
}

④完成

opencv之批量转换灰度图并保存-LMLPHP

05-11 14:56