查找对象的 3D 坐标

Finding 3D coordinate of object

我试图找到我知道他在图像中的坐标的卑鄙对象的 3D 坐标(在世界中)。所以在互联网上进行一些研究后,我成功地找到了 X 和 Y 坐标。但我找不到 Z

那是我在 openCV 中的程序

  void drawAndCalcul3D(int x,int y,Mat& frame)
{
cv::Mat uvPoint = cv::Mat::ones(3,1,cv::DataType<double>::type);
uvPoint.at<double>(0,0) = x; //got this point using mouse callback
uvPoint.at<double>(1,0) = y;

std::vector<cv::Point2f> imagePoints;
std::vector<cv::Point3f> objectPoints;
cv::Mat rotationMatrix(3,3,cv::DataType<double>::type);
Mat cameraMatrix(3,3,DataType<double>::type);
  setIdentity(cameraMatrix);  
  cameraMatrix.at<double>(0,0)=493.415; //my Camera matrix
  cameraMatrix.at<double>(0,1)=0;
  cameraMatrix.at<double>(0,2)=319.5;

  cameraMatrix.at<double>(1,0)=0;
  cameraMatrix.at<double>(1,1)=493.415;
  cameraMatrix.at<double>(1,2)=179.5;

  cameraMatrix.at<double>(2,0)=0;
  cameraMatrix.at<double>(2,1)=0;
  cameraMatrix.at<double>(2,2)=1;
// extrinsic parameters rvec and tvect : rotation and translation matrix
Mat rvec(3,1,cv::DataType<double>::type);
    rvec.at<double>(0)=-0.1408;
    rvec.at<double>(1)=3.011;
    rvec.at<double>(2)=-0.171147;
//
cv::Mat tvec(3,1,cv::DataType<double>::type);
    tvec.at<double>(0)=15.84;
    tvec.at<double>(1)=-64.67;
    tvec.at<double>(2)=274.584;

cv::Rodrigues(rvec,rotationMatrix);
//
cv::Mat tempMat, tempMat2;
double s;
tempMat = rotationMatrix.inv() * cameraMatrix.inv() * uvPoint;
tempMat2 = rotationMatrix.inv() * tvec;
s =tempMat2.at<double>(2,0); //12 represents the height Zconst
s /= tempMat.at<double>(2,0);
Mat exp=rotationMatrix.inv() * (s * cameraMatrix.inv() * uvPoint - tvec);
cv::putText(frame,intToString(exp.at<double>(0))+ " , " + intToString(exp.at<double>(1))+ " , " + intToString(exp.at<double>(2)),cv::Point(x,y+20),2,1,Scalar(0,255,0));
}

有谁知道我怎样才能找到物体的深度 (Z) 吗?

我和她用的是同一个程序Computing x,y coordinate (3D) from image point我不知道如何更改它以在 Z 不是常量时找到它

x,y像点只决定了从摄像机中心通过像点的射线。它有无数个可能的 z,当你将图像点与逆矩阵相乘时,你将得到一条射线或一条线的方程。使用单个相机不可能从单个图像点获得 3D。当然,除非你做出一些强有力的假设,比如物体的形状或大小,或者使用额外的知识,比如单眼线索。此类提示的示例有 blur/focus、brightness/shading、纹理大小、附近其他物体的熟悉大小等。

另一个例子 - 如果你知道物体所在平面的方程式,你可以将这个平面与光线相交并到达一个独特的 3D 点。再举一个例子,比如说,您在单元格 phone 中使用加速度计,它会为您提供相机相对于重力矢量的角度。您还知道您持有一个单元格的大致高度 phone(离地面 1.5 米)。比起您可以轻松地将地面上的 3D 点计算为光线与已知平面的交点,请参见下文。该公式给出了图像中心点的 Z。对于其他点,您必须添加由偏心像素形成的额外角度。这将允许您完全恢复地平面的 3D。