C++OpenCV使用PnP方法计算位姿

使用pnp方法可以计算相机在空间中的位姿,包括旋转R与平移t。

pnp,简言之,已知几个点在三维空间中的坐标,未知相机的空间位姿。现在相机拍摄这几个已知点,得到图像,即已知点在图片中的像素坐标也已知了,通过已知点的三维坐标与二维坐标、相机参数,可以计算得到相机在世界坐标系下的位姿R、t。

示意图: 

C++OpenCV使用PnP方法计算位姿_第1张图片

OpenCV中solvePnP函数

void solvePnP(InputArray objectPoints, InputArray imagePoints, InputArray cameraMatrix, InputArray distCoeffs, OutputArray rvec, OutputArray tvec, bool useExtrinsicGuess=false, int flags = CV_ITERATIVE)

输入:

objectPoints - 世界坐标系下的景物点的三维坐标
imagePoints - 图像坐标系下对应点的二维坐标
cameraMatrix - 相机的内参矩阵
distCoeffs - 相机的畸变系数

flags - 默认使用CV_ITERATIV迭代法

输出:
rvec - 输出的旋转向量。使坐标点从世界坐标系旋转到相机坐标系
tvec - 输出的平移向量。使坐标点从世界坐标系平移到相机坐标系

完整代码:

pose.cpp

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

#include 
#include 

std::vector Generate2DPoints();
std::vector Generate3DPoints();

int main( int argc, char* argv[])
{
  // Read points
  std::vector imagePoints = Generate2DPoints();
  std::vector objectPoints = Generate3DPoints();

  std::cout << "There are " << imagePoints.size() << " imagePoints and " << objectPoints.size() << " objectPoints." << std::endl;

    //camera parameters
  double fx = 800; //focal length x
  double fy = 800//focal le

  double cx = 400; //optical centre x
  double cy = 500; //optical centre y

  cv::Mat cameraMatrix(3,3,cv::DataType::type);
  cameraMatrix.at(0,0)=fx;
  cameraMatrix.at(1,1)=fy;
  cameraMatrix.at(2,2)=1;
  cameraMatrix.at(0,2)=cx;
  cameraMatrix.at(1,2)=cy;
  cameraMatrix.at(0,1)=0;
  cameraMatrix.at(1,0)=0;
  cameraMatrix.at(2,0)=0;
  cameraMatrix.at(2,1)=0;


  //std::cout << "Initial cameraMatrix: " << cameraMatrix << std::endl;

  cv::Mat distCoeffs(5,1,cv::DataType::type);
  distCoeffs.at(0) = -0.09;
  distCoeffs.at(1) = 0.33;
  distCoeffs.at(2) = 0;
  distCoeffs.at(3) = 0;
 distCoeffs.at(5) = -0.22;
  cv::Mat rvec(3,1,cv::DataType::type);
  cv::Mat tvec(3,1,cv::DataType::type);

  cv::solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs, rvec, tvec);
//输出计算结果
  std::cout << "rvec: " << rvec << std::endl;
  std::cout << "tvec: " << tvec << std::endl;

  std::vector projectedPoints;
  cv::projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs, projectedPoints);

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

  return 0;
}


std::vector Generate2DPoints()
{
  std::vector points;
  float x,y;

//x和y是图片上的像素坐标,

  x=100 ; y=110;
  points.push_back(cv::Point2f(x,y));

  x=100;y=1;
  points.push_back(cv::Point2f(x,y));

  x=2 ; y=2;
  points.push_back(cv::Point2f(x,y));

  x=2;y=2;
  points.push_back(cv::Point2f(x,y));

  x=2 ; y=2;
 points.push_back(cv::Point2f(x,y));

  x=3;y=3;
  points.push_back(cv::Point2f(x,y));

  x=2 ; y=4;
 points.push_back(cv::Point2f(x,y));
  
  x=4;y=4;
  points.push_back(cv::Point2f(x,y));

  for(unsigned int i = 0; i < points.size(); ++i)
    {
  //  std::cout << points[i] << std::endl;
    }

  return points;
}


std::vector Generate3DPoints()
{
  std::vector points;
  float x, y, z;

//xyz是图片上的像素对应的景物点在世界坐标系中的三维坐标,
  x=5; y=0; z=0;
  points.push_back(cv::Point3f(x,y,z));

  x=5; y=6; z=0;
  points.push_back(cv::Point3f(x,y,z));

  x=0;	y=6; z=0;
  points.push_back(cv::Point3f(x,y,z));

  x=5;y=6;z=0;
  points.push_back(cv::Point3f(x,y,z));

  x=5; y=0; z=0;
  points.push_back(cv::Point3f(x,y,z));

  x=5; y=6; z=0;
  points.push_back(cv::Point3f(x,y,z));
  
  x=0; y=6; z=0;
  points.push_back(cv::Point3f(x,y,z));
  
  x=5; y=6; z=0;
  points.push_back(cv::Point3f(x,y,z));
  
  for(unsigned int i = 0; i < points.size(); ++i)
    {
   // std::cout << points[i] << std::endl;
    }

  return points;
}

使用cmake

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(vo1)

set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++14 ${SSE_FLAGS} -msse4")
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
set(OpenCV_DIR /usr/lib/x86_64-linux-gnu/cmake/opencv4)
find_package(OpenCV  REQUIRED)

include_directories(
        ${OpenCV_INCLUDE_DIRS}
        ${Sophus_INCLUDE_DIRS}
        "/usr/local/include/eigen3/"
)

add_executable(pose pose.cpp)
target_link_libraries(pose
        ${OpenCV_LIBS}
        fmt)

代码仅供参考、学习。

欢迎交流。

参考:

https://github.com/LiliMeng/SolvePNPicon-default.png?t=M4ADhttps://github.com/LiliMeng/SolvePNP

你可能感兴趣的:(c++,算法,计算机视觉,opencv)