Calculate Car speed from a video stream

조회 수: 4 (최근 30일)
Chadi St
Chadi St 2019년 1월 11일
댓글: AKASH KUMAR SWAIN 2022년 9월 4일
Hi , i'm trying to calculate an estimation of the car speed using the following code , However it gives me the same speed for every car:
foregroundDetector = vision.ForegroundDetector('NumGaussians', 3, ...
'NumTrainingFrames', 50);
V=VideoReader('visiontraffic.avi');
videoReader = vision.VideoFileReader('visiontraffic.avi');
for i = 1:150
frame = step(videoReader); % read the next video frame
foreground = step(foregroundDetector, frame);
end
imwrite(frame,'D:\referenceimage.jpg','jpg');
videoPlayer = vision.VideoPlayer('Name', 'Detected Cars');
videoPlayer.Position(3:4) = [650,400]; % window size: [width, height]
se = strel('square', 3); % morphological filter for noise removal
referenceimage=imread('D:\referenceimage.jpg');
X=zeros(2,121);
Y=zeros(2,121);
Z=zeros;
while ~isDone(videoReader)
frame = step(videoReader); % read the next video frame
% Detect the foreground in the current video frame
foreground = step(foregroundDetector, frame);
% Use morphological opening to remove noise in the foreground
filteredForeground = imopen(foreground, se);
%-----------------------SPEED ---------------------------%
frame2=((im2double(frame))-(im2double(referenceimage)));
frame1=im2bw(frame2,0.1);
[Labelimage]=bwlabeln(frame1);
stats=regionprops(Labelimage,'basic');
BB=stats.BoundingBox;
i=2; %fblasst for
X(i)=BB(1);
Y(i)=BB(2);
Dist=((X(i)-X(i-1))^2+(Y(i)-Y(i-1))^2)^(1/2);
Z(i)=Dist;
M=median(Z);
%disp(M);
%clc;
%disp('speed=')
Speed=((M)*(120/8))/(4);
%disp(Speed);
%SPEED = M ???????????
i = i + 1;
SE = strel('disk',6);
frame3=imclose(frame1,SE);
step(videoReader);
pause(0.05);
%if(i==121) end; ??
%-----------------------SPEED ---------------------------%
% Detect the connected components with the specified minimum area, and
% compute their bounding boxes
blobAnalysis = vision.BlobAnalysis('BoundingBoxOutputPort', true, ...
'AreaOutputPort', true, 'CentroidOutputPort', true, ...
'MinimumBlobArea', 150);
%bbox = step(blobAnalysis, filteredForeground);
[areas, centroids, bbox] = step(blobAnalysis, filteredForeground);
% Draw bounding boxes around the detected cars
result = insertShape(frame, 'Rectangle', bbox, 'Color', 'green');
% Display the number of cars found in the video frame
%ICI
%disp(centroids); //show...
%disp(size(centroids));//show....
numCars = size(bbox, 1); %cars ...
numCars_str = ['car number:', num2str(size(bbox, 1))];
speed_str = [num2str(Speed),'KM/h'];
result = insertText(result,[10,10],numCars_str, 'BoxOpacity', 1, ...
'FontSize', 14);
for i=1:size(bbox, 1)
result = insertText(result,[centroids(i,1),centroids(i,2)], speed_str, 'BoxOpacity', 1, ...
'FontSize', 20);
end;
step(videoPlayer, result); % display the results
end;
release(videoReader); % close the video file
  댓글 수: 2
Ameni chetouane
Ameni chetouane 2019년 9월 6일
I have the same error, could you please help me
AKASH KUMAR SWAIN
AKASH KUMAR SWAIN 2022년 9월 4일
this code is detecting multiple objects falsely
please help me with this

댓글을 달려면 로그인하십시오.

답변 (1개)

Sandhiya CSE
Sandhiya CSE 2020년 5월 15일
Can someone send the input video for this code

카테고리

Help CenterFile Exchange에서 Tracking and Motion Estimation에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by