Tracking cars in a video using Optical flow. Vectors not displaying properly on the image

2 views (last 30 days)
Hi I am new to computer vision and optical flow in general. So I am basically trying to plot motion vectors on moving vehicles in a video. It is based on one of the MATLAB tutorial for computer vision. So the problem I am having is that the vectors are not plotting properly. Can anyone help me?
if true
% code
end
Here is the code:
%%initialization
close all
clear all
% Create reader
reader = vision.VideoFileReader; reader.Filename = 'viptraffic.avi';
% Create viewer
viewer = vision.DeployableVideoPlayer;
%%viewer.FrameRate = 10;
%Create Optical Flow
optical = vision.OpticalFlow; %how pixels are moving from one frame to the next
optical.OutputValue = 'Horizontal and vertical components in complex form'; %will allow us to draw a vector
%%%on the vision so that we see how the pixels are moving from one frame to the next
%%We pass the horizontal and vertical components to the shape inserter
%%below
% Display vector fields
shapes = vision.ShapeInserter;
shapes.Shape = 'Lines';
shapes.BorderColor = 'white';
R = 1:4:120;%%downsample the optical flow field
C = 1:4:160;%%downsample the optical flow field
[Cv, Rv] = meshgrid (C, R); %%%display a grid on the image and take every fourth value
Rv = Rv(:)';
Cv = Cv(:)';
%%Execution
reset(reader)
%Set up for stream
while ~isDone(reader)
I = step(reader);
of = step(optical,rgb2gray(I));
size(of)
ofd = of(R,C);
size(ofd)
H = imag(ofd)*20;
V = real(ofd)*20;
%Draw lines on top of image
lines = [Rv;Cv; Rv+H(:)'; Cv+V(:)']; %%start and a finish , start+movement, end+movement
% lines = [Cv;Rv;Cv;Rv];
Ishp = step(shapes,I,lines);
step(viewer,Ishp);
end release(viewer);

Answers (0)

Community Treasure Hunt

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

Start Hunting!