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).?
Show older comments
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
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
RAJESH RAJAN
on 9 Oct 2020
Sudhakar Shinde
on 9 Oct 2020
Edited: Sudhakar Shinde
on 9 Oct 2020
You can use resize function on both images:
%Example resize image to 256 rows and 256 columns
Image=imresize(Image,[256 256]);
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)
This answer will also throw an error if the images have different numbers of dimensions:
size(Image1) == size(Image2)
The robust solution is to use isequal.
Sudhakar Shinde
on 9 Oct 2020
Edited: Sudhakar Shinde
on 9 Oct 2020
Image1 = imread('image1.jpg');
Image2 = imread('image2.jpg');
%Now you can convert both images into 2D using rgb2gray function:
if ndims(Image1) > 2
Image1 = rgb2gray(Image1)
else
Image1=Image1;
end
%
if ndims(Image2) > 2
Image2 = rgb2gray(Image2)
else
Image2=Image2;
end
%Resize image to make same number of rows and columns
Image1 = imresize(Image1,[256 256]);
Image2 = imresize(Image2,[256 256]);
%now check for equality:
if isequal(size(Image1),size(Image2))
disp('same size')
else
disp('different size')
end
RAJESH RAJAN
on 9 Oct 2020
Sudhakar Shinde
on 9 Oct 2020
one way to do this:
Convert first image into 2 D and then resize to 128x128.
if ndims(Image1) > 2
Image1 = rgb2gray(Image1)
else
Image1=Image1;
end
%Now Image1 is a 2D image which is eg. 764x764.
%Then resize into 128x128
Image1 = imresize(Image1,[128 128]);
You can check earlier comment for more details.
RAJESH RAJAN
on 9 Oct 2020
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
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
Categories
Find more on Deep Learning Toolbox 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!