以防万一有人在某个时候发现它有用 - 我想出了这个实现(解决方案主要基于 this 答案(最后一部分),它回答了关于单个图像的类似问题......) :
function [] = stabilizeVideo(fileName)
% open video reader and writer
InputVid = vision.VideoFileReader(fileName);
StabilizedVid = vision.VideoFileWriter('StabilizedVidWithArtifacts.avi');
leftPent = 0;
upPent = 0;
[downPent, rightPent] = size(imgB);
% any initialization you might need...
while ~isDone(InputVid)
% Stabilize frame... the warped frame is called 'imgBp'
% write the warped frame to the video
step(StabilizedVid,imgBp);
% now the magic happens:
[up, down, left, right] = FindMaxArtifactPenetration(imgBp);
leftPent = max(left, leftPent);
rightPent = min(right, rightPent);
upPent = max(up, upPent);
downPent = min(down, downPent);
end
release(InputVid);
release(StabilizedVid);
cropArtifacts(upPent, downPent, leftPent, rightPent);
display(num2str(toc));
end
我写了那些辅助函数:
function [up, down, left, right] = FindMaxArtifactPenetration(I)
% allow RGB or grayscale image
if size(I,3)==3
I1 = rgb2gray(I);
else
I1 = I;
end
nonZeroCols = find(any(I1)); % find non-zero cols
left = min(nonZeroCols);
right = max(nonZeroCols);
I2 = I1(:, left : right, :);
nonZeroRows = find(any(I2, 2)); % find non-zero rows
up = min(nonZeroRows);
down = max(nonZeroRows);
end
function [] = cropArtifacts(minRow, maxRow, minCol, maxCol)
% reload video with artifacts, crop every frame and save it in the
% new video
InputVid = vision.VideoFileReader('StabilizedVidWithArtifacts.avi');
StabilizedVid = vision.VideoFileWriter('StabilizedVid.avi');
while ~isDone(InputVid)
frame = step(InputVid);
step(StabilizedVid, frame(minRow:maxRow, minCol:maxCol, :));
end
release(InputVid);
release(StabilizedVid);
end