Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Resizing two images to the same pixel dimensions without aspect ratio distortion

Asked by Simon on 9 Jul 2012

Hi All,

I have two images with the object of focus in the centre of each. I then need to crop around these (basically to save time during my analysis porgram). The problem then becomes the cropped images are different sizes - in terms of pixel value.

I am trying to get transects from the same positions along each object, and if the sizes are different, the transects will be in the wrong place, and if the aspect ratio changes this corrupts the correct data.

I haven't been able to find a way to resize one image to the other without losing the aspect ratio of whichever image that was resized.

To resize the images I used:

[rowsA colsA RGBA]=size(A);
[rowsB colsB RGBB]=size(B);
C=imresize(b,[rowsA colsA]); % i used C so I could compare the resized image to the original.

Thank you all for any help or ideas.



Simon on 12 Jul 2012

That's a good question. The object of focus is a model of a fish. However due to movement of tide, the camera moved slightly between taking pictures. But this shouldn't change the aspect ratio, just the position and size of the object in the image.

Mariappan on 18 Feb 2013

In this above code, what it is "b"???

Walter Roberson on 18 Feb 2013

It should probably read "B"



No products are associated with this question.

4 Answers

Answer by Thorsten on 18 Feb 2013

You have found no way because there is no way. If you resize an image A to match the size of another image B the aspect ratio of the resulting image is the aspect ratio of B.


Answer by Jurgen on 18 Feb 2013

Your question is: Can I change the aspect ratio of A to fit that of B without changing the aspect ratio of A? It makes no sense!

Just fix the size of the cropping area to avoid uneven ARs. Resizing only works if both dimensions of A are integer multiples of B, which im guessing isnt the case here.


Answer by Image Analyst on 18 Feb 2013

I know this is a really old, obsolete thread, but question should read "How can I change the magnification of Image B to match that of image A?" If the camera moved closer or farther away, the image size in pixels will remain the same, say 1600x900 pixels, but the image of the fish will become larger or smaller. Simon has found the fish in each large image, and cropped it out, so he now has two smaller cropped images. A simple resize like Simon proposed should work. For example, a circle cropped out of image A is 100 by 100 pixels. Now let's say we have different magnifications for each direction in the second image and the circle now appears as an ellipse of a different size, 150 by 50. If you resize this 150x50 image to 100x100, it will now appear as a circle like it should.


Image Analyst
Answer by Simon on 19 Feb 2013

Hi All, thank you for your comments. And my apologies for the terrible phrasing of my question. I appreciate your help. In the end, I simplified my problem, just turning total body length into 100%, and then sampling at different percentiles along the bodies of the fish (e.g. distance from nose to tail = 100%, so sample at 10% of this, 20%, 30% etc.), rather than try to make each body size equal in size and AR. But I am going to go back and try the suggestion from Image Analyst, to see if this would have solved my problem. Should i be closing this problem now, or leave it open in case others have ideas and or thoughts? (It's my first and only post so far on the mathworks site)

Thanks again.




Contact us