简体   繁体   English

如何在MATLAB中将多个图像读取到数组中?

[英]How do I read multiple images into an array in MATLAB?

I am currently working on a PCA face recognition project and I am wondering how do I read multiple images into a matrix and then resize them to say 50x50. 我目前正在从事PCA人脸识别项目,我想知道如何将多个图像读入矩阵,然后将其调整为50x50的大小。 Im aware that I need to use Imread and pass in the images, followed by using imresize. 我知道我需要使用Imread并传递图像,然后再使用imresize。 Would it be something like the following? 会是下面这样吗?

myFolder = 'C:\Users\X';
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for k = 1:length(jpegFiles)
baseFileName = jpegFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray50x50 = imread(fullFileName);
imageArray50x50New = imresize(imageArray50x50, [50 50]);
imshow(imageArray30x40New) 

Is this a good approach? 这是一个好方法吗? how would I resize the images correctly? 如何正确调整图像尺寸?

Thanks in advance, Mark 预先感谢,马克

From what I have dealt with, the only way to read in multiple images from file is to do it serially and through a for loop. 根据我的处理,从文件中读取多个图像的唯一方法是依次并通过for循环进行操作。 What you have currently is indeed a good approach, but you need to determine how you want to store all of these images in MATLAB. 当前所拥有的确实是一种很好的方法,但是您需要确定如何在MATLAB中存储所有这些图像。 The two easiest options would be to create a 3D matrix where each slice is a 50 x 50 image you read from file or a cell array where each cell is a 50 x 50 image. 两种最简单的选择是创建一个3D矩阵,其中每个切片是您从文件中读取的50 x 50图像,或者是一个单元格数组,其中每个单元格是50 x 50图像。

If you want to do the first option, you would do something like this: 如果要选择第一个选项,则应执行以下操作:

%// Your code
myFolder = 'C:\Users\X';
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);

%// New - 3D matrix to store images
imageMatrix = uint8(zeros(50,50,numel(jpegFiles)));

%// Your code
for k = 1:length(jpegFiles)
    baseFileName = jpegFiles(k).name;
    fullFileName = fullfile(myFolder, baseFileName);
    fprintf(1, 'Now reading %s\n', fullFileName);
    imageArray50x50 = imread(fullFileName);
    imageArray50x50New = imresize(imageArray50x50, [50 50]);

    %// New
    imageMatrix(:,:,k) = imageArray50x50New;
end

To access the k th image, you would do: 要访问 k 图像,你会怎么做:

img = imageMatrix(:,:,k);

The above code is assuming that all of your images are of type uint8 . 上面的代码假设您所有的图像均为uint8类型。 If this isn't the case where your images are of different types, a cell array approach would be preferred.... so that'd be the second approach. 如果不是您的图像是不同类型的情况,则首选单元阵列方法.....所以这是第二种方法。 If this is the case, then do this instead: 如果是这种情况,请改为执行以下操作:

%// Your code
myFolder = 'C:\Users\X';
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);

%// New - 3D matrix to store images
imageMatrix = cell(1,numel(jpegFiles));

%// Your code
for k = 1:length(jpegFiles)
    baseFileName = jpegFiles(k).name;
    fullFileName = fullfile(myFolder, baseFileName);
    fprintf(1, 'Now reading %s\n', fullFileName);
    imageArray50x50 = imread(fullFileName);
    imageArray50x50New = imresize(imageArray50x50, [50 50]);

    %// New
    imageMatrix{k} = imageArray50x50New;
end

To access the k th image, you would do: 要访问 k 图像,你会怎么做:

img = imageMatrix{k};

However, if you are dealing with PCA, then what I suggest you do instead is create a 2D matrix where each row is the unrolled version of the image and you would have as many rows as you have images. 但是,如果要处理PCA,则建议您创建一个2D矩阵 ,其中每一都是图像的展开版本,并且您将拥有与图像一样多的行。 Therefore, each row would be a 1 x 250 vector of intensities. 因此,每一行都是强度的1 x 250向量。 The reason why you'd want it like this is because if you were to use the pca function in MATLAB, each row is a data point while each column is a variable. 之所以想要这样,是因为如果要在MATLAB中使用pca函数,则每一都是一个数据点,而每一都是一个变量。 Therefore, you would do this instead: 因此,您可以改为:

%// Your code
myFolder = 'C:\Users\X';
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);

%// New - 3D matrix to store images
imageMatrix = zeros(numel(jpegFiles), 250);

%// Your code
for k = 1:length(jpegFiles)
    baseFileName = jpegFiles(k).name;
    fullFileName = fullfile(myFolder, baseFileName);
    fprintf(1, 'Now reading %s\n', fullFileName);
    imageArray50x50 = imread(fullFileName);
    imageArray50x50New = imresize(imageArray50x50, [50 50]);

    %// New
    imageMatrix(k,:) = double(imageArray50x50New(:).');
end

Therefore, each row would be an image represented as a single vector. 因此,每一行都是代表单个矢量的图像。 This statement: imageArray50x50New(:).' 该语句: imageArray50x50New(:).' first converts the 50 x 50 image into a column vector, then it is transposed so that it becomes a row vector. 首先将50 x 50的图像转换为列向量,然后对其进行转置,使其成为行向量。 Also, take notice that I made the image matrix double precision. 另外,请注意,我将图像矩阵的精度提高了两倍 I did this because pca is best suited for floating-point data, and so when I transformed each image into a row vector, I've casted the data to double to facilitate this. 之所以这样做,是因为pca最适合浮点数据,因此,当我将每张图像转换为行向量时,我已将数据转换为double以便于此。

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

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