Hello!
I'm trying to fill the "spottedRegions" edit field with the number of objects. In the simple script this already works for me with the output of N_objects (number of objects):
imageRGB = imread('shapes.jpg');
imageGray = rgb2gray(imageRGB);
imageBinary = imbinarize(imageGray); figure; imshow(imageBinary); title('Binary Image');
[B,~,~] = bwboundaries(imageBinary);
N_objects = length(B)-1 % number of shapes
shapes.jpg
I have done little in the App Designer so far. How do I get the N_objects displayed in my output field for "spottedRegions" - meaning after I hit OpenRGBimage?
The function for this button:
function OpenButtonPushed(app, event)
imageRGB = imread('shapes.jpg');
imshow(imageRGB, 'Parent', app.UIAxes);
end
Full code in app designer:
classdef shapesProperties < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
CreateBinaryImageButton matlab.ui.control.Button
OpenButton matlab.ui.control.Button
spottedRegionsEditField matlab.ui.control.EditField
spottedRegionsEditFieldLabel matlab.ui.control.Label
regionIndexSpinner matlab.ui.control.Spinner
regionIndexSpinnerLabel matlab.ui.control.Label
UIAxes2 matlab.ui.control.UIAxes
UIAxes matlab.ui.control.UIAxes
end
methods (Access = public)
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: OpenButton
function OpenButtonPushed(app, event)
imageRGB = imread('shapes.jpg');
imshow(imageRGB, 'Parent', app.UIAxes);
end
% Button pushed function: CreateBinaryImageButton
function CreateBinaryImageButtonPushed(app, event)
imageRGB = imread('shapes.jpg');
% RGB --> Graustufen
imageGray = rgb2gray(imageRGB);
% Graustufen --> Binärbild (Schwarz/Weiß)
imageBinary = im2bw(imageGray);
imshow(imageBinary, 'Parent', app.UIAxes2);
end
% Value changed function: spottedRegionsEditField
function spottedRegionsEditFieldValueChanged(app, event)
value = app.spottedRegionsEditField.Value;
% value = N_objects ????
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'RGB Image')
app.UIAxes.Visible = 'off';
app.UIAxes.Position = [16 283 300 185];
% Create UIAxes2
app.UIAxes2 = uiaxes(app.UIFigure);
title(app.UIAxes2, 'Binary Image')
app.UIAxes2.Visible = 'off';
app.UIAxes2.Position = [315 283 300 185];
% Create regionIndexSpinnerLabel
app.regionIndexSpinnerLabel = uilabel(app.UIFigure);
app.regionIndexSpinnerLabel.HorizontalAlignment = 'right';
app.regionIndexSpinnerLabel.Position = [366 230 68 22];
app.regionIndexSpinnerLabel.Text = 'regionIndex';
% Create regionIndexSpinner
app.regionIndexSpinner = uispinner(app.UIFigure);
app.regionIndexSpinner.Position = [449 230 69 22];
% Create spottedRegionsEditFieldLabel
app.spottedRegionsEditFieldLabel = uilabel(app.UIFigure);
app.spottedRegionsEditFieldLabel.HorizontalAlignment = 'right';
app.spottedRegionsEditFieldLabel.Position = [143 230 89 22];
app.spottedRegionsEditFieldLabel.Text = 'spottedRegions';
% Create spottedRegionsEditField
app.spottedRegionsEditField = uieditfield(app.UIFigure, 'text');
app.spottedRegionsEditField.ValueChangedFcn = createCallbackFcn(app, @spottedRegionsEditFieldValueChanged, true);
app.spottedRegionsEditField.Position = [247 230 40 22];
% Create OpenButton
app.OpenButton = uibutton(app.UIFigure, 'push');
app.OpenButton.ButtonPushedFcn = createCallbackFcn(app, @OpenButtonPushed, true);
app.OpenButton.Position = [12 230 108 22];
app.OpenButton.Text = 'OpenRGB-Image';
% Create CreateBinaryImageButton
app.CreateBinaryImageButton = uibutton(app.UIFigure, 'push');
app.CreateBinaryImageButton.ButtonPushedFcn = createCallbackFcn(app, @CreateBinaryImageButtonPushed, true);
app.CreateBinaryImageButton.Position = [12 184 123 22];
app.CreateBinaryImageButton.Text = 'CreateBinary-Image';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = shapesProperties
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
Thank you very much in advance!
Michael

 Respuesta aceptada

Rik
Rik el 1 de Jul. de 2021

1 voto

app.spottedRegionsEditField.Value=N_objects;
You need to put this in OpenButtonPushed (along with the code to calculate N_objects).

2 comentarios

Thank you very much Rik!
I also tried assigning a number to the shapes (without that of the main image, i.e. white rectangle), but my loop does not work. I get only the "1" for the main image.
What I intend to do is to use a spinner that selects an index of a recognized region and displays it extracted in another graphic. How could I do that?
function OpenButtonPushed(app, event)
imageRGB = imread('C:\Users\Admin\Documents\MATLAB\Dennis\shapes.jpg');
imshow(imageRGB, 'Parent', app.UIAxes);
% RGB --> Graustufen
imageGray = rgb2gray(imageRGB);
% Graustufen --> Binärbild (Schwarz/Weiß)
imageBinary = im2bw(imageGray);
[Bnd,L,N,A] = bwboundaries(imageBinary);
N_objects = length(Bnd)-1;
app.spottedRegionsEditField.Value = num2str(N_objects);
[L,n] = bwlabel(imageBinary);
hold on
for k = 1:numel(N_objects) % I also tried 'numel(N_objects)+1' to skip the main image, didn't work out
% [L,n] = bwlabel(Bnd{k});
s = regionprops(L);
c = s(k).Centroid; % s(k+1) did not work out to skip main image
text(app.UIAxes, c(1), c(2), sprintf('%d', k), ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle', 'color', 'red');
end
end
Thanks and kind regards, Michael
Rik
Rik el 2 de Jul. de 2021
You should not hard-code a path in a GUI. Get the path from the user in some way.
bwlabel is the correct intuition. You can use L==n. However, you should retrieve n from the spinner instead of using a loop.
You should remember that your code should run correctly first. The you can implement it in your GUI. Preferably your callback retrieves some settings from objects and calls a function, nothing more.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Develop Apps Using App Designer en Centro de ayuda y File Exchange.

Productos

Versión

R2021a

Preguntada:

el 1 de Jul. de 2021

Comentada:

Rik
el 2 de Jul. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by