Attempting to account for lens distortion using " undistortedImage = undistortImage(originalIMage, cameraParams)
6 views (last 30 days)
Show older comments
I am using the MATLAB Camera Calibtion App. and am attempting to mitgate lens distortion via post processing.
% Auto-generated by cameraCalibrator app on 01-May-2024
%-------------------------------------------------------
% Define images to process
imageFileNames = {'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\0 deg 2024-04-30 17-35-57.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\5 deg w 5 deg tilt 2024-04-30 17-37-59.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\15 deg w 5 deg tilt 2024-04-30 17-38-56.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\23 deg w 5 deg tilt 2024-04-30 17-40-03.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\30 deg w 5 deg tilt 2024-04-30 17-41-40.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\38 deg w 5 deg tilt 2024-04-30 17-42-51.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\neg 6 deg w neg 15 deg tilt 2024-04-30 17-48-28.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\neg 13 deg w neg 15 deg tilt 2024-04-30 17-50-12.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\neg 20 deg w neg 15 deg tilt 2024-04-30 17-51-04.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\neg 27 deg w neg 15 deg tilt 2024-04-30 17-51-46.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\neg 33 deg w neg 15 deg tilt 2024-04-30 17-52-37.png',...
'F:\Programs\ARES\IHSS Program\Pictures\Lens Distortion Mapping\IMX323 SN 0001\neg 39 deg w neg 15 deg tilt 2024-04-30 17-53-44.png',...
};
% Detect checkerboards in images
[imagePoints, boardSize, imagesUsed] = detectCheckerboardPoints(imageFileNames);
imageFileNames = imageFileNames(imagesUsed);
% Read the first image to obtain image size
originalImage = imread(imageFileNames{1});
[mrows, ncols, ~] = size(originalImage);
% Generate world coordinates of the corners of the squares
squareSize = 1.270000e+01; % in units of 'millimeters'
worldPoints = generateCheckerboardPoints(boardSize, squareSize);
% Calibrate the camera
[cameraParams, imagesUsed, estimationErrors] = estimateCameraParameters(imagePoints, worldPoints, ...
'EstimateSkew', true, 'EstimateTangentialDistortion', true, ...
'NumRadialDistortionCoefficients', 3, 'WorldUnits', 'millimeters', ...
'InitialIntrinsicMatrix', [], 'InitialRadialDistortion', [], ...
'ImageSize', [mrows, ncols]);
% View reprojection errors
h1=figure; showReprojectionErrors(cameraParams);
% Visualize pattern locations
h2=figure; showExtrinsics(cameraParams, 'CameraCentric');
% Display parameter estimation errors
displayErrors(estimationErrors, cameraParams);
% For example, you can use the calibration data to remove effects of lens distortion.
undistortedImage = undistortImage(originalImage, cameraParams);
% See additional examples of how to use the calibration data. At the prompt type:
% showdemo('MeasuringPlanarObjectsExample')
% showdemo('StructureFromMotionExample')
JUst not sure how to get from is Highlghted code above to an answer. Is it just a subtraction of cameraParams - original image that wil tell us how to adjust for distotion in post processed maner please??
2 Comments
Image Analyst
on 1 May 2024
If you have any more questions, then attach your images with the paperclip icon after you read this:
Answers (1)
Aastha
on 2 Sep 2024
Hi Fred,
I understand that you want to use camera parameters estimated with the help of MATLAB Image Processing Toolbox Camera Calibration Routine to mitigate lens distortion using the “undistortImage” function.
Please follow the steps mentioned below to remove lens distortion from the image:
1. Estimate Camera Parameters: You can use the “estimateCameraParameters” function to calibrate the camera and obtain camera parameters. Here is a sample snippet for the same:
[cameraParams, imagesUsed, estimationErrors] = estimateCameraParameters(imagePoints, worldPoints, ...
'EstimateSkew', true, 'EstimateTangentialDistortion', true, ...
'NumRadialDistortionCoefficients', 3, 'WorldUnits', 'millimeters', ...
'InitialIntrinsicMatrix', [], 'InitialRadialDistortion', [], ...
'ImageSize', [mrows, ncols]);
The “cameraParams” variable is a “cameraParameters” object that stores the intrinsic, extrinsic, and lens distortion parameters of the camera. You may refer to the following MathWorks documentation for more information on “cameraParameters” object:
2. Remove lens distortion: Once you have the “cameraParams” object from the calibration routine, you can use it to correct lens distortion in images. The “undistortImage” function is used for this purpose. Here is an example code on how to use the “undistortImage” function:
J = undistortImage(I, cameraParams);
J is the corrected image, I is the original image and “cameraParams” is the camera parameter object. For more information on “undistortImage” function, you may refer to the MathWorks documentation link below:
3. Further, you can use the “psnr” function as a metric to calculate the amount of distortion that was present in the original image I with respect to the undistorted image J. The code snippet below demonstrates on how to use “psnr” function:
distortion_present = psnr(I, J);
J is the corrected image, I is the original image and "cameraParams" is the camera parameter object. The function “psnr” calculates the peak signal-to-noise ratio for the image I, with the image J as the reference. A greater value of PSNR signifies better image quality.
For additional information on “psnr” function, you may refer to the link below:
I hope this helps!
0 Comments
See Also
Categories
Find more on Camera Calibration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!