Batch conversion of grayscale images in opencv and save

When the image name has a numerical pattern, batch processing.

①srcImage image names are regular

 

 

② Convert the picture under the srcImage file to a grayscale image and save it into the grayImage folder.

 

copy code
 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 }
copy code

 

④Complete

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325807108&siteId=291194637