I have written code for shape detection ,it is working well ; However ,yellow square in the image is being labelled as 'rhombus'.

14 vues (au cours des 30 derniers jours)
function Shape_detection_Callback(hObject, eventdata, handles)
% hObject handle to Shape_detection (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of Shape_detection as text
% str2double(get(hObject,'String')) returns contents of Shape_detection as a double
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.60) && (abs(STATS(i).Metric) > 0.60))
STATS(i).Shape = 'Rhombus';
else
STATS(i).Shape = 'Triangle';
end
elseif (abs(STATS(i).BoundingBox(3) - STATS(i).BoundingBox(4)) > 0.2)
if (abs(STATS(i).Extent) > 1.12)
STATS(i).Shape = 'Rectangle';
elseif ((abs(STATS(i).Extent) > 1.0) && (abs(STATS(i).Metric) > 1.0))
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 results 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 = 'black';
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 :

Réponses (1)

Image Analyst
Image Analyst le 5 Août 2021
Evidently the circularity (what you call metric) is not a good measure to distinguish between squares and rhombuses.
  2 commentaires
MashalS
MashalS le 6 Août 2021
Modifié(e) : MashalS le 6 Août 2021
I have tried changing the metric (values) , but it does not work.What changes in code metrics are suggested ?
Image Analyst
Image Analyst le 6 Août 2021
It seems both your rhombus and square are squares with the only difference being the orientation of the square. Therefore, ask regionprops for 'Orientation'.

Connectez-vous pour commenter.

Produits


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by