Code to count number of heads(people) present in a image
10 views (last 30 days)
Show older comments
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 Comments
MRahul
on 20 Feb 2017
Edited: Walter Roberson
on 20 Feb 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
on 20 Feb 2017
As I glance at that code, I do not see how it handles dark-skinned people.
Answers (3)
Image Analyst
on 20 Apr 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
on 20 Feb 2017
See crowd estimation articles here: http://www.visionbib.com/bibliography/motion-f735m1.html#Crowds,%20Tracking%20Multiple%20People,%20Multiple%20Pedestrian%20Tracking
7 Comments
babar ali
on 21 Nov 2017
i am expecting the code for calculating number of people in an image
Image Analyst
on 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.
Gogul
on 2 Aug 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 Comment
Image Analyst
on 2 Aug 2024
Edited: Image Analyst
on 2 Aug 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')
% 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
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
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.
See Also
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!






