简体   繁体   English

在Matlab中读取文件夹上的多个图像

[英]Read multiple images on a folder in Matlab

I have a problem reading multiple images in Matlab from a folder. 我在从一个文件夹中读取Matlab中的多个图像时遇到问题。 I want to read with their original name (with the command imread because are multiband). 我想用他们的原始名称阅读(使用命令imread因为是多频段)。 The names of the images are like '2001_01', '2001_02'. 图像的名称类似于“2001_01”,“2001_02”。 This is my code: 这是我的代码:

myPath= 'C:\images\'; %'
a=dir(fullfile(myPath,'*.tif'));
fileNames={a.name};

And then... 然后...

for k = 1:length(fileNames)
    filename = [fileNames(k).name];  
    I = imread(filename);
end

But it doesn't work and I don't know how to save and imread each one individually. 但它不起作用,我不知道如何单独保存和imread每一个。 Does somebody know how can I do it? 有人知道我该怎么办? Really thanks in advance, 非常感谢提前,

  1. Regarding the first problem: 关于第一个问题:

    But it doesn't work... 但它不起作用......

    Just assign the output of dir directly into fileNames (without brackets): 只需将dir的输出直接分配到fileNames (不带括号):

     fileNames = dir(fullfile(myPath, '*.tif')); 
  2. Regarding the second problem: 关于第二个问题:

    ... I don't know how to save and imread each one individually. ......我不知道如何保存和单独imread每一个。

    it seems that you need a cell array to store all images in a single collection. 您似乎需要一个单元格数组来存储单个集合中的所有图像。 First, define the cell array to have the right size: 首先,将单元格数组定义为具有正确的大小:

     C = cell(length(fileNames), 1); 

    and then store each image into a different cell: 然后将每个图像存储到不同的单元格中:

     for k = 1:length(fileNames) filename = fileNames(k).name; C{k} = imread(filename); end 

    To access any image in the cell array C later, use curly braces ( {} ). 要稍后访问单元格数组C中的任何图像,请使用花括号( {} )。 For instance, the second image is accessed as follows: C{2} . 例如,第二个图像的访问方式如下: C{2}

Instead of 代替

 fileNames={a.name};

Try 尝试

fileNames = arrayfun( @(x) fullfile( myPath, x.name ), a, 'UniformOutput', false );

Then, in the loop you can access the k -th file name as 然后,在循环中,您可以访问第k个文件名

I = imread( filenames{k} );

Does it return the full file path? 它是否返回完整的文件路径? fileNames(k).name ? fileNames(k).name? or just the actual file name? 或只是实际的文件名? You might need to append myPath with filename taking care of slashes as well 您可能需要使用带有文件名的myPath附加myoff来处理斜杠

fileName = strcat(myPath, fileName)

Then do the imread, make sure you have looked at the slashes once contactenated 然后做imread,确保你看过斜线一旦接触

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

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