I have been trying to draw a interactive rectangle in matlab appdesigner, over an existing image currently on the UIAxes. Is there anyway to allow user choose a region for processing
13 views (last 30 days)
Show older comments
The image file is already loaded on the UIAxes. I need to create small regions in the image so I can extract and process from.
classdef Program02
if true
% code
end< matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
UIAxes matlab.ui.control.UIAxes
Load2ImagesButton matlab.ui.control.Button
ToggleImagesSwitchLabel matlab.ui.control.Label
ToggleImagesSwitch matlab.ui.control.ToggleSwitch
UITable matlab.ui.control.Table
CorrelationWindowsizeDropDownLabel matlab.ui.control.Label
CorrelationWindowsizeDropDown matlab.ui.control.DropDown
end
properties (Access = private)
_ _imgObj; % Object for loading and displaying images
A;B; test; % Objects used in switching between 2 images__
end
methods (Access = private)
% Button pushed function: Load2ImagesButton
function Load2ImagesButtonPushed(app, event)
[File,Path] = uigetfile({'*.jpg;*.png;*.bmp;*.tif';'*.*'},...
'Select Two Images to Process (hold ctrl + shift and click on 2 images)',...
'MultiSelect', 'on'); %%opens user interface for selecting
%%two image files
if (length(File))~=2
text = ['Sorry, Please select 2 images,'...
'\nby holding Ctrl and Click on 2 images'...
'\n You must only select 2 images'...
'\n Continue or Exit?'];
message = sprintf(text);
reply = questdlg(message, 'Select 2 images',...
'Continue', 'Exit', 'Continue');
if strcmpi(reply, 'Exit')
% User said No, so exit.
return;
else
[File,Path] = uigetfile({'*.jpg;*.png;*.bmp;*.tif';'*.*'},...
'Select Two Images to Process (hold ctrl + shift and click on 2 images)',...
'MultiSelect', 'on'); % opens user interface for selecting
% two image files
end
end
app.A = strcat(Path,File{1}); %%creates file directory for first for first image selected
app.B = strcat(Path,File{2}); %%creates file directory and object for first for second image selected
if ~isempty(app.A) %%check if there is any selected image files
imshow(app.A,'Parent',app.UIAxes); %%displays first selected image
l=imfinfo(app.A); % gets the information of selected image
%and presents it in structure format
d = {'Image Height',l.Height;'Image Width',l.Width;...
'Image size in bytes',l.FileSize;...
'Image Format', l.Format;...
'Image Bit depth', l.BitDepth}; % cell matrix with required
% image info
app.UITable.Data=d; %%display required info on UI table
else
% displays on home screen error message if cancel button is selected
disp('no file selcted,Please select Images to Process');
end
end
% Value changed function: ToggleImagesSwitch
function ToggleImagesSwitchValueChanged(app, event)
value = app.ToggleImagesSwitch.Value;
if (strcmpi(value,'Image 2') ) %%compares if the switch is on or off
imshow(app.A,'Parent',app.UIAxes); %%shows 1st image if switch off
l=imfinfo(app.A); %%collect 1st image info
d = {'Image Height',l.Height;'Image Width',l.Width;...
'Image size in bytes',l.FileSize;...
'Image Format', l.Format;...
'Image Bit depth', l.BitDepth}; %%cell matrix with required
%%image info
app.UITable.Data=d;%%display required info on UI table
else
imshow(app.B,'Parent',app.UIAxes);%%shows 2nd image if switch on
k=imfinfo(app.B); %%collects 2nd image info
e = {'Image Height',k.Height;'Image Width',k.Width;...
'Image size in bytes',k.FileSize;...
'Image Format', k.Format;...
'Image Bit depth', k.BitDepth};%%cell matrix with required
%%image info
app.UITable.Data=e;%%display required info on UI table
end
end
% Value changed function: CorrelationWindowsizeDropDown
function CorrelationWindowsizeDropDownValueChanged(app, event)
value = app.CorrelationWindowsizeDropDown.Value;
if (strcmpi(value,'16X16') ) % for each drop down window
h = drawrectangle('Parent',app.UIAxes,...
'Position',[10,10,100,100],'EdgeColor','b');
end
end
end
% App initialization and construction
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure
app.UIFigure = uifigure;
app.UIFigure.Position = [100 100 746 527];
app.UIFigure.Name = 'UI Figure';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, 'Images')
app.UIAxes.Position = [316 211 309 292];
% Create Load2ImagesButton
app.Load2ImagesButton = uibutton(app.UIFigure, 'push');
app.Load2ImagesButton.ButtonPushedFcn = createCallbackFcn(app, @Load2ImagesButtonPushed, true);
app.Load2ImagesButton.Position = [50 402 126 37];
app.Load2ImagesButton.Text = 'Load 2 Images';
% Create ToggleImagesSwitchLabel
app.ToggleImagesSwitchLabel = uilabel(app.UIFigure);
app.ToggleImagesSwitchLabel.HorizontalAlignment = 'center';
app.ToggleImagesSwitchLabel.Position = [59 61 80 22];
app.ToggleImagesSwitchLabel.Text = 'ToggleImages';
% Create ToggleImagesSwitch
app.ToggleImagesSwitch = uiswitch(app.UIFigure, 'toggle');
app.ToggleImagesSwitch.Items = {'Image 1', 'Image 2'};
app.ToggleImagesSwitch.ValueChangedFcn = createCallbackFcn(app, @ToggleImagesSwitchValueChanged, true);
app.ToggleImagesSwitch.Position = [79 114 38 86];
app.ToggleImagesSwitch.Value = 'Image 2';
% Create UITable
app.UITable = uitable(app.UIFigure);
app.UITable.ColumnName = {'Column 1'; 'Column 2'};
app.UITable.RowName = {};
app.UITable.Position = [259 30 302 170];
% Create CorrelationWindowsizeDropDownLabel
app.CorrelationWindowsizeDropDownLabel = uilabel(app.UIFigure);
app.CorrelationWindowsizeDropDownLabel.HorizontalAlignment = 'right';
app.CorrelationWindowsizeDropDownLabel.Position = [19 279 135 22];
app.CorrelationWindowsizeDropDownLabel.Text = 'Correlation Window size';
% Create CorrelationWindowsizeDropDown
app.CorrelationWindowsizeDropDown = uidropdown(app.UIFigure);
app.CorrelationWindowsizeDropDown.Items = {'8X8', '16X16', '32X32', '64X64', '128X128', '256X256'};
app.CorrelationWindowsizeDropDown.ValueChangedFcn = createCallbackFcn(app, @CorrelationWindowsizeDropDownValueChanged, true);
app.CorrelationWindowsizeDropDown.Position = [170 279 100 22];
app.CorrelationWindowsizeDropDown.Value = '8X8';
end
end
methods (Access = public)
% Construct app
function app = MECH6505_Program02_Omeziri_Chukwuibuikem
% Create and configure 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
0 Comments
Answers (2)
Image Analyst
on 23 Oct 2018
You can use either imrect() in the Image Processing Toolbox, or rbbox() in base MATLAB.
5 Comments
Image Analyst
on 9 Jun 2022
You can call delete(hRect) if you want to clear the old one, like this:
% Demo to show how drawrectangle can be used to draw a rectangular box on the image.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
rgbImage = imread('peppers.png');
hFig = figure;
imshow(rgbImage);
hFig.WindowState = 'maximized';
% Ask user to draw rectangle.
button = 'Redraw';
while contains(button, 'Redraw', 'IgnoreCase',true)
uiwait(helpdlg('Draw a box'));
% User draws a box. It exits as soon as they lift the mouse.
hBox = drawrectangle('Color', 'r');
% Get the coordinates in the form [xLeft, yTop, width, height].
roiPosition = hBox.Position;
% Delete the ROI object.
delete(hBox);
% and replace it with a rectangle in the graphical overlay.
hold on;
hRect = rectangle('Position', roiPosition, 'EdgeColor', 'r', 'LineWidth', 2);
% Ask user if the rectangle is acceptable.
message = sprintf('Is this good?');
button = questdlg(message, message, 'Accept', 'Redraw', 'Reject and Quit', 'Accept');
if contains(button, 'Quit','IgnoreCase',true)
delete(hRect); % Delete the box from the overlay.
roiPosition = [];
break;
elseif contains(button, 'Redraw','IgnoreCase',true)
% OPTIONAL If you want to delete the prior one before drawing the next one.
delete(hRect);
elseif contains(button, 'Accept','IgnoreCase',true)
break;
end
end
% If you want to delete the rectangle from the overlay, do this:
delete(hRect); % Delete the box from the overlay.
% Close down figure.
close(hFig);
Biraj Khanal
on 9 Jun 2022
Yes, that works. Thanks!!
I started another question for the same topic. I am also trying to do this without the image processsing toolbox to learn the keypress and windowbuttonpress functions in appdesigner. Not sure if linking to another question is allowed here, but could you please help?
https://de.mathworks.com/matlabcentral/answers/1728335-interactive-rectangle-on-uiaxes-for-user-to-choose-a-region
Rami Babayew
on 26 Sep 2023
Edited: Rami Babayew
on 26 Sep 2023
@Image Analyst hi, thank you for your example. its works great in app designer, but when im trying to work on stand alone mode (execute file) it wont opening the figure for selecting the box region. i dont understand why. do you have a clue?
7 Comments
Image Analyst
on 29 Sep 2023
workspace is a function that can only be called from within the MATLAB integrated development environment, as are all other panels that you can display in MATLAB, as well as any of the "Apps" shown on the Apps tab of the tool ribbon. The workspace panel is meant for helping you debug your code and is not meant to be shown in compiled programs. If you want to display the values of some variables, you'll have to use fprint() to print them out to the command window, or else put some place on your GUI where you can show them, like a table or edit fields or static text labels.
See Also
Categories
Find more on MATLAB Support Package for IP Cameras in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!