%% This script is used to correct video files for lens distortion using % camera parameters found using matlabs camera calibrator application it % reads in the the camera parameters variable "cameraParams" from a loaded % matlab workspace "CameraParameters.mat" then uses a loop that reads in % the video file and corrects the frames one at a time, then saves them to % a new video file. %% setup % Pass in the camera parameters varibale from a saved work space load CameraParameters.mat % opens up user interface to select video file filename = uigetfile('', 'select video file'); % gets input file name fileVideoUNDIST = filename; % prompts the user to name the new video file NewFileName = uiputfile; % **********************NOTE*********************************************** % you MUST use the .mp4 extension when you enter the name of the new file % to be saved i.e. " newfile.mp4 " % ************************************************************************* % Video file reader variable videoFileReaderUNDIST = vision.VideoFileReader(fileVideoUNDIST); % Player window videoPlayerUNDIST = vision.VideoPlayer('Position', [100, 100, 1280, 720]); % reads in the first frame of the video file objectFrameUNDIST = step(videoFileReaderUNDIST); % sets up the video file writer variable and its parameters videoFileWriterUNDIST = vision.VideoFileWriter(NewFileName, 'FrameRate',... videoFileReaderUNDIST.info.VideoFrameRate, 'FileFormat', 'MPEG4'); %% loop to read in video frame, pass it into the undistort function, and % save as a new video file while ~isDone(videoFileReaderUNDIST) % reads in frame from video file framePRE_UNDIST = step(videoFileReaderUNDIST); % uses the camera paramters to undistort the image framePOST_UNDIST = undistortImage(framePRE_UNDIST, cameraParams); % writes to video file step(videoFileWriterUNDIST, framePOST_UNDIST); end %% release video reader/player release(videoPlayerUNDIST); release(videoFileReaderUNDIST); release(videoFileWriterUNDIST); %% finish msgbox('conversion Complete')