事先需要先编译好Kinect和PCL的库
先用Kinect获取视野里cameraSpace的三维坐标点和colorSpace的rgb图像,因为这两个数据都是depthFrame map过去的,所以这两组点可以看做是一一对应的,把三维坐标和颜色结合就能得到点云的pointxyzrgb数据了。
CMakeLists.txt文件如下:
# cmake needs this line
cmake_minimum_required(VERSION 3.1)
# Enable C++11
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED TRUE)
#Define project name
project(robotCalibrate)
set(KINECT_INC "C:\\Program Files\\Microsoft SDKs\\Kinect\\v2.0_1409\\inc")
set(KINECT_LIB "C:\\Program Files\\Microsoft SDKs\\Kinect\\v2.0_1409\\Lib\\x64")
find_package(OpenCV REQUIRED)
find_package(PCL REQUIRED)
include_directories(${PCL_INCLUDE_DIRS} ${KINECT_INC} )
message("${PCL_LIBRARIES}")
link_directories(${PCL_LIBRARY_DIRS} ${KINECT_LIB})
add_definitions(${PCL_DEFINITIONS})
add_executable(robotCalibrate main.cpp lib.cpp definition.h KinectPointcloud.cpp)
target_link_libraries(robotCalibrate ${PCL_LIBRARIES} ${OpenCV_LIBS} kinect20.lib)
definition.h文件里如下
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <pcl/io/pcd_io.h>
#include <pcl/point_cloud.h>
#include <pcl/point_types.h>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <pcl/visualization/pcl_visualizer.h>
#include <kinect.h>
using namespace std;
typedef pcl::PointXYZRGB PointRGB;
typedef pcl::PointCloud<pcl::PointXYZ> PointCloudXYZ;
typedef pcl::PointCloud<pcl::PointXYZRGB> PointCloudRGB;
#define depthWidth 512
#define depthHeight 424
#define rgbWidth 1920
#define rgbHeight 1080
#define depthSize 424 * 512
#define rgbSize 1920 * 1080
class KinectPointcloud
{
public:
template<class Interface>
inline void SafeRelease(Interface *& pInterfaceToRelease);
KinectPointcloud();
void getPointcloudData();
PointCloudRGB::Ptr getPointcloud();
ColorSpacePoint* colorSpacePts = new ColorSpacePoint[depthSize]; // Maps depth pixels to rgb pixels
CameraSpacePoint* cameraSpacePts = new CameraSpacePoint[depthSize]; // Maps depth pixels to 3d coordinates
cv::Mat getRGBImg();
cv::Mat getDepthImg();
~KinectPointcloud();
private:
IKinectSensor* _sensor = nullptr;
std::string _filepath;
PointCloudRGB::Ptr _cloud = PointCloudRGB::Ptr(new PointCloudRGB);
cv::Mat _rgbImg;
cv::Mat _depthImg;
PointRGB _neckPoint;
};
template<