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

17 views (last 30 days)
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
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
Walter Roberson on 20 Feb 2017
As I glance at that code, I do not see how it handles dark-skinned people.

Sign in to comment.

Answers (2)

Image Analyst
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
Image Analyst on 20 Feb 2017
  7 Comments
babar ali
babar ali on 21 Nov 2017
i am expecting the code for calculating number of people in an image
Image Analyst
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!