【问题标题】:Resizing image in specific case在特定情况下调整图像大小
【发布时间】:2014-03-26 17:59:33
【问题描述】:

我有一个大小为260 x 260 像素的图像。例如,我知道如何将其调整为140 x 140 像素,然后将其转换为灰度。让我们假设下面的matlab代码:

image = imread('my_image.jpg');
image_resized = imresize(image, [140 140]);
size(image_resized) % 140 x 140 x 3
image_gray = rgb2gray(image_resized);
size(image_gray) % 140 x 140

我想要的是一个具体的案例。我有兴趣将图像标准化为 140 像素高度,其中 相应地重新调整宽度,以便保留图像纵横比。不幸的是,我不知道如何编辑上面的代码。

任何帮助将不胜感激。

【问题讨论】:

    标签: image-processing matlab


    【解决方案1】:

    您可以使用 NaN 是所需的大小参数来准确指示您想要什么

    image_resized = imresize( image, [140 NaN] );
    

    这基本上告诉 Matlab “不要打扰我图像的宽度 - 自己弄清楚!”。

    有关详细信息,请参阅 imresize 文档。

    【讨论】:

    • 我试过你的代码和 Divakar 的代码,我得到了同样的结果。这两个代码是否相似但您的代码简化了很多?
    • @Christina 查看我的编辑 1,关于 imresize 自行决定尺寸的一些问题。
    • @Christina 如果您对如何计算另一个维度(rounded 或 ceiled...)不太挑剔,那么我的回答是 Divakar 的简化版本建议的。恕我直言,使用简化版本使代码更简洁,更易于理解和维护。
    【解决方案2】:

    试试这个 -

    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

    注意: 或者,您也可以按照 Shai 的解决方案所建议的使用 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 查看编辑 1,为什么在让 imresize 决定尺寸之前要三思而后行。
    • 我做了n2 = round((size(image,2)/size(image,1))*desired_height);。如果我没有round 的值,我会得到n2 作为184.0819。因此,图像必须是173x184.0819。由于宽度不能是十进制数,所以我们需要选择一个整数。将宽度四舍五入到184 是有意义的,而不是像使用NaN 作为大小参数时所做的那样,直到185。在此处阅读更多信息 - stackoverflow.com/questions/22453444/…
    • @Christina 尝试使用不是方形的图像,即不是260x260 并尝试调整大小以将高度保持在像173 这样的数字。然后,根据纵横比手动计算宽度必须是多少,这样就清楚了。
    • @Christina 没错!再多一行:)
    猜你喜欢
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多