How do I add a draw rectangle function to an image app?
32 次查看(过去 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!