MATLAB Answers

Does order of GUI function callbacks matter in displaying output on axes (for an image)? Can output of two callback functions distort or doesnt appear beacuse of it ?

1 view (last 30 days)
MashalS
MashalS on 16 Aug 2021
Commented: Rik on 17 Aug 2021
% --- Executes on button press in histogram.
function histogram_Callback(hObject, eventdata, handles)
% hObject handle to histogram (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
a=imread('Capture.jpg');
red_plane=a(:,:,1);
green_plane=a(:,:,2);
blue_plane=a(:,:,3);
[red_data pixel_level]=imhist(red_plane);
[green_data pixel_level]=imhist(green_plane);
[blue_data pixel_level]=imhist(blue_plane);
bar(pixel_level, red_data,'r');
hold on;
bar(pixel_level, green_data,'g');
bar(pixel_level, blue_data,'b');
axes(handles.axes2);
% --- Executes on button press in ShapeDetection.
function ShapeDetection_Callback(hObject, eventdata, handles)
% hObject handle to ShapeDetection (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
debugDisplay = 0;
%Read image
originalImage = imread('Capture.jpg');
if debugDisplay == 1
figure;
axes(handles.axes2);
end
% Convert to grey
grayImage = rgb2gray(originalImage);
if debugDisplay == 1
axes(handles.axes2);
imshow(grayImage);
end
% Binarize image
binarizedImage = imbinarize(grayImage, 0.9);
if debugDisplay == 1
axes(handles.axes2);
imshow(binarizedImage);
end
% Detect closed regions
[B, L] = bwboundaries(~ binarizedImage, 'noholes');
if debugDisplay == 1
axes(handles.axes2);
imshow(originalImage);
hold on
for k = 1:length(B)
boundary = B{k};
plot(boundary(:, 2), boundary(:, 1), 'red', 'LineWidth', 2)
end
end
% Get properties of detected regions
STATS = regionprops(L, 'Area', 'Centroid', 'Perimeter', 'Extent', 'BoundingBox');
numberOfShapes = length(STATS);
% Calculate metric for each shape
for i = 1 : numberOfShapes
STATS(i).Metric = 4 * 3.14 * STATS(i).Area / (STATS(i).Perimeter * STATS(i).Perimeter);
end
%Analyze each figure properties
for i = 1 : numberOfShapes
if (abs(STATS(i).BoundingBox(3) - STATS(i).BoundingBox(4)) < 0.1)
if (abs(STATS(i).Extent) > 0.95)
STATS(i).Shape = 'Square';
elseif ((abs(STATS(i).Extent) > 0.70) && (abs(STATS(i).Metric) > 0.95))
STATS(i).Shape = 'Circle';
elseif ((abs(STATS(i).Extent) > 0.70) && (abs(STATS(i).Metric) > 0.70))
STATS(i).Shape = 'Rhombus';
else
STATS(i).Shape = 'Triangle';
end
elseif (abs(STATS(i).BoundingBox(3) - STATS(i).BoundingBox(4)) > 0.1)
if (abs(STATS(i).Extent) > 0.95)
STATS(i).Shape = 'Rectangle';
elseif ((abs(STATS(i).Extent) > 0.78) && (abs(STATS(i).Metric) > 0.64))
STATS(i).Shape = 'Ellipsis';
elseif (abs(STATS(i).Extent) < 0.6) && (0.65 > abs(STATS(i).Metric) && (abs(STATS(i).Metric) > 0.40))
STATS(i).Shape = 'Triangle';
elseif (abs(STATS(i).Metric) > 0.70)
STATS(i).Shape = 'Rhombus';
else
STATS(i).Shape = 'Other2';
end
else
STATS(i).Shape = 'Other1';
end
end
% Prepare shapes figure
axes(handles.axes2);
imshow(originalImage)
hold on;
% Display name of each shape
for i = 1 : numberOfShapes
txtOffset = 45;
txt = STATS(i).Shape;
switch STATS(i).Shape
case {'Rectangle', 'Rhombus'}
txtOffset = 58;
case {'Ellipsis', 'Triangle'}
txtOffset = 45;
end
centroid = STATS(i).Centroid;
t = text(centroid(1) - txtOffset, centroid(2), txt);
t.Color = 'white';
t.FontSize = 10;
rectangle(...
'Position', ...
[STATS(i).BoundingBox(1) ...
STATS(i).BoundingBox(2) ...
STATS(i).BoundingBox(3) ...
STATS(i).BoundingBox(4)], ...
'EdgeColor', 'blue', ...
'LineStyle', '--', ...
'LineWidth', 1 ...
);
end
GUI : Option which is selected first gives no error in output, but either button selected at second place gives error in output.
When i click histogram , it displays (no errors) :
BUT when i click Shape detection afterwards, it shows this white patch(error) :
WHEN I CLICK 'shape detection' first ,it gives no errors :
But when i select 'histogram' , it gives errors :
  1 Comment
Rik
Rik on 17 Aug 2021
Your functions both seem to assume they have a clean axes to work with, but you don't make sure that is actually the case. You also don't use explicit handles to your axes object in every graphics call. It is good practice when writing a GUI to use explicit handles everywhere, as the user might click on a different figure midway your code, which would change the current figure and current axes.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by