Why does not my code work?
Info
Diese Frage ist geschlossen. Öffnen Sie sie erneut, um sie zu bearbeiten oder zu beantworten.
Ältere Kommentare anzeigen
My code is about shape recognition for a circle and rectangle, I'm having one error in the code and didn't know how to make it right. can any one help me please.
clc
close all
clear
%%Reading Image.
Image = 'Image.JPG';
RGBImage = imread(Image);
RGBImage = imresize(RGBImage,[500,500]);
subplot(2,2,1)
imshow(RGBImage)
title('Input Image')
%%Image Conversions To GrayScale.
GrayImage = rgb2gray(RGBImage);
subplot(2,2,2)
imshow(GrayImage)
title('GrayScale Image')
%%Binarize The Image.
BinaryImage = imbinarize(GrayImage);
subplot(2,2,3)
imshow(BinaryImage)
title('Initial (Noisy) Binary Image')
%%Remove Small Objects.
BinaryImage = bwareaopen(BinaryImage, 100000);
BinaryImage = imcomplement(BinaryImage); % Complement image
subplot(2,2,4)
imshow(BinaryImage)
title('Cleaned Binary Image')
%%Measure Properties Of BinaryImage Regions.
[LabeledImage, NumberOfObjects] = bwlabel(BinaryImage);
Stats = regionprops('Table', LabeledImage, 'All');
%%Get The Outermost Boundaries Of The Objects.
FilledImage = imfill(BinaryImage, 'holes');
Boundaries = bwboundaries(FilledImage);
%%Collect Some Of The Measurements Into Individual Arrays.
Perimeter = [Stats.Perimeter];
Area = [Stats.Area];
FilledArea = [Stats.FilledArea];
Solidities = [Stats.Solidity];
%%Calculate Circularies.
Circularities = Perimeter.^2./(4*pi*FilledArea);
%%Print To Command Window.
fprintf('#, Perimeter, Area, Filled Area, Solidity, Circulariity\n');
for BlobNumber = 1:NumberOfObjects
fprintf('%d, %9.3f, %11.3f, %11.3f, %8.3f, %11.3f\n',...
BlobNumber, Perimeter(BlobNumber), Area(BlobNumber),...
FilledArea(BlobNumber), Solidities(BlobNumber), Circularities(BlobNumber));
end
for BlobNumber = 1:NumberOfObjects
% Outline the object so the user can see it.
ThisBoundary = Boundaries{BlobNumber};
subplot(2,2,2); % Switch to upper right image.
hold on
% Display previous boundaries in blue.
for k = 1:BlobNumber-1
ThisBoundary = Boundaries(k);
plot(ThisBoundary(:,2), ThisBoundary(:,1), 'b', 'LineWidth', 3);
end
% Display this boundary in red.
ThisBoundary = Boundaries{BlobNumber};
plot(ThisBoundary(:,2), ThisBoundary(:,1), 'r', 'LineWidth', 3);
subplot(2,2,4); % Switch to lower right image.
%%Determaine The Shape.
if Circularities(BlobNumber) < 1.10
Message = sprintf('The Circularity Of Object #%d is %.3f, \nso The Object Is Circle',...
BlobNumber, Circularities(BlobNumber));
Shape = 'Circle';
elseif Circularities(BlobNumber) < 2.0
Message = sprintf('The Circularity Of Object #%d is %.3f, \nso The Object Is Retangle',...
BlobNumber, Circularities(BlobNumber));
Shape = 'Rectangle';
else
Message = sprintf('The Circularity Of Object #%d is %.3f, \nso The Object Is Something else',...
BlobNumber, Circularities(BlobNumber));
Shape = 'Something else';
end
%%Display In Overlay Above The Object.
OverlayMessage = sprintf('Object #%d = %s\ncirc = %.2f, s = %.2f',...
BlobNumber, Shape, Circularities(BlobNumber), Solidities(BlobNumber));
text(Stats(BlobNumber).Centroid(1), Stats(BlobNumber).Centroid(2),...
OverlayMessage, 'color', 'r');
button = questdlg(Message, 'Continue', 'Continue', 'Cancel', 'Continue');
if strcmp(button, 'Cancel')
break;
end
end
2 Kommentare
M
am 2 Mär. 2018
Could you post the entire error message that you get ?
odai kiwan
am 2 Mär. 2018
Antworten (0)
Diese Frage ist geschlossen.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!