Main Content

undistortPoints

Correct point coordinates for lens distortion

Description

undistortedPoints = undistortPoints(points,cameraParams) returns point coordinates corrected for lens distortion. This function uses numeric nonlinear least-squares optimization.

example

[undistortedPoints,reprojectionErrors] = undistortPoints(points,cameraParams) additionally returns the errors used to evaluate the accuracy of undistorted points.

Examples

collapse all

Create an imageDatastore object containing calibration images.

images = imageDatastore(fullfile(toolboxdir('vision'),'visiondata', ...
   'calibration','mono'));
imageFileNames = images.Files;

Detect the calibration pattern.

[imagePoints,boardSize] = detectCheckerboardPoints(imageFileNames);

Generate world coordinates of the corners of the squares. The square size is in millimeters.

squareSize = 29; 
worldPoints = generateCheckerboardPoints(boardSize,squareSize);

Calibrate the camera.

I = readimage(images,10); 
imageSize = [size(I, 1), size(I, 2)];
params = estimateCameraParameters(imagePoints,worldPoints, ...
                                  'ImageSize',imageSize);

Load an image and detect the checkerboard points.

points = detectCheckerboardPoints(I);

Undistort the points

undistortedPoints = undistortPoints(points,params);

Undistort the image.

[J, newOrigin] = undistortImage(I,params,'OutputView','full');

Translate undistorted points

undistortedPoints = [undistortedPoints(:,1) - newOrigin(1), ...
                    undistortedPoints(:,2) - newOrigin(2)];

Display the results

figure; 
imshow(I); 
hold on;
plot(points(:,1),points(:,2),'r*-');
title('Detected Points'); 
hold off;

Figure contains an axes object. The axes object with title Detected Points contains 2 objects of type image, line.

figure; 
imshow(J); 
hold on;
plot(undistortedPoints(:,1),undistortedPoints(:,2),'g*-');
title('Undistorted Points'); 
hold off;

Figure contains an axes object. The axes object with title Undistorted Points contains 2 objects of type image, line.

Input Arguments

collapse all

Input points, specified an M-by-2 matrix of M number of [x y] coordinates.

Camera parameters, specified as a cameraParameters or cameraIntrinsics object. You can return the cameraParameters object using the estimateCameraParameters function. The cameraParameters object contains the intrinsic, extrinsic, and lens distortion parameters of a camera.

Output Arguments

collapse all

Undistorted points, returned as an M-by-2 matrix. The undistortedPoints output contains M [x,y] point coordinates corrected for lens distortion. When you input points as double, the function outputs undistortedPoints as double. Otherwise, it outputs undistortedPoints as single.

Data Types: single | double

Reprojection errors, returned as an M-by-1 vector. You can use the errors to evaluate the accuracy of undistorted points. The function computes the errors by applying distortion to the undistorted points, and then taking the distances between the result and the corresponding input points. The reprojectionErrors output is in pixels.

Extended Capabilities

C/C++ Code Generation
Generate C and C++ code using MATLAB® Coder™.

Version History

Introduced in R2014b