OpenCV 3d point projection

OpenCV 3d point projection

本文关键字:projection point 3d OpenCV      更新时间:2023-10-16

我在纠正OpenCV程序以投影3d点时遇到问题。在使用OpenCV的projectPoints功能时,我似乎遇到了这个问题。

这是我得到的错误:

OpenCV 错误:断言失败(mtype == type0 ||(CV_MAT_CN(mtype) == CV_MAT_CN(type0) && ((1 <<type0) & fixedDepthMask) != 0))在创建中,文件/home/daniel/Comp4102/opencv/modules/core/src/matrix.cpp,第 2375 行 在抛出"cv::异常"实例后终止调用 what():/home/daniel/Comp4102/opencv/modules/core/src/matrix.cpp:2375: 错误: (-215) 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;
}

函数projectPoints需要相同类型的参数objectPointsimagePoints,而您将objectPoints传递为Point3dimagePoints传递为Point2f

错误告诉您这两种类型是不同的: double != float .

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

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