Simplicity
Simplicity

Reputation: 48916

Resizing an image and preserving the aspect ratio

In the imresize documentation here, it mentions the following:

B = imresize(A, [numrows numcols]) returns image B that has the number of rows and columns specified by [numrows numcols]. Either numrows or numcols may be NaN, in which case imresize computes the number of rows or columns automatically to preserve the image aspect ratio.

In the following part:

Either numrows or numcols may be NaN, in which case imresize computes the number of rows or columns automatically to preserve the image aspect ratio

Does the NaN here come after resizing the image, or does it refer to not determining numrows or numcols?

So, if I have an image of size 365x147 and want to resize it to 512x512, would the aspect ratio be preserved having assigned numrows and numcols the value 512?

Thanks.

Upvotes: 2

Views: 17076

Answers (2)

Buck Thorn
Buck Thorn

Reputation: 5073

The answer is no. Preserving the aspect ratio means retaining the ratio of physical dimensions in pixels. For instance, for an image of size 365x147, the aspect ratio length(y):length(x) is 365:147. To retain that aspect ratio, a resized image has to satisfy length(y):length(x) = length(y)/length(x):1.

If for instance you have an image of size 365x147 and want to resize it to 512x512, you would alter the aspect ratio from 365:147 to 512:512, or in other words, from 2.48:1 to 1:1.

On the other hand, if you execute

b = imresize(a,512/147);

where a is of size 365 x 147, you would retain the same aspect ratio (to within a rounding error) and generate an image of size 1271 x 512. If you execute

b = imresize(a,512/365);

you retain the same aspect ratio but generate an image of size 512 x 206.

Upvotes: 3

Milan
Milan

Reputation: 492

If you have an image A of size 365x147 and want to resize it to 512x512, use

B = imresize(A, [512 512]);

If you want e.g. the height to be 512, but you want to preserve the ratio, use

B = imresize(A, [512 NaN]);

Upvotes: 2

Related Questions