如何处理一系列图像?

时间:2015-10-12 18:07:19

标签: image matlab image-processing

我正在尝试创建一系列图像数据。但是,当我运行下面的代码时,它不显示任何错误,但是当我查看我创建的图像数据时,只存储我正在引用的文件夹中的图像序列中的最后一个图像。

当我验证此图像数据的变量详细信息时,它仅显示最后一个图像。共有8幅图像,仅显示最后一幅或第8幅图像。我做错了什么?

clc; clear all; close all;
% Create an image filename, and read it in to a variable called manosData.

for k= 1:8
    jpgFileName = strcat('image', num2str(k),'.jpg');

    if exist(jpgFileName, 'file')
        manosData = imread(jpgFileName);
    else
        fprintf('File %s does not exist.\n', jpgFileName);
    end    
end

%%%and then save manosData

1 个答案:

答案 0 :(得分:1)

那是因为你的循环只保存最后一张图像。 manosData只会记住您读入的最后一张图片,因为它会一直被覆盖。我不知道您的图像是彩色还是灰度,我不知道每个图像是否具有相同的分辨率,因此我建议您使用单元格数组来处理图像中的图像:

manosData = {}; %// Cell array
for k= 1:8    
    jpgFileName = strcat('image', num2str(k),'.jpg');

    if exist(jpgFileName, 'file') 
        manosData = [manosData imread(jpgFileName)]; %// Add image to cell array if possible    
    else
        fprintf('File %s does not exist.\n', jpgFileName);
    end    
end

%%%and then save manosData

然后,您可以manosData{k}访问任何图片,其中k是图片索引。例如,如果要显示第四张图像,则需要imshow(manosData{4});