counting people from video
10 views (last 30 days)
Show older comments
do you know matlab code to counting people walk in video? when people walk from left to right in video, i want counting plus 1 and when people walk from right to left in video, i want counting minus 1. i have code in matlab to position for centroid but i don't know code to counting people. this my code
if true
% code
end
clc;
close all;
clear all;
video = mmreader('Video_1_2.avi'); %in place of aviread
%nframes = length(video);
nframes=video.NumberOfFrames;
for i=1:nframes
mov(i).cdata=read(video,i);
%creating '.cdata' field to avoid much changes to previous code
end
temp = zeros(size(mov(1).cdata));
[M,N] = size(temp(:,:,1));
for i = 1:10
temp = double(mov(i).cdata) + temp;
end
imbkg = temp/10;centroidx = zeros(nframes,1);
centroidy = zeros(nframes,1);
predicted = zeros(nframes,4);
actual = zeros(nframes,4);
R=[[0.2845,0.0045]',[0.0045,0.0455]'];
H=[[1,0]',[0,1]',[0,0]',[0,0]'];
Q=0.01*eye(4);
P = 100*eye(4);
dt=1;
A=[[1,0,0,0]',[0,1,0,0]',[dt,0,1,0]',[0,dt,0,1]'];
kfinit = 0;
th = 38;
for i=1:nframes
imshow(mov(i).cdata);
hold on
imcurrent = double(mov(i).cdata);
diffimg = zeros(M,N);
diffimg = (abs(imcurrent(:,:,1)-imbkg(:,:,1))>th) ...
| (abs(imcurrent(:,:,2)-imbkg(:,:,2))>th) ...
| (abs(imcurrent(:,:,3)-imbkg(:,:,3))>th);
labelimg = bwlabel(diffimg,4);
markimg = regionprops(labelimg,['basic']);
[MM,NN] = size(markimg);
for nn = 1:MM
if markimg(nn).Area > markimg(1).Area
tmp = markimg(1);
markimg(1)= markimg(nn);
markimg(nn)= tmp;
end
end
bb = markimg(1).BoundingBox;
xcorner = bb(1);
ycorner = bb(2);
xwidth = bb(3);
ywidth = bb(4);
cc = markimg(1).Centroid;
centroidx(i)= cc(1);
centroidy(i)= cc(2);
hold on
rectangle('Position',[xcorner ycorner xwidth ywidth],'EdgeColor','b');
hold on
plot(centroidx(i),centroidy(i), 'bx');
a=text(centroidx(i)+15,centroidy(i), strcat('X: ', num2str(round(centroidx(i))), ' Y: ', num2str(round(centroidy(i)))));
set(a, 'FontName', 'Arial', 'FontWeight', 'bold', 'FontSize', 12, 'Color', 'yellow');
kalmanx = centroidx(i)- xcorner;
kalmany = centroidy(i)- ycorner;
if kfinit == 0
predicted =[centroidx(i),centroidy(i),0,0]' ;
else
predicted = A*actual(i-1,:)';
end
kfinit = 1;
Ppre = A*P*A' + Q;
K = Ppre*H'/(H*Ppre*H'+R);
actual(i,:) = (predicted + K*([centroidx(i),centroidy(i)]' - H*predicted))';
P = (eye(4)-K*H)*Ppre;
hold on
rectangle('Position',[(actual(i,1)-kalmanx)...
(actual(i,2)-kalmany) xwidth ywidth],'EdgeColor','r','LineWidth',1.5);
hold on
plot(actual(i,1),actual(i,2), 'rx','LineWidth',1.5);
drawnow;
end
6 Comments
B Sarath
on 3 Feb 2023
Sir,actually the code is not working which you have posted.Can you do any modifications with below code,because it doesn't get any accurate output. So please help in this......
% Read the video
videoname = VideoReader('1.mp4');
% Create a Haar cascade classifier object
faceDetector = vision.CascadeObjectDetector();
% Initialize the count of people
peopleCount = 0;
% Loop through the video frames
while hasFrame(videoname)
frame = readFrame(videoname);
% Apply the Haar cascade classifier
bbox=step(faceDetector,frame);
% Count the number of people
peopleCount = peopleCount + size(bbox, 1);
end
% Display the total number of people detected
disp(peopleCount);
Image Analyst
on 3 Feb 2023
It looks like, if a person is in, say, 10 frames in a row, you count it as being 10 persons. You don't want to do that.
Answers (1)
Image Analyst
on 27 Oct 2015
If you can't get the people tracking code here http://www.mathworks.com/products/computer-vision/features.html#object-tracking-and-motion-estimation to work, then call the Mathworks, since it's one of their online demos.
0 Comments
See Also
Categories
Find more on Image and Video Ground Truth Labeling 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!