Search code examples
matlabimage-processingcrop

Cropping images in matlab for loop


I am trying to cut some images in a matlab for-loop. However, I am facing some problems in order to do so. My code is the above:

clear
clc

dir
dir 'D:\christos\DATABASE\animal';
listing = dir('D:\christos\DATABASE\animal');

[m n] =  size(listing);

for indx=3:m
 listing(indx).name
 A = imread(strcat( 'D:\christos\DATABASE\animal\' , listing(indx).name)); 
 [t1 t2] = size(A); imshow(A);
 A = imcrop(A, [ (1/3)*t1 (1/3)*t2  (2/3)*t1 (2/3)*t2 ] );
 %A =  imresize(A, [360, 280]);
 imwrite(A, strcat('D:\christos\DAt\animal\animal_',  int2str(indx-2) , '.jpg'));
end

In some images, it doesnt work proper the imcrop line. Beside the fact that the images is been read and has size t1 t2, I cant cropped it in the way I want and I get as a result an empty A image.


Solution

  • Maybe some are colored images, so use this -

    [t1 t2 t3] = size(A);

    and keep the rest of the code as it is.

    With your original code for colored images, t2 would store - widthx3 and not the width itself.