How to save a stabilized video to hard disk?

1 次查看(过去 30 天)
Hi, I used the demo script (run file below) to learn about video stabilization.
% Input video file which needs to be stabilized.
filename = 'shaky_car.avi';
% filename = 'TEST.avi';
hVideoSource = vision.VideoFileReader(filename, ...
'ImageColorSpace', 'Intensity',...
'VideoOutputDataType', 'double');
hTranslate = vision.GeometricTranslator( ...
'OutputSize', 'Same as input image', ...
'OffsetSource', 'Input port');
hTM = vision.TemplateMatcher('ROIInputPort', true, ...
'BestMatchNeighborhoodOutputPort', true);
hShapeInserter = vision.ShapeInserter('BorderColor', 'White');
hTextInserter = vision.TextInserter('(%+05.1f,%+05.1f)', ...
'Location', [191 215], ...
'Color', 1, ...
'FontSize', 16);
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];
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);
fileInfo = info(hVideoSource);
W = fileInfo.VideoSize(1); % Width in pixels
H = fileInfo.VideoSize(2); % 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 = fileInfo.VideoSize;
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;
while ~isDone(hVideoSource)
input = step(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 = step(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 = step(hTranslate, input, fliplr(Offset));
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 = step(hShapeInserter, input, [TargetRect; SearchRegionRect]);
% Display the offset values on the input image
input = step(hTextInserter, input, Offset);
% Display video
step(hVideoOut, [input Stabilized]);
end;
QUESTIONS:
1) I wonder if someone could suggest me how to write a command line to write the stabilized video into a local folder in the computer.
2) Why the file does not run when I substitute shaky_car.avi by TEST.avi. For example,I copied TEST.avi into the same folder as shaky_car.avi and changed the first line of the script accordingly but it generate the following errors:
Error using dspmmfileinfo>getFileInfo (line 82)
Could not add the data source to the filter graph : The specified file was not found.
Error in dspmmfileinfo (line 46)
fileInfo = getFileInfo(filename);
I thank you in advance for your attention and help
Emerson

回答(1 个)

Image Analyst
Image Analyst 2013-9-3
1) You can use VideoWriter to write out your video.
2) Try writing robust code like this:
folder = 'd:\my videos'; % Wherever...
baseFileName = 'shaky_car.avi';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
  1 个评论
Wenting Xie
Wenting Xie 2016-10-24
Hi,
I have the same problems. And I have tried to add the code you write.
However, it just inform me the 'error message' and the video is not saved at the required site.
Is there anything I can try to save the stabilized video.
Thanks a lot.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Computer Vision with Simulink 的更多信息

产品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by