opencv undistortpoints和MYundistortpoints有不同的结果

时间:2014-07-18 09:09:16

标签: c++ opencv

我有一个关于openCV方法undistortpoints的问题。在我的应用程序中,我不能使用openCV,因此有必要编写我自己的undistortpoints方法版本。

问题是,我的版本计算其他结果为openCV版本。我试图找到错误,但我不知道我能做什么。

这可能与数据类型或其他内容不同吗?

我希望你能帮助我!

以下代码:

#include "opencv2\core\core.hpp"
#include "opencv2\highgui\highgui.hpp"
#include "opencv2\imgproc\imgproc.hpp"
#include <iostream>
#include "opencv2\opencv.hpp"
#include <stdio.h>
#include <math.h>

void MYundistortPoints(const Mat & point, Mat & out, const Mat & k, const Mat & kc)
{

  double u = point.at<double>(0, 0);
  double v = point.at<double>(0, 1);

  double k1 = kc.at<double>(0, 0);
  double k2 = kc.at<double>(0, 1);
  double p1 = kc.at<double>(0, 2);
  double p2 = kc.at<double>(0, 3);
  double k3 = kc.at<double>(0, 4);

  double fx = k.at<double>(0, 0);
  double fy = k.at<double>(1, 1);

  double nfx = 1.0 / fx; //fx normalisiert
  double nfy = 1.0 / fy; //fy pow

  double u0 = k.at<double>(0, 2);
  double v0 = k.at<double>(1, 2);

  double x = (u - u0) * nfx;
  double y = (v - v0) * nfy;

  double r = sqrt(pow(x, 2) + pow(y, 2));

  double undistortu = (x * (1 + (k1 * pow(r, 2)) + (k2 * pow(r, 4)) + (k3 * pow(r, 6)))) + 2 * p1 * x * y + p2 * (pow(r, 2) + 2 * pow(x, 2));
  double undistortv = (y * (1 + (k1 * pow(r, 2)) + (k2 * pow(r, 4)) + (k3 * pow(r, 6)))) + 2 * p2 * y * x + p1 * (pow(r, 2) + 2 * pow(y, 2));

  /*double undistortx = fx * undistortu + u0;
  double undistorty = fy * undistortv + v0;*/

  out = Mat (1,1, CV_64F);
  out.at<Vec2d>(0, 0) = Vec2d(undistortu, undistortv);

  return ;
}
int main(){

  Mat k(3, 3, CV_64F, double(0));
  k1.at<double>(0, 0) = double(4742.388876517584);
  k1.at<double>(1, 1) = double(4763.948527588444);
  k1.at<double>(0, 2) = double(1227.470759269065);
  k1.at<double>(1, 2) = double(1056.04714786588);
  k1.at<double>(2, 2) = double(1.0);

  Mat kc(1, 5, CV_64F, double(0));
  kc1.at<double>(0, 0) = double(-0.06467969683562796);
  kc1.at<double>(0, 1) = double(-1.153609598994524);
  kc1.at<double>(0, 2) = double(0.001415340992108717);
  kc1.at<double>(0, 3) = double(0.0001345774658006295);
  kc1.at<double>(0, 4) = double(0.0);

  Mat inp1(1, 1, CV_64FC2);
  inp1.at<Vec2d>(0, 0) = Vec2d(p.x, p.y);
  Mat output1;

  undistortPoints(inp1, output1, k, kc);

  const Vec2d & outvec1 = output1.at<cv::Vec2d>(0, 0);

  double x = outvec1[0];
  double y = outvec1[1];

  MYundisortPoint(inp1, output1, k, kc);

  const Vec2d & outvec1 = output1.at<cv::Vec2d>(0, 0);

  double myx = outvec1[0];    
  double myy = outvec1[1];

  return 0;
}

1 个答案:

答案 0 :(得分:0)

你的函数(MYundistortPoints)扭曲点(undistortPoints的反向)。因此,您应该尝试将您的函数与opencv中的projectPoints函数进行比较。

cv::Mat rVec(3, 1, cv::DataType<double>::type); // Rotation vector
rVec.at<double>(0) = 0;
rVec.at<double>(1) = 0;
rVec.at<double>(2) =0;
cv::Mat tVec(3, 1, cv::DataType<double>::type); // Translation vector
tVec.at<double>(0) =0;
tVec.at<double>(1) = 0;
tVec.at<double>(2) = 0;

cv::projectPoints(points,rVec,tVec, cameraMatrix, distCoeffs,result);

ProjectPoints