How can I remove distortion from video files?

Hi--
How can the parameters/outputs from the cameraCalibrator app be applied to video files to remove distortion from mp4 files? I have only been able to successfuly remove distortion from images using the camera parameters and code below. Thank you!
%file name
filename='D:\SideViewCalibration\CalibrationObjectSide2.jpg';
%Coefficients measured from CameraClibrator
IntrinsicMatrix=[987.111820921548,0,0;0,987.149003204452,0;539.716264741029,481.311994646507,1];
radialDistortion = [-0.301068525976252, 0.0836710021489489 ];
cameraParams = cameraParameters('IntrinsicMatrix',IntrinsicMatrix,'RadialDistortion',radialDistortion);
%Open/read file, apply undistortion, and resize to fit in screen/window
I = imread(filename);
J = undistortImage(I,cameraParams);
imshow(J);
imsave;

Answers (1)

A video file can be undistorted by looping through each frame and undistorting them . vision.VideoFileReader() function will create the video file reader. vision.VideoPlayer() sets the player window. vision.VideoFileWriter sets the video file writer variable. Using these you can undistort the video file.
This sample code will provide a better understanding.
vfrUndist = vision.VideoFileReader(videoFile); % Video File Variable
vpUndist = vision.VideoPlayer(required name value arguments); % Player
vfwUdist = vision.VideoFileWriter(required name-value arguments); % video file writer variable
% Looping through each frame and undistorting each frame
while ~isDone(vfrUndist)
frame_undist = step(vfrUndist);
frame = undistortImage(frame_undist,cameraParams);
% write to the video file
step(vfwUndist, frame);
end
release(vpUndist);
release(vfrUndist);
release(vfwUdist);

3 Comments

EGR
EGR on 5 Aug 2019
Edited: EGR on 5 Aug 2019
Hi--
Thank you for the feedback. I have two followup questions:
  1. What does "required name value arguments" refer to? I'm not entirely sure what to write for vision.VideoPlayer and vision.VideoFileWriter
  2. How can the undistorted videos be displayed?
For vision.VideoPlayer, vision.VideoFileWriter will give you indepth understanding.
For example
vpUndist = vision.VideoPlayer('Position', [100, 100, 1280, 720]);
vfwUdist = vision.VideoFileWriter(NewFileName, 'FrameRateAs ',...
videoFileReaderUNDIST.info.VideoFrameRate, 'FileFormat', 'MPEG4');
The final video can be played on on any player that supports the video codec
Hope this helps!!!
Hello, I had the same problem and tried your code
vfrUndist = vision.VideoFileReader('Filename.mp4'); % Video File Variable
vpUndist = vision.VideoPlayer('Position', [100, 100, 1280, 720]); % Player
vfwUdist = vision.VideoFileWriter('NewFileName', 'FrameRateAs ',...
videoFileReaderUNDIST.info.VideoFrameRate, 'FileFormat', 'MPEG4');
% Looping through each frame and undistorting each frame
while ~isDone(vfrUndist)
frame_undist = step(vfrUndist);
frame = undistortImage(frame_undist,cameraParams);
% write to the video file
step(vfwUndist, frame);
end
release(vpUndist);
release(vfrUndist);
release(vfwUndist);
But there is always a mistake in the vfwUdist line, Matlab is unable to resolve the name "videoFileReaderUNDIST.info.VideoFrameRate".
should i add here something?
if i remove anything form this line there is an other mistake, that there are to many value inputs.
Furthermore there is a problem with the video input, i took pictures and videos with the same camera, but they are not the same size, how can i fix this?

Sign in to comment.

Asked:

EGR
on 23 Jul 2019

Commented:

on 15 Dec 2020

Community Treasure Hunt

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

Start Hunting!