How can I detect only regular shaped red color objects in HSV color detection method, eliminating the irregular red objects as shown in the figure

2 views (last 30 days)
Hello...Someone please help. I need to extract only regular shaped colored objects from an image. The image I have attached shows a circular red object along with some irregular shaped red objects. I need to extract only the regular shaped objects like square, circle, rectangle and triangle. I used the HSV color space to get the binary image of red objects.How can i remove the irregular shaped red objects in the image and display only the red circular object placed anywhere in the image (not always centrally placed). Image file is attached below (help.jpg).Here is the code:
if true
% code
end
I=imread('test3.jpg');
hsvI = rgb2hsv(I);
hueI = round(hsvI(:,:,1)*360);
satI = hsvI(:,:,2);
valI = hsvI(:,:,3);
threshI1 = (satI>=0.3)&(satI<=1)&(valI>=0.2)&(valI<=1);%original valI lower is .37%
red = ((hueI<15)|(hueI>=330))&threshI1;
red = medfilt2(red, [3 3]);
red=bwareaopen(red,200);
red = imclearborder(red);
figure;
set(gcf, 'Position', get(0, 'ScreenSize'));
imshow(I);
title('detected red');
hold on;
binaryImagered = imfill(red, 'holes');
labeledImagered = bwlabel(binaryImagered, 8);
blobMeasurementsred = regionprops(labeledImagered,binaryImagered,'centroid','BoundingBox','Area','perimeter');
numberOfBlobsred = size(blobMeasurementsred, 1);
boundingboxesred = cat(1,blobMeasurementsred.BoundingBox);
hold on;
boundariesred = bwboundaries(binaryImagered);
numberOfBoundariesred = size(boundariesred, 1);
Redx=[];
Redy=[];
for object = 1:length(blobMeasurementsred)
bc = blobMeasurementsred(object).Centroid;
plot(bc(1),bc(2), 'r*')
Redx=cat(2,Redx,bc(1));
Redy=cat(2,Redy,bc(2));
a=text(bc(1),bc(2)+50, strcat('X: ', num2str(round(bc(1))), ' Y: ', num2str(round(bc(2)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
end
for i = 1 : numberOfBoundariesred
thisBoundaryred = boundariesred{i};
plot(thisBoundaryred(:,2), thisBoundaryred(:,1), 'r', 'LineWidth', 2);
end
[labeled,numRedObjects] = bwlabel(binaryImagered,4);
%disp(numRedObjects);
shapedata = regionprops(labeled,'basic');
for j=1:numRedObjects
ShapeDataRed=shapedata(j).BoundingBox;
for k = 1:size(boundingboxesred,1) rectangle('position',boundingboxesred(k,:),'Edgecolor','y') end end hold off; hold off;

Answers (0)

Categories

Find more on Read, Write, and Modify Image 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!