Skip to content

Commit

Permalink
Merge pull request opencv#21931 from victor1234:calib3d-add-undistort…
Browse files Browse the repository at this point in the history
…ImagePoints

Add undistortImagePoints function

* Add undistortImagePoints function
undistortPoints has unclear interface and additional functionality. New function computes only undistorted image points position

* Add undistortImagePoints test

* Add TermCriteria

* Fix layout
  • Loading branch information
victor1234 authored May 12, 2022
1 parent dda9626 commit 2b67bc4
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 0 deletions.
16 changes: 16 additions & 0 deletions modules/calib3d/include/opencv2/calib3d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
#define OPENCV_CALIB3D_HPP

#include "opencv2/core.hpp"
#include "opencv2/core/types.hpp"
#include "opencv2/features2d.hpp"
#include "opencv2/core/affine.hpp"

Expand Down Expand Up @@ -3721,6 +3722,21 @@ void undistortPoints(InputArray src, OutputArray dst,
InputArray cameraMatrix, InputArray distCoeffs,
InputArray R, InputArray P, TermCriteria criteria);

/**
* @brief Compute undistorted image points position
*
* @param src Observed points position, 2xN/Nx2 1-channel or 1xN/Nx1 2-channel (CV_32FC2 or
CV_64FC2) (or vector\<Point2f\> ).
* @param dst Output undistorted points position (1xN/Nx1 2-channel or vector\<Point2f\> ).
* @param cameraMatrix Camera matrix \f$\vecthreethree{f_x}{0}{c_x}{0}{f_y}{c_y}{0}{0}{1}\f$ .
* @param distCoeffs Distortion coefficients
*/
CV_EXPORTS_W
void undistortImagePoints(InputArray src, OutputArray dst, InputArray cameraMatrix,
InputArray distCoeffs,
TermCriteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5,
0.01));

//! @} calib3d

/** @brief The methods in this namespace use a so-called fisheye camera model.
Expand Down
6 changes: 6 additions & 0 deletions modules/calib3d/src/undistort.dispatch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
//
//M*/

#include "opencv2/core/types.hpp"
#include "precomp.hpp"
#include "distortion_model.hpp"

Expand Down Expand Up @@ -607,6 +608,11 @@ void undistortPoints(InputArray _src, OutputArray _dst,
cvUndistortPointsInternal(&_csrc, &_cdst, &_ccameraMatrix, pD, pR, pP, criteria);
}

void undistortImagePoints(InputArray src, OutputArray dst, InputArray cameraMatrix, InputArray distCoeffs, TermCriteria termCriteria)
{
undistortPoints(src, dst, cameraMatrix, distCoeffs, noArray(), cameraMatrix, termCriteria);
}

static Point2f mapPointSpherical(const Point2f& p, float alpha, Vec4d* J, enum UndistortTypes projType)
{
double x = p.x, y = p.y;
Expand Down
38 changes: 38 additions & 0 deletions modules/calib3d/test/test_undistort_points.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// of this distribution and at http://opencv.org/license.html.

#include <opencv2/ts/cuda_test.hpp> // EXPECT_MAT_NEAR
#include "opencv2/core/types.hpp"
#include "test_precomp.hpp"

namespace opencv_test { namespace {
Expand Down Expand Up @@ -97,6 +98,43 @@ TEST_F(UndistortPointsTest, accuracy)
}
}

TEST_F(UndistortPointsTest, undistortImagePointsAccuracy)
{
Mat intrinsics, distCoeffs;
generateCameraMatrix(intrinsics);

vector<Point3f> points(500);
generate3DPointCloud(points);


int modelMembersCount[] = {4,5,8};
for (int idx = 0; idx < 3; idx++)
{
generateDistCoeffs(distCoeffs, modelMembersCount[idx]);

/* Project points with distortion */
vector<Point2f> projectedPoints;
projectPoints(Mat(points), Mat::zeros(3,1,CV_64FC1),
Mat::zeros(3,1,CV_64FC1), intrinsics,
distCoeffs, projectedPoints);

/* Project points without distortion */
vector<Point2f> realUndistortedPoints;
projectPoints(Mat(points), Mat::zeros(3, 1, CV_64FC1),
Mat::zeros(3,1,CV_64FC1), intrinsics,
Mat::zeros(4,1,CV_64FC1), realUndistortedPoints);

/* Undistort points */
Mat undistortedPoints;
TermCriteria termCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, thresh / 2);
undistortImagePoints(Mat(projectedPoints), undistortedPoints, intrinsics, distCoeffs,
termCriteria);

EXPECT_MAT_NEAR(realUndistortedPoints, undistortedPoints.t(), thresh);
}
}


TEST_F(UndistortPointsTest, stop_criteria)
{
Mat cameraMatrix = (Mat_<double>(3,3,CV_64F) << 857.48296979, 0, 968.06224829,
Expand Down

0 comments on commit 2b67bc4

Please sign in to comment.