【问题标题】:crop image with fixed x/y ratio以固定 x/y 比率裁剪图像
【发布时间】:2013-05-14 01:38:53
【问题描述】:

我想使用以下代码裁剪图像。但我希望用户只能选择具有预定义 x/y 比率的裁剪区域。例如,如果 x=2,y=2,则用户只能使用鼠标选择具有 (x/y)=1 比率的区域。

I = imread('image.jpg');
[rows columns numberOfColorBands] = size(I);
I2 = imcrop(I);
imshow(I), figure, imshow(I2)

【问题讨论】:

    标签: matlab image-processing crop


    【解决方案1】:

    您可以使用 imrect 生成坐标,然后将其传递给 imcrop。

    figure, imshow(I);
    h = imrect(gca,[10 10 100 100]); 
    setFixedAspectRatio(h,1); % this fixes the aspect ratio; user can now change size/position
    position = wait(h); % returns coordinates in "position" when user doubleclicks on rectangle
    I2 = imcrop(I,position);
    figure, imshow(I2);
    

    在实际代码中,您必须将 [10 10 100 100] 替换为适合图像大小/纵横比的内容。您可能希望添加其他约束来 imrect(例如,阻止用户将矩形移到实际图像之外)。

    【讨论】:

    • setFixedAspectRatioMode 是固定比率的正确函数。我花了一段时间才弄清楚出了什么问题
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2014-06-29
    • 1970-01-01
    • 2020-06-28
    相关资源
    最近更新 更多