简体   繁体   English

变量作为 Matlab 中的 imread 参数?

[英]Variable as argument for imread in Matlab?

I am trying to run an image analysis script on ~5,000 files in Matlab.我正在尝试对 Matlab 中的约 5,000 个文件运行图像分析脚本。 I'm trying to run the main part of the script inside a for loop and iterate across every file name.我正在尝试在 for 循环中运行脚本的主要部分并遍历每个文件名。 I've listed the directory as aa variable and have come up with something like the following:我已将目录列为 aa 变量,并提出了如下内容:

images = dir
images.name
imagesdim = size(images)
imageslength = imagesdim(1)

for i = 1:imageslength
    cimg =  imread(images(i,1).name);
    etc etc
end

However, this doesn't seem to be an acceptable input argument for imread.但是,这似乎不是 imread 可接受的输入参数。 Is there any way I can format this list so that I can use a variable here, or will I have to copy this argument 5,000 times?有什么办法可以格式化这个列表,这样我就可以在这里使用一个变量,还是我必须复制这个参数 5000 次?

I do this in directories of images using the function form of the dir command.我使用dir命令的 function 形式在图像目录中执行此操作。 For example, if you are working with TIFF files:例如,如果您正在使用 TIFF 文件:

dir_items = dir('*.tiff');
file_names = {dir_items.name};
disp('Using .tiff files found in current directory:')
disp(file_names');
for k = 1:length(file_names)
    disp(file_names{k})
    cimg = imread(file_names{k});
end

If you can't use the wildcard filter like "*.tiff", then you have to check the isdir field of each struct in the array, like so:如果不能使用“*.tiff”之类的通配符过滤器,则必须检查数组中每个结构的 isdir 字段,如下所示:

dir_items = dir;
for k = 1:length(dir_items)
    if dir_items(k).isdir==1
        fprintf(1,'%s is a directory (ignore)\n',dir_items(k).name)
    else
        disp(dir_items(k).name)
        cimg = imread(dir_items(k).name)
    end
end

A much easier way of iterating through a folder, or nested folder of images in MATLAB is to use imageDatastore.在 MATLAB 中遍历文件夹或图像嵌套文件夹的一种更简单的方法是使用 imageDatastore。

imds = imageDatastore(desiredDirectory,"FileExtensions",[".tif",".tiff"]);

while hasdata(imds)
     img = read(imds);
     % Your code that operates on img
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM