Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.error: OpenCV(4.0.0) error: (-215:Assertion failed) dst.data == (uchar*)dst_ptr in function 'cvShowImage'

import numpy as np
import cv2

# first_method
# img = cv2.imread('sample.jpg')
# second_method
# img = np.zeros((1000, 1000, 3), np.int8) 

while True:
    cv2.imshow('sample', img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()

In the above code I'm trying to display the image using imshow() function of opencv. When I try to use the first method, i.e crating an array from a sample image the code work perfectly, but when I create my own array I get the following error-

PS C:\Users\tanma\Dropbox\Coding\python\AI> python .\test_1.py
Traceback (most recent call last):
  File ".\test_1.py", line 16, in <module>
    cv2.imshow('sample', 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'
like image 543
Tanmay Gupta Avatar asked Sep 17 '25 20:09

Tanmay Gupta


1 Answers

I had a similar problem using OpenCV 4.0.0. According to this, the bug is fixed in 4.0.1, so you can just update the opencv-python package.

My image was of type floating-point, but according to OpenCV Documentation it is ok to show such images:

  • If the image is 8-bit unsigned, it is displayed as is.
  • If the image is 16-bit unsigned or 32-bit integer, the pixels are divided by 256. That is, the value range [0,255*256] is mapped to [0,255].
  • If the image is 32-bit floating-point, the pixel values are multiplied by 255. That is, the value range [0,1] is mapped to [0,255].
like image 103
Butterfly Avatar answered Sep 19 '25 11:09

Butterfly