OpenNI和OpenCV:cv2.imshow()因错误而崩溃:(-215:声明失败)函数'cvShowImage'中的dst.data ==(uchar *)dst_ptr [英] OpenNI and OpenCV: cv2.imshow() crashes with error: (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 'cvShowImage'

查看:588
本文介绍了OpenNI和OpenCV:cv2.imshow()因错误而崩溃:(-215:声明失败)函数'cvShowImage'中的dst.data ==(uchar *)dst_ptr的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从连接到Windows 10计算机的Orbbec Astra Pro摄像机接收深度图像.我已经安装了opencv-python 4.0.0.21primesense 2.2.0.30.post5,这似乎是最新的可用的稳定python软件包.

I am trying to receive a depth image from an Orbbec Astra Pro camera connected to a Windows 10 machine. I have therfor installed opencv-python 4.0.0.21 and primesense 2.2.0.30.post5 which seems to be latest stable python packages available.

这是我正在尝试的代码片段:

This is the code snippet I am experimenting with:

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

openni2.initialize("./OpenNI-Windows-x64-2.3/Redist")    

if openni2.is_initialized():
    print('openni2 ready')
else:
    print('openni2 not ready')

dev = openni2.Device.open_any()

depth_stream = dev.create_depth_stream()
depth_stream.set_video_mode(c_api.OniVideoMode(pixelFormat = c_api.OniPixelFormat.ONI_PIXEL_FORMAT_DEPTH_100_UM, resolutionX = 640, resolutionY = 480, fps = 30))
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()

但是,当cv2.imshow()被呼叫时,我会收到:

However when cv2.imshow() is getting called I am receiving:

cv2.error:OpenCV(4.0.0) C:\ projects \ opencv-python \ opencv \ modules \ highgui \ src \ window_w32.cpp:1230: 错误:(-215:声明失败)函数中的dst.data ==(uchar *)dst_ptr 'cvShowImage'

cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window_w32.cpp:1230: error: (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 'cvShowImage'

我不知道如何将OpenNI帧转换为OpenCV mat数据结构,以及为什么cv2.imshow()拒绝显示图像. OpenNI似乎已正确初始化,至少它会打印openni2 ready ...我在这里做错了什么?

I have no idea on how to convert an OpenNI frame to an OpenCV mat data structure and why cv2.imshow() refuses to show an image. OpenNI seems to correctly initialize, at least it prints openni2 ready... What am I doing wrong here?

修改

这似乎是我在这里报告的错误 https://github. com/skvark/opencv-python/issues/167

This seems to be a bug which I have reported here https://github.com/skvark/opencv-python/issues/167

解决方案(种类)

将OpenCV版本降级到最新的3.x版本可以正常工作!

Downgrading OpenCV version to the latest 3.x release made it work!

pip install --upgrade opencv-python==3.4.5.20

推荐答案

您是否尝试过:

frame.get_buffer_as_uint32()
img = np.frombuffer(frame_data, dtype=np.uint32)

在opencv4中,由于某些原因,如果不使用特定的dtype,imshow将无法正常工作

In opencv4 for some reason imshow will not work if you do not use specific dtype

这篇关于OpenNI和OpenCV:cv2.imshow()因错误而崩溃:(-215:声明失败)函数'cvShowImage'中的dst.data ==(uchar *)dst_ptr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆