我是光学流程和计算机视觉的新手,我开始使用Matlab的一个简单的演示示例。
它的目的是使用视频并将运动矢量绘制到屏幕上。我使用以下代码:
%% 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);
我不知道为什么矢量线没有正确绘图。
任何人都可以帮助我吗?
由于
PS:结果如下:
答案 0 :(得分:1)
尝试使用
lines = [Rv(:); Cv(:); Rv(:)+H(:); Cv(:)+V(:)];
而不是
lines = [Rv;Cv; Rv+H(:)'; Cv+V(:)'];
更好的是,如果您有最新版本的Matlab,请尝试使用insertShape
函数代替vision.ShapeInserter
。
修改强>
如果您有最新版本的计算机视觉系统工具箱,请尝试新的光流功能:opticalFlowHS
,opticalFlowLK
,opticalFlowLKDoG
和opticalFlowFarneback
。