试试这个 -
image = imread('my_image.jpg');
desired_height = 140;
%%// Width of the resized image keeping the aspect ratio same as before
n2 = round((size(image,2)/size(image,1))*desired_height);
%%// Resized image
image_resized = imresize(image, [desired_height n2]);
编辑1
注:或者您可以使用通过使用NaN的imresize
规定的夏嘉曦的解决方案的建议太大小,但它ceils或四舍五入在大多数情况下你可能不需要的尺寸。
为了证明这种情况,我试着imresize
将高度保持为173,我用手动调整大小获得了不同的尺寸,而不是当我让imresize
决定尺寸。用于实验
%%// Resized image
image_resized_with_auto_sizing = imresize(image, [desired_height NaN]);
image_resized_with_manual_sizing = imresize(image, [desired_height n2]);
大小输出用于我的实验
代码 -
>> whos image_resized_with_manual_sizing image_resized_with_auto_sizing
Name Size Bytes Class Attributes
image_resized_with_auto_sizing 173x185x3 96015 uint8
image_resized_with_manual_sizing 173x184x3 95496 uint8
通告两种情况中的宽度的差值。这个问题也被讨论了here。
为什么不让'imresize'自己找出'n2'? – Shai
@Shai查看编辑1,为什么在让'imresize'决定尺寸之前,可能会三思而后行。 – Divakar
好的,在这种情况下,您的宽度等于184而不是185和95496字节,而不是96015.这是否与自动调整大小相比具有优势?所以你的方法更有效率? – Christina