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).?

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

Answers (1)

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

Dear sir is there any possibility to equalise the number of rows and colums of 2 images?
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.
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
This is for one image
Name Size Bytes Class Attributes
I 128x128x3 49152 uint8
K 1x1 8 double
MD 11x1 1480 cell
MODE 1x1 8 double
i 1x1 8 double
map 0x0 0 double
x 128x128x3 49152 uint8
But for the other image , all other parts are same but the x part is different
x 128x128 16384 uint8 .
So this is the difference. so how to convert an image 128x128x3 to 128x128
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.
The same code i used, i got the array with 128x128. but when we download the image1, and check that image property, it shows 128x128x3
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.
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 Deep Learning Toolbox in Help Center and File Exchange

Asked:

on 9 Oct 2020

Commented:

on 12 Oct 2020

Community Treasure Hunt

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

Start Hunting!