1.opencv
(1)文件名连续情况下
// 功能:代码 2-32 文件名连续情况下
// 作者:朱伟 zhu1988wei@
// 来源:《OpenCV图像处理编程实例》
// 博客:
// 更新:2016-8-1
// 说明:版权所有,引用或摘录请联系作者,并按照上面格式注明出处,谢谢。//
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace cv;
using namespace std;
int main()
{
// 定义相关参数
const int num = 4;
char fileName[50];
char windowName[50];
cv::Mat srcImage;
for (int i = 1; i <= num; i++)
{
// sprintf读入指定路径下图片序列
sprintf_s(fileName, "..\\images\\test\\1 (%d).jpg", i);
sprintf_s(windowName, "NO%d", i);
// 按照图像文件名读取
srcImage = cv::imread(fileName);
if (!srcImage.data)
{
std::cout << "No data!" << std::endl;
return -1;
}
cv::namedWindow(windowName);
cv::imshow(windowName, srcImage);
std::cout << "NO: " << i << std::endl;
//cv::waitKey(0);
/* 该处可以添加处理步骤 */
}
cv::waitKey(0);
return 0;
}
(2)文件名无规则情况读取
// 功能:代码 2-33 文件名无规则情况读取
// 作者:朱伟 zhu1988wei@
// 来源:《OpenCV图像处理编程实例》
// 博客:
// 更新:2016-8-1
// 说明:版权所有,引用或摘录请联系作者,并按照上面格式注明出处,谢谢。//
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
// LPCWSTR转string
std::string WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen <= 0) return std::string("");
char* pszDst = new char[nLen];
if (NULL == pszDst) return std::string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen - 1] = 0;
std::string strTemp(pszDst);
delete[] pszDst;
return strTemp;
}
// 利用winWIN32_FIND_DATA读取文件下的文件名
void readImgNamefromFile(char* fileName, vector <string> &imgNames)
{
// vector清零 参数设置
imgNames.clear();
WIN32_FIND_DATA file;
int i = 0;
char tempFilePath[MAX_PATH + 1];
char tempFileName[50];
// 转换输入文件名
sprintf_s(tempFilePath, "%s/*", fileName);
// 多字节转换
WCHAR wstr[MAX_PATH] = { 0 };
MultiByteToWideChar(CP_ACP, 0, tempFilePath, -1, wstr, sizeof(wstr));
// 查找该文件待操作文件的相关属性读取到WIN32_FIND_DATA
HANDLE handle = FindFirstFile(wstr, &file);
if (handle != INVALID_HANDLE_VALUE)
{
FindNextFile(handle, &file);
FindNextFile(handle, &file);
// 循环遍历得到文件夹的所有文件名
do
{
sprintf(tempFileName, "%s", fileName);
imgNames.push_back(WChar2Ansi(file.cFileName));
imgNames[i].insert(0, tempFileName);
i++;
} while (FindNextFile(handle, &file));
}
FindClose(handle);
}
int main()
{
// 设置读入图像序列文件夹的路径
char* fileName = "..\\images\\test\\";
std::vector <string> imgNames;
// 获取对应文件夹下所有文件名
readImgNamefromFile(fileName, imgNames);
// 遍历对应文件夹下所有文件名
for (int i = 0; i < imgNames.size(); i++)
{
cv::Mat img = cv::imread(imgNames[i]);
if (!img.data)
return -1;
/* 可添加图像处理算法code*/
cv::imshow("im", img);
cv::waitKey(0);
}
return 0;
}
2.matlab
(1)指定路径下 单个文件夹data中所有图像
file_path = '.\data\';% 图像文件夹路径
img_path_list = dir(strcat(file_path,'*.jpg'));%获取该文件夹中所有jpg格式的图像
img_num = length(img_path_list);%获取图像总数量
if img_num > 0 %有满足条件的图像
for j = 1:img_num %逐一读取图像
image_name = img_path_list(j).name;% 图像名
image = imread(strcat(file_path,image_name));
fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 显示正在处理的图像名
%图像处理过程 省略
end
end
注,上述的代码只能读取data文件夹中的图像,假设data中包含子文件夹,不能读取子文件夹中的图像。
(2)指定路径下 多个文件夹中所有图像,该代码可以读取文件夹data中及data的所有子文件夹中的图像。
p = genpath('.\data');% 获得文件夹data下所有子文件的路径,这些路径存在字符串p中,以';'分割
length_p = size(p,2);%字符串p的长度
path = {};%建立一个单元数组,数组的每个单元中包含一个目录
temp = [];
for i = 1:length_p %寻找分割符';',一旦找到,则将路径temp写入path数组中
if p(i) ~= ';'
temp = [temp p(i)];
else
temp = [temp '\']; %在路径的最后加入 '\'
path = [path ; temp];
temp = [];
end
end
clear p length_p temp;
%至此获得data文件夹及其所有子文件夹(及子文件夹的子文件夹)的路径,存于数组path中。
%下面是逐一文件夹中读取图像
file_num = size(path,1);% 子文件夹的个数
for i = 1:file_num
file_path = path{i}; % 图像文件夹路径
img_path_list = dir(strcat(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 = imread(strcat(file_path,image_name));
fprintf('%d %d %s\n',i,j,strcat(file_path,image_name));% 显示正在处理的路径和图像名
%图像处理过程 省略
end
end
end