undistortPoints()函数用法总结

undistortPoints()函数用法总结

  函数调用:C++: void undistortPoints(InputArray src, OutputArray dst, InputArray cameraMatrix, InputArray distCoeffs, InputArrayR=noArray(), InputArray P=noArray())
  参数说明:
undistortPoints()函数用法总结_第1张图片
  特别说明:
undistortPoints()函数用法总结_第2张图片
  实现功能:通过旋转平移变换,将观察点转换到理想的点坐标下。
  用法举例:

  bool CoreAlgorithm::findPntsWithTagVStrong(vector ¢erPnt, vector<float>& longaxisRadius, vector& tagPnts, float &firstFeaturelength, const double gamma, Mat cameraMatrix,double angle)
{
    /////////////////////////////added by QiYong///////////////////////////////////
    //////利用undistortPoints()函数将拍摄的图像矫正为正常的视角,便于检测。
    if (centerPnt.size() != 8)
    {
        return false;
    }
    vector  DistortCenterPnt;
    double k1 = 0;
    double k2 = 0;
    double k3 = 0;
    double k4 = 0;
    vector<double> Distort_Coefficients;
    Distort_Coefficients.push_back(k1);
    Distort_Coefficients.push_back(k2);
    Distort_Coefficients.push_back(k3);
    Distort_Coefficients.push_back(k4);
    Mat Rx = (Mat_<double>(3, 3) << 1, 0, 0, 0, cos(angle), -sin(angle), 0, sin(angle), cos(angle));
    Mat Ry = (Mat_<double>(3, 3) << cos(angle), 0, sin(angle), 0, 1, 0, -sin(angle), 0, cos(angle));
    Mat Rz = (Mat_<double>(3, 3) << cos(angle), -sin(angle), 0, sin(angle), cos(angle), 0, 0, 0, 1);
    Mat R = Rx*Ry;
    undistortPoints(centerPnt, DistortCenterPnt, cameraMatrix, Distort_Coefficients, R);
**///////////需要将变换后的点先变化为齐次坐标系,然后还需要乘以相机参数矩阵,才是最终的变化的坐标。**
    Mat centerPntsThreeCols = Mat::zeros(8, 3, CV_64FC1);
    for (size_t i = 0; i < centerPnt.size(); i++)
    {
        centerPntsThreeCols.at<double>(i, 0) = DistortCenterPnt[i].x;
        centerPntsThreeCols.at<double>(i, 1) = DistortCenterPnt[i].y;
        centerPntsThreeCols.at<double>(i, 2) = 1;
    }
    Mat undistortCenterThreeCols = cameraMatrix*centerPntsThreeCols.t();
    vector undistortCenterPnts;
    for (size_t i = 0; i < centerPnt.size(); i++)
    {
        Point2f Point = Point2f(undistortCenterThreeCols.at<double>(0, i), undistortCenterThreeCols.at<double>(1, i));
        undistortCenterPnts.push_back(Point);
    }
    Mat img1 = Mat::zeros(1500, 1500, CV_64FC1);
    Mat img2 = Mat::zeros(1500, 1500, CV_64FC1);
    for (size_t i = 0; i < centerPnt.size(); i++)
    {
        DrawEllipse(img1, centerPnt[i].x, centerPnt[i].y, 5, 5, 0);
        DrawEllipse(img2, undistortCenterPnts[i].x -500, undistortCenterPnts[i].y, 5, 5, 0);
    }
    namedWindow("源中心点", 2);
    namedWindow("矫正后的中心点", 2);
    imshow("源中心点", img1);
    imshow("矫正后的中心点", img2);
    waitKey();
    imwrite("D:/1.bmp", img1);
    imwrite("D:/2.bmp", img2);
}

  程序运行结果:
  原来的中心点排布如下:
undistortPoints()函数用法总结_第3张图片

  经过undistortPoints()旋转变换后的中心点排布如下:`

undistortPoints()函数用法总结_第4张图片

  注:代码中加粗的部分为常常会犯错误的地方。

  个人公众号:

这里写图片描述

你可能感兴趣的:(3DCVer,OPENCV,c++)