主要内容

视频稳定

这个例子显示了如何删除视频相机运动的影响。

介绍

在这个示例中,我们首先定义目标跟踪。在这种情况下,它是一辆汽车的车牌。我们也建立一个动态搜索区域的位置是由最后一个已知的目标位置。然后我们寻找的目标只有在这个搜索区域,从而降低找到目标所需的数量计算。在每个后续视频帧,我们确定目标已经相对于前一帧。我们用这些信息来删除不需要的平移相机运动并生成一个稳定的视频。

初始化

创建一个系统对象™读视频从一个多媒体文件。我们设置的输出强度只有视频。

%输入视频文件需要稳定。文件名=“shaky_car.avi”;hVideoSource = VideoReader(文件名);

创建一个模板匹配器系统对象计算的位置最佳匹配目标的视频帧。我们使用这个位置发现连续的视频帧之间的翻译。

hTM = vision.TemplateMatcher (“ROIInputPort”,真的,“BestMatchNeighborhoodOutputPort”,真正的);

创建一个系统对象来显示原始视频和稳定的视频。

hVideoOut = vision.VideoPlayer (“名字”,“视频稳定”);一轮hVideoOut.Position (1) = (0.4 * hVideoOut.Position (1));一轮hVideoOut.Position (2) = (1.5 * (hVideoOut.Position (2)));hVideoOut.Position (3:4) = (650 - 350);

在这里我们处理循环中使用的一些变量进行初始化。

pos.template_orig = (109 - 100);% (x, y)左上角pos.template_size = [22 18];%(宽高)pos.search_border = 15 [10];%最大水平和垂直位移地板pos.template_center = ((pos.template_size-1) / 2);pos.template_center_pos = (pos.template_orig + pos.template_center - 1);W = hVideoSource.Width;%在像素宽度H = hVideoSource.Height;%的高度以像素为单位(1:pos.search_border BorderCols = (1) + 4 W-pos.search_border (1) + 4: W);BorderRows = [1: pos.search_border (2) + 4 H-pos.search_border (2) + 4: H);深圳= [W H];TargetRowIndices =pos.template_orig (2) 1: pos.template_orig (2) + pos.template_size (2) 2;TargetColIndices =pos.template_orig (1) 1: pos.template_orig (1) + pos.template_size (1) 2;SearchRegion = pos.template_orig - pos.search_border - 1;抵消= [0 0];目标= 0 (18、22);首次= true;

流处理循环

这是主要处理循环使用我们上面实例化的对象稳定输入视频。

hasFrame (hVideoSource)输入= rgb2gray (im2double (readFrame (hVideoSource)));%找到输入视频帧中目标的位置如果首次Idx = int32 (pos.template_center_pos);MotionVector = [0 0];首次= false;其他的IdxPrev = Idx;投资回报率= [SearchRegion pos.template_size + 2 * pos.search_border);Idx = hTM(输入、目标ROI);MotionVector =双(Idx-IdxPrev);结束[抵消,SearchRegion] = updatesearch(深圳、MotionVectorSearchRegion、抵消、pos);%将视频帧来抵消摄像机运动稳定= imtranslate(输入、抵消“线性”);目标=稳定(TargetRowIndices TargetColIndices);%为显示添加黑色边框稳定(:,BorderCols) = 0;稳定(BorderRows:) = 0;(pos TargetRect =。template_orig-Offset pos.template_size);SearchRegionRect = [SearchRegion pos.template_size + 2 * pos.search_border);%画出矩形在输入显示和搜索区域目标输入= insertShape(输入,“矩形”,(TargetRect;SearchRegionRect),“颜色”,“白色”);%显示偏移量(位移)值在输入图像txt = sprintf (”(% f + 05.1, % + 05.1 f)”,抵消);输入= insertText(输入(:,:1)(191 215),三种,“字形大小”,16岁,“输入TextColor”,“白色”,“BoxOpacity”,0);%显示视频hVideoOut([输入(::1)稳定]);结束

结论

使用计算机视觉工具箱™从MATLAB®命令行功能很容易实现复杂系统如视频稳定。

附录

以下在本例使用helper函数。