Filter löschen
Filter löschen

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

10 Ansichten (letzte 30 Tage)
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 :

Antworten (1)

Image Analyst
Image Analyst am 5 Aug. 2021
Evidently the circularity (what you call metric) is not a good measure to distinguish between squares and rhombuses.
  2 Kommentare
MashalS
MashalS am 6 Aug. 2021
Bearbeitet: MashalS am 6 Aug. 2021
I have tried changing the metric (values) , but it does not work.What changes in code metrics are suggested ?
Image Analyst
Image Analyst am 6 Aug. 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'.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange

Produkte


Version

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by