use imcrop and specify width and height but not starting location?
8 次查看(过去 30 天)
显示 更早的评论
Is there a way to create a specified size rectangle on an image without a set starting point? So I could move the ROI around on the image and save that chunk but without being able to click an drage to change the size of the rectangle?
0 个评论
采纳的回答
DGM
2022-10-5
编辑:DGM
2022-10-5
This is an attempt to constrain the size of the ROI object during user interaction.
% say you have an image
inpict = imread('peppers.png');
% and you know the box geometry
cropsz = [200 100]; % [x y] (pixels)
% start by displaying the image
figure(1)
imshow(inpict);
% create an ROI object with the specified size
% at some default location (e.g. the image origin)
myROI = drawrectangle(gca,'position',[0 0 cropsz]);
addlistener(myROI,'MovingROI',@(a,b) forcesize(myROI,cropsz));
% the ROI object can be manually dragged around the image
% attempts to resize the object will be prevented
% a simple means to wait for the user
input('Press enter when you''re done moving the ROI object.','s');
% crop using the offsets from the ROI and the specified size
outpict = imcrop(inpict,[myROI.Position(1:2) cropsz]);
% show the result
figure(2)
imshow(outpict)
function forcesize(hObject,fixedsz)
hObject.Position(3:4) = fixedsz;
end
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!