当图片名字有数字规律时,批量处理方式。
①srcImage 图片名字有规律
②将srcImage文件下的图片,转换为灰度图并保存入grayImage文件夹。
③
1 #include <iostream> 2 #include <opencv2/opencv.hpp> 3 #include <string> 4 using namespace cv; 5 using namespace std; 6 7 int main() 8 { 9 string fileName, grayFile; 10 11 for(int i = 101; i <= 150; i++) 12 for(int j = 0; j <= 23; j++) 13 { 14 //int 转换为 string 15 stringstream ss1,ss2; 16 string str1, str2; 17 ss1 << i; 18 ss1 >> str1; 19 ss2 << j; 20 ss2 >> str2; 21 22 fileName = "srcImage/Tester_" + str1 + "TrainingPosepose_" + str2 + ".jpg"; 23 grayFile = "grayImage/Gray_Tester_" + str1 + "TrainingPosepose_" + str2 + ".jpg"; 24 //cout << fileName << endl; 25 26 Mat srcImage = imread(fileName), grayImage; 27 cvtColor(srcImage,grayImage,CV_BGR2GRAY); 28 29 imwrite( grayFile, grayImage); 30 } 31 system("pause"); 32 return 0; 33 }
④完成