How do I add a draw rectangle function to an image app?

20 vues (au cours des 30 derniers jours)
Kurt
Kurt le 19 Oct 2022
Commenté : Kurt le 21 Oct 2022
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?

Réponse acceptée

Eric Delgado
Eric Delgado le 20 Oct 2022
Modifié(e) : Eric Delgado le 20 Oct 2022
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 commentaires
Eric Delgado
Eric Delgado le 21 Oct 2022
Great to hear! Now that you are super happy with my support, you accept my answer, ok? :)
Kurt
Kurt le 21 Oct 2022
You betcha.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Images dans Help Center et File Exchange

Produits


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by