主要内容

视频稳定

这个例子展示了如何从视频流中删除摄像机运动的影响。

简介

在本例中,我们首先定义要跟踪的目标。在这种情况下,它是一辆车的后面和车牌。我们还建立了一个动态搜索区域,其位置由最后一个已知目标位置确定。然后,我们只在这个搜索区域内搜索目标,这减少了寻找目标所需的计算量。在随后的每一帧视频中,我们确定目标相对于前一帧移动了多少。我们使用这些信息来删除不需要的平移摄像机运动,并生成稳定的视频。

初始化

创建一个System对象™来从多媒体文件中读取视频。我们将输出设置为只有视频的强度。

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

创建一个模板匹配器系统对象来计算视频帧中目标的最佳匹配位置。我们使用这个位置来查找连续视频帧之间的转换。

视觉。TemplateMatcher (“ROIInputPort”,真的,...“BestMatchNeighborhoodOutputPort”,真正的);

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

hVideoOut =视觉。放像机(“名称”“视频稳定”);hVideoOut.Position(1) = round(0.4*hVideoOut.Position(1));hVideoOut.Position(2) = round(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 = floor((pos.template_size-1)/2);Pos.template_center_pos = (pos.template_orig + pos.template_center - 1);W = hVideoSource.Width;%宽度(像素)H = hVideoSource.Height;高度百分比(像素)border = [1:pos.search_border(1)+4 W-pos.search_border(1)+4:W];BorderRows = [1:pos.search_border(2)+4 H-pos.search_border(2)+4:H];sz = [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;搜索区域= pos.template_orig - pos.search_border - 1;Offset = [0 0];目标= 0 (18,22);firstTime = true;

流处理循环

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

hasFrame(hVideoSource) input = im2gray(im2double(readFrame(hVideoSource)));查找目标在输入视频帧中的位置如果firstTime Idx = int32(pos.template_center_pos);运动矢量= [0 0];firstTime = false;其他的IdxPrev = Idx;ROI = [SearchRegion, pos.template_size+2*pos.search_border];Idx = hTM(输入,目标,ROI);MotionVector = double(Idx-IdxPrev);结束[偏移量,搜索区域]= updatesearch(sz,运动矢量,...搜索区域,偏移量,pos);翻译视频帧以抵消相机运动稳定= imtranslate(输入,偏移量,“线性”);目标=稳定(TargetRowIndices, TargetColIndices);为显示添加黑色边框稳定(:,BorderCols) = 0;稳定(BorderRows,:) = 0;TargetRect = [pos. template_origin - offset, pos.template_size];SearchRegionRect = [SearchRegion, pos.template_size + 2*pos.search_border];在输入上绘制矩形以显示目标和搜索区域input = insertShape(输入,“矩形”, (TargetRect;SearchRegionRect),...“颜色”“白色”);显示输入图像上的偏移(位移)值TXT = sprintf(”(% f + 05.1, % + 05.1 f)”,抵消);input = insertText(input(:,:,1),[191 215],txt,“字形大小”, 16岁,...“输入TextColor”“白色”“BoxOpacity”, 0);显示视频hVideoOut([输入(::1)稳定]);结束

结论

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

附录

本例中使用了以下helper函数。