在图像处理中,经常会遇到批量处理图像的情况,下面介绍两种批量处理图像的方法:
第一种是,将图像文件按照特定的要求命名,比如focus1.bmp, focus2.bmp……,下面批量灰度化图像,代码如下:
#include <iostream>
#include "opencv2/opencv.hpp"
using namespace std;
int main()
{
int image_id = 1;
char filename[256];
char filename2[256];
char winname[256];
cv::Mat image;
sprintf(filename , "C:\\Users\\Administrator\\Desktop\\Foucsing\\FouceTestResult\\result\\foucs%d.png" , image_id );
cv::Mat src = cv::imread(filename);
cout<<"\n 开始处理! \n";
while(!src.empty())
{
sprintf(filename2 , "C:\\Users\\Administrator\\Desktop\\Foucsing\\FouceTestResult\\focusimage\\foucs%d.bmp" , image_id );
image_id++;
sprintf(filename , "C:\\Users\\Administrator\\Desktop\\Foucsing\\FouceTestResult\\grayimage\\foucs%d.png" , image_id );
src= cv::imread(filename);
cv::Mat gray;
cv::cvtColor(src, gray,CV_RGB2GRAY);
cv::imwrite(filename2, gray);
}
cout<<"\n 处理完毕! \n";
//system("PAUSE");
}