How to calculate & display the velocity of moving objects when they are being tracked?

9 views (last 30 days)
*I want to calculate the velocity of each moving vehicles/objects in a video. I have pasted the code below and want to add the velocity measuring feature also. I've heard of vision.OpticalFlow but don't know how to apply it. So I need help here. Thank u!! _*
filename = 'test3.mp4'; hvfr = vision.VideoFileReader(filename, 'ImageColorSpace', 'RGB');
hcsc = vision.ColorSpaceConverter('Conversion', 'RGB to intensity');
hfdet = vision.ForegroundDetector(... 'NumTrainingFrames', 20, ... % only 5 because of short video 'InitialVariance', (30/255)^2); % initial standard deviation of 30/255
hblob = vision.BlobAnalysis( ... 'CentroidOutputPort', false, ... 'AreaOutputPort', true, ... 'BoundingBoxOutputPort', true, ... 'OutputDataType', 'single', ... 'MinimumBlobArea', 250, ... 'MaximumBlobArea', 3600, ... 'MaximumCount', 80);
hshapeins = vision.ShapeInserter( ... 'BorderColor', 'Custom', ... 'CustomBorderColor', [0 255 0]);
htextins = vision.TextInserter( ... 'Text', '%4d', ... 'Location', [1 1], ... 'Color', [255 255 255], ... 'FontSize', 12);
sz = get(0,'ScreenSize'); pos = [20 sz(4)-300 200 200]; hVideoOrig = vision.VideoPlayer('Name', 'Original', 'Position', pos); pos(1) = pos(1)+220; % move the next viewer to the right hVideoFg = vision.VideoPlayer('Name', 'Foreground', 'Position', pos); pos(1) = pos(1)+220; hVideoRes = vision.VideoPlayer('Name', 'Results', 'Position', pos);
line_row = 23; % Define region of interest (ROI)
while ~isDone(hvfr) image = step(hvfr); % Read input video frame y = step(hcsc, image); % Convert color image to intensity
y = y-mean(y(:));
fg_image = step(hfdet, y); % Detect foreground
[area, bbox] = step(hblob, fg_image);
image_out = image;
image_out(22:23,:,:) = 255; % Count cars only below this white line
image_out(1:15,1:30,:) = 0; % Black background for displaying count
Idx = bbox(:,2) > line_row; % Select boxes which are in the ROI.
% Based on dimensions, exclude objects which are not cars. When the
% ratio between the area of the blob and the area of the bounding box
% is above 0.4 (40%) classify it as a car.
ratio = zeros(length(Idx),1);
ratio(Idx) = single(area(Idx,1))./single(bbox(Idx,3).*bbox(Idx,4));
ratiob = ratio > 0.4;
count = int32(sum(ratiob)); % Number of cars
bbox(~ratiob,:) = int32(-1);
image_out = step(hshapeins, image_out, bbox);
image_out = step(htextins, image_out, count);
step(hVideoOrig, image); % Original video
step(hVideoFg, fg_image); % Foreground
step(hVideoRes, image_out); % Bounding boxes around cars
end
% Close the video file release(hvfr);

Answers (1)

Dima Lisin
Dima Lisin on 26 Nov 2014
You can calculate velocity in pixels per frame by calculating the displacement of each object's centroid between two frames... If you want the velocity in meters per second or miles per hour, then you would need more information, such as your frame rate, the distance from the camera to the road, the angle of the camera, and the focal length of the lens. Ideally, you would want to calibrate your camera.
  1 Comment
mr viper
mr viper on 27 Nov 2014
Could you please write a additional program which could be integrated on above mentioned code assuming your own data or any conditions? I'd be grateful to you.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!