How do I add a draw rectangle function to an image app?
조회 수: 31 (최근 30일)
이전 댓글 표시
I used App Designer to create a frame containing an image. Now I want to draw a rectangle inside that image. None of the examples I have seen describe how to do this in an object-oriented manner for Matlab 2022.
My main routine contains the following code:
scan = sky_frame; % draw the background .gif image
value = [20 20 30 50]; % define the rectangle coordinates
scan.drawRect(value); % call the drawRect function in sky_frame to draw the rectangle
The sky_frame class is defined as follows:
classdef sky_frame < matlab.apps.appBase
properties(Access = public)
etc.
end
% callbacks
methods(Access = Public)
function drawrect(value)
rectangle('Position',value);
end
end
When I run this, the .gif image appears, but "rectangle" creates a new figure on the desktop, outside my original sky_frame object. How do I force the code to draw the rectangle inside the sky_frame object at the coordinates specified?
댓글 수: 0
채택된 답변
Eric Delgado
2022년 10월 20일
편집: Eric Delgado
2022년 10월 20일
Hey... you have to show your image in an uiaxes and create a handle to your ROI as property of the app, so you can draw your ROI and change it later. See app attached. Hope it's helpful! :)
% Image button callback
function ImageButtonPushed(app, event)
[file, path] = uigetfile({'*.png'; '*.jpg'; '*.jpeg'});
figure(app.UIFigure)
if ~isempty(file)
try
imshow(fullfile(path, file), 'Parent', app.UIAxes)
catch ME
uialert(app.UIFigure, ME.message, 'error', 'Icon', 'error')
end
end
end
% Rectangle button callback
function RectangleButtonPushed(app, event)
app.roi = drawrectangle(app.UIAxes);
end
댓글 수: 12
Eric Delgado
2022년 10월 21일
Great to hear! Now that you are super happy with my support, you accept my answer, ok? :)
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Convert Image Type에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!