# how to compare image size

6 views (last 30 days)
Tarik Razak on 5 Feb 2012
Answered: yanqi liu on 15 Oct 2021
hi i am trying to figure out if image is below certain size i.e.
k= want to know size of the image
if (k >= [10 10])
meaning if current image is greater than 10x10 pixel
want to get this small code right.

yanqi liu on 15 Oct 2021
% k= want to know size of the image
k = size(i);
if (k(1) >= 10 && k(2)>=10)
disp('size of image greater than 10 10');
end
size of image greater than 10 10

Walter Roberson on 5 Feb 2012
tk = size(i);
k = tk(1:2);
But please consider what you want to do if (say) the image is 8 x 15. Do you want to test that both dimensions are at least 10, or only that at least one dimension is at least 10, or that the "area" of the image is at least 100 ?

Image Analyst on 6 Feb 2012
[rows columns numberOfColorChannels] = size(imageArray)
% Check both the number of rows AND the number of columns...
if rows >= 10 && columns >= 10
% Do stuff.
else
% Bail out.
end
% OR check the number of pixels...
if rows * columns >= 100
% Do stuff.
else
% Bail out.
end

Josep Llobet on 14 Oct 2021
Excuse me because the coments are in catalan, but matbe the next code could be useful. It is for adjust one image 1 to other smaller than this one, without any of the width or height of the image 2 would be bigger than the image 1.
You can use the next variables to probe the function
% Variables proba
imshow(imatge_resize_1)
size(imatge_resize_1)
imshow(imatge_resize_2)
size(imatge_resize_2)
% function [ratio_resize] = resize_im1_im2petita(imatge_resize_1, imatge_resize_2)
% If the second image is smaller in all their borders, return the resize
% ratio (used in the function imresize) that must have the second image in
% order to adjust their borders to the size of the image 1 ones.
% INICI FUNCIO
% Imatge 1 (en teoria la més gran)
imshow(imatge_resize_1)
size_im1 = size(imatge_resize_1);
size_im1 = size_im1(1:2);
% Imatge 2 (en teoria més petita)
imshow(imatge_resize_2)
size_im2 = size(imatge_resize_2);
size_im2 = size_im2(1:2);
% Si la imatge 1 és més gran que la imatge 2 en alçada o amplada:
if size_im1(1) > size_im2(1) && size_im1(2) > size_im2(2)
% Diferenciem per la posicio en que la diferencia es menor, donat que
% al fer variar aquesta mai en excedirem de les proporcións máximes.
ratio_resize = (dif_amplada + size_im2(2) - 2) / size_im2(2);
% Si l'alçada és major, o bé si les diferencies són iguals:
else
ratio_resize = (dif_alsada + size_im2(1) - 2) / size_im2(1);
end
end
% Finalment, fem resize de la imatge, i comparem, i comprovem, les mesures:
%im_resized_re = imresize(imatge_resize_2, ratio_resize);
%imshow(im_resized_re)
%size(im_resized_re)
% FINAL FUNCIO
The return of the function is then the ratio_resize variable.
You can probe it by this way:
im_resized_re = imresize(imatge_resize_2, ratio_resize);
imshow(im_resized_re)
size(im_resized_re)
Image of the examples:
then applying imresize()
Where the answers are the use of imsize()