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).?
2 次查看(过去 30 天)
显示 更早的评论
How to check the images have exactly the same row and column sizes, and the same number of dimensions (2 or 3).?
0 个评论
回答(1 个)
Sudhakar Shinde
2020-10-9
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 个评论
Sudhakar Shinde
2020-10-9
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
2020-10-12
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
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!