OpenCV 3d点投影

时间:2016-02-03 19:21:07

标签: c++ opencv

我在修复OpenCV程序以投射3d点时遇到了问题。在使用OpenCV的projectPoints函数时,我似乎遇到了这个问题。

这是我得到的错误:

  

OpenCV错误:断言失败(mtype == type0 ||(CV_MAT_CN(mtype)== CV_MAT_CN(type0)&&((1<<<<<<<<<<<<<<< )& fixedDepthMask)!= 0)) ,文件/home/daniel/Comp4102/opencv/modules/core/src/matrix.cpp,第2375行   在抛出' cv :: Exception'的实例后终止调用     what():/ home / daniel / Comp4102 / opencv / modules / core / src / matrix.cpp:2375:error:( - 1515)mtype == type0 || (CV_MAT_CN(mtype)== CV_MAT_CN(type0)&&((1<<<<< type0)& fixedDepthMask)!= 0)in function create

以下是我写的代码:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"

#include <iostream>
#include <string>

std::vector<cv::Point3d> set3DPoints();

int main( int argc, char* argv[]) {
  // Setting given variables.
  double f = 500;
  double sx = 1;
  double sy = 1;
  double ox = 320;
  double oy = 240;

  std::vector<cv::Point3d> objectPoints = set3DPoints();


  cv::Mat Xw(1,3,cv::DataType<double>::type);

  Xw.at<double>(0,0) = 150;
  Xw.at<double>(0,1) = 200;
  Xw.at<double>(0,2) = 350;

  // Create the K matrix.
  cv::Mat K(3,3,cv::DataType<double>::type);

  K.at<double>(0,0) = -f/sx;
  K.at<double>(1,0) = 0;
  K.at<double>(2,0) = ox;

  K.at<double>(0,1) = 0;
  K.at<double>(1,1) = -f/sy;
  K.at<double>(2,1) = oy;

  K.at<double>(0,2) = 0;
  K.at<double>(1,2) = 0;
  K.at<double>(2,2) = 1;

  // Creating the Rotation Matrix
  cv::Mat R(3,3,cv::DataType<double>::type);

  R.at<double>(0,0) = 1; 
  R.at<double>(1,0) = 0; 
  R.at<double>(2,0) = 0; 

  R.at<double>(0,1) = 0; 
  R.at<double>(1,1) = 1; 
  R.at<double>(2,1) = 0; 

  R.at<double>(0,2) = 0; 
  R.at<double>(1,2) = 0; 
  R.at<double>(2,2) = 1;

  // Creating the Translation vector
  cv::Mat T(3,1,cv::DataType<double>::type);
  T.at<double>(0) = -70;
  T.at<double>(1) = -95;
  T.at<double>(2) = -120;

  std::cout << "K: " << "\n" << K << "\n";
  std::cout << "R: " << "\n" << R << "\n";
  std::cout << "T: " << "\n" << T << "\n";

  // Create zero distortion
  cv::Mat distCoeffs(4,1,cv::DataType<double>::type);
  distCoeffs.at<double>(0) = 0;
  distCoeffs.at<double>(1) = 0;
  distCoeffs.at<double>(2) = 0;
  distCoeffs.at<double>(3) = 0;

  // Creating Rodrigues rotation matrix
  cv::Mat rvecR(3,1,cv::DataType<double>::type);
  cv::Rodrigues(R,rvecR);

  std::vector<cv::Point2f> projectedPoints;

  cv::projectPoints(objectPoints, rvecR, T, K, distCoeffs, projectedPoints);

  for(unsigned int i=0; i<projectedPoints.size(); i++){
    std::cout << "Image point: " << objectPoints[i] << " Projected to " << projectedPoints[i] << "\n"; 
  }

  return 0;

}

std::vector<cv::Point3d> set3DPoints() {
  std::vector<cv::Point3d> points;

  double x,y,z;

  x=150;
  y=200;
  z=350;
  points.push_back(cv::Point3d(x,y,z));

  return points;
}

1 个答案:

答案 0 :(得分:2)

函数projectPoints需要相同类型的参数objectPointsimagePoints,而您将objectPoints作为Point3d传递,{{1 } {} imagePoints

错误告诉您两种类型不同:Point2f

只需将double != float声明为projectedPoints,以便它与Point2d具有相同的类型:

Point3d