MATLAB Answers

How can I remove distortion from video files?

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

  0 Comments

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);
end
release(vpUndist);
release(vfrUndist);
release(vfwUdist);

  2 Comments

EGR
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?
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 ',...
videoFileReaderUNDIST.info.VideoFrameRate, 'FileFormat', 'MPEG4');
The final video can be played on on any player that supports the video codec
Hope this helps!!!

Sign in to comment.