【视频处理】视频中标记目标,根据目标偏移移动每一帧位置,达到去抖动效果

 matlab系统自带工具箱程序,传入自己的视频需要调参。完整代码可以在链接 下载。

clear;clc;close all;
% Input video file which needs to be stabilized.
filename = 'shaky_car.avi';
% filename = 'VID_20230525_181020.mp4';
hVideoSource = VideoReader(filename);
% Create a template matcher System object to compute the location of the best match of the target in the video frame. We use this location to find translation between successive video frames.
hTM = vision.TemplateMatcher('ROIInputPort', true, 'BestMatchNeighborhoodOutputPort', true);
% Create a System object to display the original video and the stabilized video.
hVideoOut = vision.VideoPlayer('Name', 'Video Stabilization');
hVideoOut.Position(1) = round(0.4*hVideoOut.Position(1));
hVideoOut.Position(2) = round(1.5*(hVideoOut.Position(2)));
hVideoOut.Position(3:4) = [650 350];
% hVideoOut.Position(1:2:3:4) = [1000 1000 1000 1000];
% Here we initialize some variables used in the processing loop.
pos.template_orig = [109 100]; % [x y] upper left corner
pos.template_size = [22 18]; % [width height]
pos.search_border = [15 10]; % max horizontal and vertical displacement
pos.template_center = floor((pos.template_size-1)/2);
pos.template_center_pos = (pos.template_orig + pos.template_center - 1);
W = hVideoSource.Width; % Width in pixels
H = hVideoSource.Height; % Height in pixels
BorderCols = [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;
SearchRegion = pos.template_orig - pos.search_border - 1;
Offset = [0 0];
Target = zeros(18,22);
firstTime = true;
% Stream Processing Loop
% This is the main processing loop which uses the objects we instantiated above to stabilize the input video.
% while hasFrame(hVideoSource)
fps = hVideoSource.FrameRate;
out = VideoWriter(filename+"out.mp4");
out.FrameRate = fps;
open(out);
while hasFrame(hVideoSource)
input = rgb2gray(im2double(readFrame(hVideoSource)));
% Find location of Target in the input video frame
if firstTime
Idx = int32(pos.template_center_pos);
MotionVector = [0 0];
firstTime = false;
else
IdxPrev = Idx;
ROI = [SearchRegion, pos.template_size+2*pos.search_border];
Idx = hTM(input,Target,ROI);
MotionVector = double(Idx-IdxPrev);
end
[Offset, SearchRegion] = updatesearch(sz, MotionVector, SearchRegion, Offset, pos);
% Translate video frame to offset the camera motion
Stabilized = imtranslate(input, Offset, 'linear');
Target = Stabilized(TargetRowIndices, TargetColIndices);
% Add black border for display
Stabilized(:, BorderCols) = 0;
Stabilized(BorderRows, :) = 0;
TargetRect = [pos.template_orig-Offset, pos.template_size];
SearchRegionRect = [SearchRegion, pos.template_size + 2*pos.search_border];
% Draw rectangles on input to show target and search region
input = insertShape(input, 'Rectangle', [TargetRect; SearchRegionRect],'Color', 'white');
% Display the offset (displacement) values on the input image
txt = sprintf('(%+05.1f,%+05.1f)', Offset);
input = insertText(input(:,:,1),[191 215],txt,'FontSize',16,'TextColor', 'white', 'BoxOpacity', 0);
% Display video
hVideoOut([input(:,:,1) Stabilized]);
writeVideo(out, Stabilized);
end
close(out);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值