Skip to content

cv2.imshow() complains with: error: (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 'cvShowImage' #167

Closed
@matthiasguentert

Description

@matthiasguentert

Expected behaviour

I was expecting that cv2.imshow() will show the depth image red from openni2 device.

Actual behaviour

When it reaches cv2.imshow("image", img) it crashes with:

Traceback (most recent call last):
File "C:/Dev/Repos/untitled/demo.py", line 28, in
cv2.imshow("image", img)
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'

This seems to be related to this opencv issue which unfortunately got closed. I don't see why.

This is my related post on stackoverflow:

The shape of img is a tuple (480, 640, 3) and dtype is uint16 just before the call to cv2.imshow()

Please note: The same happens when calling

cv2.imshow("test", np.ones((50, 50), dtype=np.uint16));

... so I guess OpenNI can be ruled out... also downgrading to the latest 3.x release (opencv-python 3.4.5.20) made it work!

Steps to reproduce

  • Install opencv-python from pip (version 4.0.0.21)
  • Install openni-2.2.0.post6
  • Run the bellow script on a Windows 10 Pro (x64) machine with Python (3.6.8 - Anaconda)
  • I am using an Orbbec Astra Pro camera which correctly shows up within my device manager as deph sensor and camera as well.
import numpy as np
import cv2
from openni import openni2
from openni 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(25)

depth_stream.stop()
openni2.unload()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions