0

我正在尝试将我的 KinectV2 设置为 Linux 中的网络摄像头,使用libfreenect2与 Kinect 交互,并使用libv4l2cpp作为 C++ 的 V4L2 库的包装器。

问题是我无法让它显示我的图像,而我在相机设备上只能看到一个黑框......这是我的代码:

#include <csignal>

#include "V4l2Device.h"
#include "V4l2Output.h"
#include "libfreenect2/frame_listener_impl.h"
#include "libfreenect2/libfreenect2.hpp"
#include "libfreenect2/packet_pipeline.h"
#include "libfreenect2/registration.h"
#include <iostream>
#include <unistd.h>

// Wether the program should terminate
bool shutdown_flag = false;

void sigintHandler(int signal);

namespace lf2 = libfreenect2;

int main(int argc, char **argv) {
  // Manually handle SIGINT in order to properly
  // shutdown Freenect before terminating the program
  struct sigaction sigint_handler;
  sigint_handler.sa_handler = sigintHandler;
  sigemptyset(&sigint_handler.sa_mask);
  sigint_handler.sa_flags = 0;

  sigaction(SIGINT, &sigint_handler, NULL);

  // Freenect2 context
  lf2::Freenect2 freenect2;
  lf2::Freenect2Device *dev = nullptr;

  // Freenect2 device discovery
  if (freenect2.enumerateDevices() == 0) {
    std::cerr << "No devices connected!" << std::endl;
    return -1;
  }

  std::string serial{freenect2.getDefaultDeviceSerialNumber()};

  if ((dev = freenect2.openDevice(serial)) == 0) {
    std::cerr << "Failure opening device!" << std::endl;
    return -1;
  }

  // Freenect2 frame listeners
  lf2::SyncMultiFrameListener listener(lf2::Frame::Color);
  lf2::FrameMap frames;

  dev->setColorFrameListener(&listener);

  // Freenect2 driver start
  if (!dev->startStreams(true, false)) {
    std::cerr << "Failure starting stream!" << std::endl;
    return -1;
  }

  std::clog << "Device serial: " << dev->getSerialNumber() << std::endl;
  std::clog << "Device firmware: " << dev->getFirmwareVersion() << std::endl;

  // V4L2 output interface
  V4L2DeviceParameters param("/dev/video0", V4L2_PIX_FMT_XBGR32, 1920, 1080, 30);
  V4l2Output *videoOutput = V4l2Output::create(param);

  // Main loop
  while (!shutdown_flag) {
    if (!listener.waitForNewFrame(frames, 10 * 1000 /*10 seconds*/)) {
      std::cerr << "Timeout!" << std::endl;
      return -1;
    }

    lf2::Frame *rgb = frames[lf2::Frame::Color];

    timeval timeout;
    if (videoOutput->isWritable(&timeout) == 1) {
      videoOutput->write(reinterpret_cast<char *>(rgb->data), 4);
    }

    listener.release(frames);
  }

  // Freenect2 driver stop
  dev->stop();
  dev->close();

  return 0;
}

void sigintHandler(int signal) { shutdown_flag = true; }

我究竟做错了什么?我从两个库的示例中复制了大部分内容,所以我希望它能够工作......我的猜测是它与我告诉 V4L2 使用的两种格式有关,但据我所知,V4L2_PIX_FMT_XBGR32应该是适当的一种,根据 libreenect2 的文档,帧应该是原始 BGR 格式,每个像素使用 4 个字节,最后一个用作填充...

4

0 回答 0