How can I detect the objects from a snapshot taken by camera

1 view (last 30 days)
I have taken a snapshot by camera and i want to detect the number of objects (the 2 bottles in this case). But after implementing the object detection algorithm, many objects are detected simultaneously and the result is not what I really want. It will be of great help if someone can post their code along with the explanation. My code is as follows:
clc
camR = ipcam('http://192.168.43.1:8080/video');
N = 1;
for i = 1:N
ARless = snapshot(camR);
end
AL1 = ARless + 110;
AL1rmat = AL1(:,:,1);
AL1gmat = AL1(:,:,2);
AL1bmat = AL1(:,:,3);
levelr = 0.5;
levelg = 0.4;
levelb = 0.3;
AL11 = imbinarize(AL1rmat,levelr);
AL12 = imbinarize(AL1gmat,levelg);
AL13 = imbinarize(AL1bmat,levelb);
AL1Isum = (AL11&AL12&AL13);
AL1Icomp = imcomplement(AL1Isum);
AL1Ifilled = imfill(AL1Icomp,'holes');
seL = strel('disk',5);
AL1Iopenned = imopen(AL1Ifilled,seL);
AL1Iregion = regionprops(AL1Iopenned, 'centroid');
[labeled,AL1numObjects] = bwlabel(AL1Iopenned,4);
AL1stats = regionprops(labeled,'Eccentricity', 'Area', 'BoundingBox');
AL1areas = [AL1stats.Area];
AL1eccentricities = [AL1stats.Eccentricity];
AL1idxOfSkittles = find(AL1eccentricities);
AL1statsDefects = AL1stats(AL1idxOfSkittles);
figure, imshow(AL1);
hold on;
for AL1idx = 1 : length(AL1idxOfSkittles)
AL1h = rectangle('Position', AL1statsDefects(AL1idx).BoundingBox);
set (AL1h,'EdgeColor' , [.75 0 0]);
hold on;
end
if AL1idx == 1
title(['There is ', num2str(AL1numObjects), ' Object in the image']);
end
if AL1idx>1
title(['There are ', num2str(AL1numObjects), ' Objects in the image']);
end
hold off;
And the result is:

Answers (0)

Categories

Find more on MATLAB Support Package for IP Cameras 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!