Search code examples
matlabmatlab-figuredimensions

Matlab imageset method order of read and name of image?


%% Load images from folder

% Use imageSet to manage images stored in multiple folders
imset = imageSet('pet_images','recursive');
% Preallocate arrays with fixed size for prediction
imageSize = cnnModel.net.normalization.imageSize;
trainingImages = zeros([imageSize sum([imset(:).Count])],'single');

% Load and resize images for prediction
for ii = 1:numel(imset)
    for jj = 1:imset(ii).Count
        imshow(read(imset(ii),jj)); 
        trainingImages(:,:,:,jj) = imresize(single(read(imset(ii),jj)),imageSize(1:2));

    end
end

I want to read images from a directory. But it gives me error some images.

Assignment has fewer non-singleton rhs dimensions than non-singleton subscripts
  • I use breakpoint to find which image has a problem. When it gives error, I catch the index of the image and show this image.

  • I find this image from this directory. it's order is 1061 (orders according to name) but value of jj is 1012.

I have three Question.

  1. Why it gives this error? (The resolution of (error) image : (263x380))
  2. In order to find the image, I check with the image and same type cats. In for loop, Can I got the name of this image.?
  3. While imageset read the directory, what according to read it (name,type,date, etc...)? Why the row number (1061) and index(1012) are mismatches?

Solution

    1. if the image is grayscale, it gives error.
      Because rgb image is expected by trainingImages array.
    2. getting the name of image, you can use code below;

      imshow(read(imset(ii),jj));
      a = select(imset(ii),jj);
      str = cellstr(a(1,1).ImageLocation);
      title(str);
      
    3. Because imageset orders alphabetically but different way(name) For example.

      • Directory order : img1, img2, img3,... img9, img10, img11... img20, ...
      • imageSet read order: img1, img2, img3,... img9, img10, img20... img21, ...