在安装好了kinect2.0在linux下的驱动(libfreenect2),以及opencv3.4.2后,就可以运用他们来驱动kinect2.0显示各种视频画面。下面是实现步骤:
1,main.cpp
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <time.h>
#include <signal.h>
#include <opencv2/opencv.hpp>
#include <libfreenect2/libfreenect2.hpp>
#include <libfreenect2/frame_listener_impl.h>
#include <libfreenect2/registration.h>
#include <libfreenect2/packet_pipeline.h>
#include <libfreenect2/logger.h>
using namespace std;
using namespace cv;
enum
{
Processor_cl,
Processor_gl,
Processor_cpu
};
bool protonect_shutdown = false; // Whether the running application should shut down.
void sigint_handler(int s)
{
protonect_shutdown = true;
}
// void drawText(Mat & image, const char* a );
int main()
{
//定义变量
std::cout << "Hello World!" << std::endl;
libfreenect2::Freenect2 freenect2;
libfreenect2::Freenect2Device *dev = 0;
libfreenect2::PacketPipeline *pipeline = 0;
//搜寻并初始化传感器
if(freenect2.enumerateDevices() == 0)
{
std::cout << "no device connected!" << std::endl;
return -1;
}
string serial = freenect2.getDefaultDeviceSerialNumber();
std::cout << "SERIAL: " << serial << std::endl;
//配置传输格式
#if 1 // sean
int depthProcessor = Processor_cl;
if(depthProcessor == Processor_cpu)
{
if(!pipeline)
//! [pipeline]
pipeline = new libfreenect2::CpuPacketPipeline();
//! [pipeline]
}
else if (depthProcessor == Processor_gl) // if support gl
{
#ifdef LIBFREENECT2_WITH_OPENGL_SUPPORT
if(!pipeline)
{
pipeline = new libfreenect2::OpenGLPacketPipeline();
}
#else
std::cout << "OpenGL pipeline is not supported!" << std::endl;
#endif
}
else if (depthProcessor == Processor_cl) // if support cl
{
#ifdef LIBFREENECT2_WITH_OPENCL_SUPPORT
if(!pipeline)
pipeline = new libfreenect2::OpenCLPacketPipeline();
#else
std::cout << "OpenCL pipeline is not supported!" << std::endl;
#endif
}
//启动设备
if(pipeline)
{
dev = freenect2.openDevice(serial, pipeline);
}
else
{
dev = freenect2.openDevice(serial);
}
if(dev == 0)
{
std::cout << "failure opening device!" << std::endl;
return -1;
}
signal(SIGINT, sigint_handler);
protonect_shutdown = false;
libfreenect2::SyncMultiFrameListener listener(
libfreenect2::Frame::Color |
libfreenect2::Frame::Depth |
libfreenect2::Frame::Ir);
libfreenect2::FrameMap frames;
dev->setColorFrameListener(&listener);
dev->setIrAndDepthFrameListener(&listener);
//启动数据传输
dev->start();
std::cout << "device serial: " << dev->getSerialNumber() << std::endl;
std::cout << "device firmware: " << dev->getFirmwareVersion() << std::endl;
//循环接收
libfreenect2::Registration* registration = new libfreenect2::Registration(dev->getIrCameraParams(), dev->getColorCameraParams());
libfreenect2::Frame undistorted(512, 424, 4), registered(512, 424, 4), depth2rgb(1920, 1080 + 2, 4);
Mat rgbmat, depthmat, depthmatUndistorted, irmat, rgbd, rgbd2;
cv::namedWindow("rgb", WND_PROP_ASPECT_RATIO);
cv::namedWindow("ir", WND_PROP_ASPECT_RATIO);
cv::namedWindow("depth", WND_PROP_ASPECT_RATIO);
//cv::namedWindow("undistorted", WND_PROP_ASPECT_RATIO);
//cv::namedWindow("registered", WND_PROP_ASPECT_RATIO);
cv::namedWindow("depth2RGB", WND_PROP_ASPECT_RATIO);
while(!protonect_shutdown)
{
listener.waitForNewFrame(frames);
libfreenect2::Frame *rgb = frames[libfreenect2::Frame::Color];
libfreenect2::Frame *ir = frames[libfreenect2::Frame::Ir];
libfreenect2::Frame *depth = frames[libfreenect2::Frame::Depth];
cv::Mat(rgb->height, rgb->width, CV_8UC4, rgb->data).copyTo(rgbmat);
cv::Mat(ir->height, ir->width, CV_32FC1, ir->data).copyTo(irmat);
cv::Mat(depth->height, depth->width, CV_32FC1, depth->data).copyTo(depthmat);
// drawText(rgbmat, "NIT");
cv::imshow("rgb", rgbmat);
cv::imshow("ir", irmat / 4500.0f);
cv::imshow("depth", depthmat / 4500.0f);
registration->apply(rgb, depth, &undistorted, ®istered, true, &depth2rgb);
cv::Mat(undistorted.height, undistorted.width, CV_32FC1, undistorted.data).copyTo(depthmatUndistorted);
cv::Mat(registered.height, registered.width, CV_8UC4, registered.data).copyTo(rgbd);
cv::Mat(depth2rgb.height, depth2rgb.width, CV_32FC1, depth2rgb.data).copyTo(rgbd2);
cv::imshow("undistorted", depthmatUndistorted / 4500.0f);
cv::imshow("registered", rgbd);
cv::imshow("depth2RGB", rgbd2 / 4500.0f);
int key = cv::waitKey(1);
protonect_shutdown = protonect_shutdown || (key > 0 && ((key & 0xFF) == 27)); // shutdown on escape
listener.release(frames);
}
//关闭设备
dev->stop();
dev->close();
delete registration;
#endif
std::cout << "Goodbye World!" << std::endl;
return 0;
}
/*
void drawText(Mat & image, const char* str)
{
putText(image, str,
Point(100, 100),
FONT_HERSHEY_SIMPLEX, 3, // font face and scale
Scalar(255, 255, 0),
3, LINE_AA); // line thickness and type
}
*/
2,CMakeList.txt
cmake_minimum_required(VERSION 2.8)
project( main )
set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} $ENV{HOME}/freenect2/lib/cmake/freenect2)
find_package( OpenCV REQUIRED )
FIND_PACKAGE( PkgConfig REQUIRED )
FIND_PACKAGE( freenect2 REQUIRED )
include_directories("/usr/include/libusb-1.0/")
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories( ${freenect2_INCLUDE_DIRS} )
add_executable( main main.cpp )
target_link_libraries( main ${freenect2_LIBRARIES} ${OpenCV_LIBS} )
3,效果
- ir:红外图
- depth:深度图
- rgb:彩色图
- depth2RGB:深度图转换为RGB
- registered:彩色图注入深度图
- undistorted:深度图(无失真)
网友评论