drawrectangle draws rectangle even though the mouse is clicked outside the parent UIAxes

18 次查看(过去 30 天)
I have a small app designed using the App Designer. Matlab 2021b
The UI is constructed of Figure->UIAxes only and the UIAxes is much smaller from the Figure.
This is the code of thestartup function.
startupFcn(app)
imshow('peppers.png', 'Parent', app.UIAxes);
h = drawrectangle(app.UIAxes)
end
There are two issues:
  1. While clicking the mouse outside the UIAxes and dragging , the app draws a line (i.e. rectangle with no width) on the UIAxes but outside the image. See attached.
  2. The rectangle can be drawn in the UIAxes but outside the image.See attached.
While using imrect(app.UIAxes) instead, the rectangle can be drawn on the image only. Mouse events outside the image are ignored.
How can I make the drawrectangle work as the imrect? (creating rectangle by clicking on the image only)

采纳的回答

Dave B
Dave B 2021-10-18
编辑:Dave B 2021-11-3
Edit: This answer from MATLAB support is pasted from one of the comments below. I'm editing my initial answer to include it so that others can find it more easily:
This is the answer of MATLAB support team and it works fine.
using images.roi.Rectangle instead of drawrectangle.
function startupFcn(app)
app.UIAxes.Visible = 'on';
im = imshow('peppers.png', 'Parent', app.UIAxes);
axis(app.UIAxes, 'tight');
% Assign the ROI creating function as the callback for any button press on image object
im.ButtonDownFcn = @createROI;
function createROI(~,~)
% Create ROI object
roi = images.roi.Rectangle(app.UIAxes);
% Define an ROI start point within the image
beginDrawingFromPoint(roi,[0,0])
end
end
---- Original answer follows ----
You can pass in a DrawingArea to drawrectangle which will constrain the rectangle to the image. That keeps the drawn rectangles inside the image boundaries, although the crosshair will apear when the mouse hovers outside of the image...clicking there will initiate a rectangle inside the image:
im = imread('peppers.png')
imshow(im, 'Parent', app.UIAxes);
h = drawrectangle(app.UIAxes, 'DrawingArea', [0,0,width(im),height(im)])
Alternatively, you could use axis tight to make the axis (including the invisible part) correspond directly to the boundaries of the image.
imshow('peppers.png', 'Parent', app.UIAxes);
axis(app.UIAxes,'tight')
h = drawrectangle(app.UIAxes)
  10 个评论
gil
gil 2021-11-3
This is the answer of MATLAB support team and it works fine.
using images.roi.Rectangle instead of drawrectangle.
function startupFcn(app)
app.UIAxes.Visible = 'on';
im = imshow('peppers.png', 'Parent', app.UIAxes);
axis(app.UIAxes, 'tight');
% Assign the ROI creating function as the callback for any button press on image object
im.ButtonDownFcn = @createROI;
function createROI(~,~)
% Create ROI object
roi = images.roi.Rectangle(app.UIAxes);
% Define an ROI start point within the image
beginDrawingFromPoint(roi,[0,0])
end
end

请先登录,再进行评论。

更多回答(1 个)

Matt J
Matt J 2021-10-19
编辑:Matt J 2021-10-19
You can fall back to imrect, if necessary
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
h=imrect('PositionConstraintFcn',fcn)

标签

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by