具有固定x / y比率的裁剪图像 [英] crop image with fixed x/y ratio

查看:203
本文介绍了具有固定x / y比率的裁剪图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to crop image using following code. but i want to user only can select the crop area with a predefined x/y ratio.for example if x=2,y=2 ,then user can only use mouse to select an area with (x/y)=1 ratio.

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


推荐答案

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

You could use imrect to produce the coordinates, and then pass those into 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]适合您图像的尺寸/宽高比。您可能希望添加其他约束以进行修改(例如,阻止用户将矩形移动到实际图像之外)。

In the actual code, you'll have to replace [10 10 100 100] with something of the appropriate size/aspect ratio for your images. You may want to add other constraints to imrect (for example to stop the user moving the rectangle outside the actual image).

这篇关于具有固定x / y比率的裁剪图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆