crop image with fixed x/y ratio

有些话、适合烂在心里 提交于 2019-12-11 02:55:24

问题


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)

回答1:


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);

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).



来源:https://stackoverflow.com/questions/16633554/crop-image-with-fixed-x-y-ratio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!