Code to count number of heads(people) present in a image

10 vues (au cours des 30 derniers jours)
Rohit Bhide
Rohit Bhide le 20 Avr 2013
Modifié(e) : Image Analyst le 2 Août 2024
I want a matlab program which will count number of heads present in image and not other things present such as trees,other objects etc.please send me entire code.
  3 commentaires
MRahul
MRahul le 20 Fév 2017
Modifié(e) : Walter Roberson le 20 Fév 2017
create a folder, put the image in jpg format on which u want to run this code, save the program in the same folder. Run
instead of 15.jpg put your image file name.
%get the input image
I=imread('15.jpg');
imshow(I),title('Image:1');
%change the color space
cform = makecform('srgb2lab');
J=applycform(I,cform);
figure;imshow(J),title('Image:2');
%equalise brightness to get skin area
K=J(:,:,2);% 2nd page of 3-d vector j
figure;imshow(K),title('Image:3');
L=graythresh(J(:,:,2));% find appropriate gray thresh value
BW1=im2bw(J(:,:,2),L);% convert to binary image based on threshold
figure;imshow(BW1),title('Image:4');
bw2=imfill(BW1,'holes');% fill patches with holes
figure;imshow(bw2)
bw3 = bwareaopen(bw2,1890); %opens area greater than 1890
cc=bwconncomp(bw3)% connected comp for finding the density of people in
image
density=cc.NumObjects / (size(bw3,1) * size(bw3,2))
figure;imshow(bw3)
labeledImage = bwlabel(bw3, 8);%same as connected components
figure;imshow(labeledImage)
blobMeasurements = regionprops(labeledImage,'all');%measure all properties of the image
numberOfPeople = size(blobMeasurements, 1)% count the number of people
% draw bounding boxes
imagesc(I);
hold on;
title('Original with bounding boxes');
for k = 1 : numberOfPeople % Loop through all blobs.
% Find the mean of each blob.
% directly into regionprops.
thisBlobsBox = blobMeasurements(k).BoundingBox;
% Get list of pixels in current blob.
x1 = thisBlobsBox(1);%1st side
y1 = thisBlobsBox(2);%2nd side
x2 = x1 + thisBlobsBox(3);%3rd side
y2 = y1 + thisBlobsBox(4);%4th side
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
%subplot(3,4,2);
plot(x, y, 'LineWidth', 2);
end
Walter Roberson
Walter Roberson le 20 Fév 2017
As I glance at that code, I do not see how it handles dark-skinned people.

Connectez-vous pour commenter.

Réponses (3)

Image Analyst
Image Analyst le 20 Avr 2013
It should be pretty easy if you have the Computer Vision System Toolbox, as this demo from it shows counted heads: http://www.mathworks.com/products/computer-vision/description4.html

Image Analyst
Image Analyst le 20 Fév 2017
  7 commentaires
babar ali
babar ali le 21 Nov 2017
i am expecting the code for calculating number of people in an image
Image Analyst
Image Analyst le 21 Nov 2017
Again, WHAT "code is not working correctly"? You haven't posted your code or your images or even read this link yet.
And I DID suggest algorithms - Lots of them. Evidently you chose one and coded it up but you haven't said which one or even shared the code, so what can anyone do to help you????
I don't even know how many heads you have in your image. Three or four moving heads is a lot easier and more accurate than tens of thousands of still heads, like from an aerial view.
Keep in mind that this is no 5 minute project and we are not going to develop a turnkey system for you. You're either going to have to write the thing yourself, or hire someone to write it for you.

Connectez-vous pour commenter.


Gogul
Gogul le 2 Août 2024
%get the input image
I=imread('15.jpg');
imshow(I),title('Image:1');
%change the color space
cform = makecform('srgb2lab');
J=applycform(I,cform);
figure;imshow(J),title('Image:2');
%equalise brightness to get skin area
K=J(:,:,2);% 2nd page of 3-d vector j
figure;imshow(K),title('Image:3');
L=graythresh(J(:,:,2));% find appropriate gray thresh value
BW1=im2bw(J(:,:,2),L);% convert to binary image based on threshold
figure;imshow(BW1),title('Image:4');
bw2=imfill(BW1,'holes');% fill patches with holes
figure;imshow(bw2)
bw3 = bwareaopen(bw2,1890); %opens area greater than 1890
cc=bwconncomp(bw3)% connected comp for finding the density of people in
image
density=cc.NumObjects / (size(bw3,1) * size(bw3,2))
figure;imshow(bw3)
labeledImage = bwlabel(bw3, 8);%same as connected components
figure;imshow(labeledImage)
blobMeasurements = regionprops(labeledImage,'all');%measure all properties of the image
numberOfPeople = size(blobMeasurements, 1)% count the number of people
% draw bounding boxes
imagesc(I);
hold on;
title('Original with bounding boxes');
for k = 1 : numberOfPeople % Loop through all blobs.
% Find the mean of each blob.
% directly into regionprops.
thisBlobsBox = blobMeasurements(k).BoundingBox;
% Get list of pixels in current blob.
x1 = thisBlobsBox(1);%1st side
y1 = thisBlobsBox(2);%2nd side
x2 = x1 + thisBlobsBox(3);%3rd side
y2 = y1 + thisBlobsBox(4);%4th side
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
%subplot(3,4,2);
plot(x, y, 'LineWidth', 2);
end
  1 commentaire
Image Analyst
Image Analyst le 2 Août 2024
Modifié(e) : Image Analyst le 2 Août 2024
There is a function rgb2lab that makes the colorspace conversion easier than using makecform and applycform.
You say you want to "equalise brightness to get skin area" -- this is not necessary, plus you didn't even do it. You created K as the "A" channel image of the LAB image, and showed it as "Image3" but you never use it.
The code is intended for use in images where only the face is visible, with no overhead views and no arms, hands, or other reddish regions in the image (since you threshold on the A channel to get red things).
You forgot to attach '15.jpg' or even a screenshot to show how well your algorithm works. So let's see how well it works with a standard demo image:
% Get the input image - standard demo image that ships with MATLAB.
I=imread('visionteam.jpg');
imshow(I),title('Image:1');
%change the color space
cform = makecform('srgb2lab');
J=applycform(I,cform);
figure;imshow(J),title('Image:2 LAB image');
%equalise brightness to get skin area
K=J(:,:,2);% 2nd page of 3-d vector j
figure;imshow(K, []),title('Image:3 The A channel only');
L=graythresh(J(:,:,2));% find appropriate gray thresh value
BW1=im2bw(J(:,:,2),L);% convert to binary image based on threshold
figure;imshow(BW1),title('Image:4 thresholded A channel');
bw2=imfill(BW1,'holes');% fill patches with holes
figure;imshow(bw2)
% Change by Image Analyst to figure out minimum allowable size of red region.
% Existing value of 1890 did not work for this particular image.
% Get areas of blobs
props = regionprops(bw2, 'Area');
allAreas = sort([props.Area], 'descend')
allAreas = 1x78
3362 1830 1585 1321 1271 1162 661 376 299 292 282 279 264 244 242 199 123 119 89 87 80 78 71 59 56 55 43 36 36 35
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
% Let's use a minimum area of 1000 for this particular photo.
minAllowableArea = 1000;
bw3 = bwareaopen(bw2,minAllowableArea); %opens area greater than 1890
% End of change by Image Analyst to figure out minimum allowable size of red region.
cc=bwconncomp(bw3)% connected comp for finding the density of people in image
cc = struct with fields:
Connectivity: 8 ImageSize: [413 800] NumObjects: 6 PixelIdxList: {[3362x1 double] [1585x1 double] [1162x1 double] [1321x1 double] [1271x1 double] [1830x1 double]}
density=cc.NumObjects / (size(bw3,1) * size(bw3,2))
density = 1.8160e-05
figure;imshow(bw3)
labeledImage = bwlabel(bw3, 8);%same as connected components
figure;imshow(labeledImage, [])
blobMeasurements = regionprops(labeledImage,'all');%measure all properties of the image
numberOfPeople = size(blobMeasurements, 1)% count the number of people
numberOfPeople = 6
% draw bounding boxes
imagesc(I);
hold on;
title('Original with bounding boxes');
for k = 1 : numberOfPeople % Loop through all blobs.
% Find the mean of each blob.
% directly into regionprops.
thisBlobsBox = blobMeasurements(k).BoundingBox;
% Get list of pixels in current blob.
x1 = thisBlobsBox(1);%1st side
y1 = thisBlobsBox(2);%2nd side
x2 = x1 + thisBlobsBox(3);%3rd side
y2 = y1 + thisBlobsBox(4);%4th side
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
%subplot(3,4,2);
plot(x, y, 'LineWidth', 2);
end
It seemed to miss one person and count red sleeves of another person. A fair start but you might want to make it more robust by putting an upper threshold on your segmentation of the A channel to eliminate abnormally red regions and figure out how to handle beards and mustaches to capture the missing person without capturing hands.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Image Segmentation and Analysis dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by