필터 지우기
필터 지우기

use imcrop and specify width and height but not starting location?

조회 수: 4 (최근 30일)
r_soo
r_soo 2022년 10월 4일
댓글: r_soo 2022년 10월 9일
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?

채택된 답변

DGM
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개)

Image Analyst
Image Analyst 2022년 10월 4일
Maybe try drawrectangle. Demo attached. Adapt as needed.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by