opencv
- 首先添加必要的头文件(用于批量处理):
#include <dirent.h>
这里可能会报错,找不到头文件,可以去网上下载,或者点击下面的链接
下载
下载后找到dirent.h文件,复制到…(对应你的VS目录)…\VC\include\
- 接下来,批量读取并处理图片:
DIR *dp;
struct dirent *dirp;
vector <std::string> filename;
if ((dp = (opendir(filein.c_str()))) == NULL) {
perror("open dir error");
return -1;
}
while ((dirp = readdir(dp)) != NULL) {
filename.push_back(filein+dirp->d_name);
}
for (int i = 0; i<filename.size(); i++) {
cout << filename[i] << endl;
Mat img = imread(filename[i], CV_LOAD_IMAGE_COLOR); // Read the file
//图像处理
string temp = filename[i];
size_t pos = temp.find("images");//我这里是把文件放在images里边,然后希望把处理后文件存入与images同目录下的results里边
fileout = temp.replace(pos, 6, "results");
imwrite(fileout, img);
}
closedir(dp);
matlab
matlab比较简单,不用添加特定头文件直接上代码就好
Input_file_path = '.\images\';% input image file path
Output_file_path='.\result\';
img_path_list = dir(strcat(Input_file_path,'*.jpg'));
img_num = length(img_path_list);
if img_num > 0
for j = 1:img_num
image_name = img_path_list(j).name;
image_name = strcat(Input_file_path,image_name);
%图像处理过程
imwrite(image,strcat(Output_file_path,image_name));
end
end