MATLAB Answers

How can I remove distortion from video files?

14 views (last 30 days)
EGR on 23 Jul 2019
Edited: Sai Bhargav Avula on 6 Aug 2019
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
%Coefficients measured from CameraClibrator
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);


Sign in to comment.

Answers (1)

Sai Bhargav Avula
Sai Bhargav Avula on 2 Aug 2019
Edited: Sai Bhargav Avula on 2 Aug 2019
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);


EGR on 5 Aug 2019
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?
Sai Bhargav Avula
Sai Bhargav Avula on 6 Aug 2019
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 ',..., 'FileFormat', 'MPEG4');
The final video can be played on on any player that supports the video codec
Hope this helps!!!

Sign in to comment.