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

13 views (last 30 days)
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)

Accepted Answer

Dave B
Dave B on 18 Oct 2021
Edited: Dave B on 3 Nov 2021
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 Comments
gil
gil on 3 Nov 2021
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
Matt J
Matt J on 3 Nov 2021
This is the answer of MATLAB support team and it works fine.
If so, you should Accept-click the answer.

Sign in to comment.

More Answers (1)

Matt J
Matt J on 19 Oct 2021
Edited: Matt J on 19 Oct 2021
You can fall back to imrect, if necessary
fcn = makeConstrainToRectFcn('imrect',get(gca,'XLim'),get(gca,'YLim'));
h=imrect('PositionConstraintFcn',fcn)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!