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

14 ビュー (過去 30 日間)
gil
gil 2021 年 10 月 18 日
編集済み: Dave B 2021 年 11 月 3 日
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
Matt J
Matt J 2021 年 11 月 3 日
This is the answer of MATLAB support team and it works fine.
If so, you should Accept-click the answer.

サインインしてコメントする。

その他の回答 (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