以防萬一有人可能會發現它很有用的某個時候 - 我來到了這個實現(解決方案主要是基於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
裁剪似乎是一個好主意,我。如果你想知道確切的最大滲透率,你應該首先處理整個視頻。作爲另一種選擇,您可以根據您之前處理的電影的先前知識使用固定的滲透。我建議你試試看。 – m7913d
是的,要知道大小,你需要看整個視頻,沒有圍繞它。 –