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

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

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

示意图: 

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 <iostream>
#include <string>

std::vector<cv::Point2f> Generate2DPoints();
std::vector<cv::Point3f> Generate3DPoints();

int main( int argc, char* argv[])
{
  // Read points
  std::vector<cv::Point2f> imagePoints = Generate2DPoints();
  std::vector<cv::Point3f> 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<double>::type);
  cameraMatrix.at<double>(0,0)=fx;
  cameraMatrix.at<double>(1,1)=fy;
  cameraMatrix.at<double>(2,2)=1;
  cameraMatrix.at<double>(0,2)=cx;
  cameraMatrix.at<double>(1,2)=cy;
  cameraMatrix.at<double>(0,1)=0;
  cameraMatrix.at<double>(1,0)=0;
  cameraMatrix.at<double>(2,0)=0;
  cameraMatrix.at<double>(2,1)=0;


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

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

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

  std::vector<cv::Point2f> 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<cv::Point2f> Generate2DPoints()
{
  std::vector<cv::Point2f> 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<cv::Point3f> Generate3DPoints()
{
  std::vector<cv::Point3f> 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

  • 10
    点赞
  • 69
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值