【问题标题】:How to print a Kinect frame in OpenCV using OpenNI bindings如何使用 OpenNI 绑定在 OpenCV 中打印 Kinect 帧
【发布时间】:2021-02-23 10:39:27
【问题描述】:

我正在尝试使用 OpenCV 处理来自 kinect 的深度图像。我使用 Python 和 primesense 的绑定 (https://pypi.org/project/primesense/),但我在显示从 openNI 获得的图像时遇到了很多麻烦。我在用

import numpy as np
import cv2
from primesense import openni2

openni2.initialize("./Redist")     # can also accept the path of the OpenNI     redistribution

dev = openni2.Device.open_any()

depth_stream = dev.create_color_stream()
depth_stream.start()

while(True):

    frame = depth_stream.read_frame()
    print(type(frame)) #prints <class 'primesense.openni2.VideoFrame'>

    frame_data = frame.get_buffer_as_uint8()
    print(frame_data) # prints <primesense.openni2.c_ubyte_Array_921600 object at 0x000002B3AF5F8848>

    image = np.array(frame_data, dtype=np.uint8) 


    print(type(image)) #prints <class 'numpy.ndarray'>
    print(image) #prints [12 24  3 ...  1  3 12], i guess this is the array that makes the image


    cv2.imshow('image', image)

depth_stream.stop()
openni2.unload()

这是我得到的输出,只是一个没有图像的窗口:

根本没有关于如何使用这些绑定的文档,所以我在这里有点盲点。我以为frame.get_buffer_as_uint8() 给了我准备打印的数组,但它只返回primesense.openni2.c_ubyte_Array_921600 object at 0x000002B3AF5F8848

实际上,我查看了绑定的代码,发现:

def get_buffer_as_uint8(self):
    return self.get_buffer_as(ctypes.c_uint8)
def get_buffer_as_uint16(self):
    return self.get_buffer_as(ctypes.c_uint16)
def get_buffer_as_triplet(self):
    return self.get_buffer_as(ctypes.c_uint8 * 3)

有人用过这个绑定吗?知道如何使它们工作吗?提前谢谢你

【问题讨论】:

  • 文档说“请参阅 C API 以获取文档”。 PyQt5 也做了类似的事情。意味着绑定的签名与底层的 c 函数相同。
  • 我检查了文档并没有找到太多.. 我确定我从 np.array(frame_data, dtype=np.uint8) 得到了正确的数组,问题似乎出在数组的数据类型上跨度>
  • (你可能想看看这个:github.com/code-iai/iai_kinect2

标签: python opencv openni


【解决方案1】:

我找到了解决办法:

您必须使用frame_data = frame.get_buffer_as_uint16(),而不是使用image = np.array(frame_data, dtype=np.uint8) 来获取图像。另外,我未能正确设置图像形状。

供将来参考

要使用 Python 的 OpenNI 绑定从深度相机(Kinect 不是唯一的)获取图像,并使用 OpenCV 处理该图像,下面的代码可以解决问题:

import numpy as np
import cv2
from primesense import openni2
from primesense import _openni2 as c_api

openni2.initialize("./Redist")     # can also accept the path of the OpenNI redistribution

dev = openni2.Device.open_any()

depth_stream = dev.create_depth_stream()
depth_stream.start()

while(True):

    frame = depth_stream.read_frame()
    frame_data = frame.get_buffer_as_uint16()

    img = np.frombuffer(frame_data, dtype=np.uint16)
    img.shape = (1, 480, 640)
    img = np.concatenate((img, img, img), axis=0)
    img = np.swapaxes(img, 0, 2)
    img = np.swapaxes(img, 0, 1)

    cv2.imshow("image", img)
    cv2.waitKey(34)


depth_stream.stop()
openni2.unload()

要使用彩色相机,您可以改用dev.create_color_stream()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多