Clear Filters
Clear Filters

Array dimensions must match for binary array op." How you solve that one? How to check the images have exactly the same row and column sizes, and the same number of dimensions (2 or 3).?

1 view (last 30 days)
How to check the images have exactly the same row and column sizes, and the same number of dimensions (2 or 3).?

Answers (1)

Sudhakar Shinde
Sudhakar Shinde on 9 Oct 2020
To check dimensions you can use:
if ndims(Image1) == ndims(Image2)
disp('dimensions are same');
else
disp('Dimensions are different')
end
To check size i.e. number of rows and colums equality, you can use
[Row1 Column1] = size(Image1);
[Row2 Column2] = size(Image2);
if (Row1==Row2) &(Column1==Column2)
disp('Both images have same size');
else
disp('Image size is different');
end
%Alternatively to check size also can be used:
if size(Image1) == size(Image2)
disp('same size')
else
disp('different size')
end
  9 Comments
Sudhakar Shinde
Sudhakar Shinde on 9 Oct 2020
Can you please elaborate meaning of download? If you re downloading from website or google this may be actual rgb image and hence it shows 128x128x3.
Stephen23
Stephen23 on 12 Oct 2020
This answer is incorrect:
[Row1 Column1] = size(Image1);
[Row2 Column2] = size(Image2);
The size documentation explains that for multiple outputs "When dim is not specified and fewer than ndims(A) output arguments are listed, then all remaining dimension lengths are collapsed into the last argument in the list." This means for a 3D array (very likely with image data) the second of two outputs does NOT give the number of columns. This is very easy to demonstrate:
>> A = rand(4,3,2); % four rows, three columns, two pages
>> [X,Y] = size(A)
X = 4
Y = 6
Is Y the number of columns in A? (hint: no)
The simple solution for images (i.e. known to be either 2D or 3D) is to specify the third output:
>> [R,C,~] = size(A)
R = 4
C = 3

Sign in to comment.

Categories

Find more on Read, Write, and Modify Image in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!