Search on blog:

OpenCV: error: (-215:Assertion failed) !_img.empty() in function 'imwrite' [GB]

Error

cv2.error: OpenCV(4.1.2) /io/opencv/modules/imgcodecs/src/loadsave.cpp:715: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

means you try to write empty array (empty image).

You can get the same error using empty array.

import cv2
import numpy as np

cv2.imwrite('img.png', np.array([]))

But if you add at least one element then it works without error.

import cv2
import numpy as np

cv2.imwrite('img.png', np.array([0]))

Directly before imwrite you can check shape or size of array to confirm it.

import cv2
import numpy as np

arr = np.array([])

print('shape:', arr.shape)
print('size:', arr.size)
print('--- arr ---\n', arr, '\n--- end ---')

cv2.imwrite('img.png', arr)

This code will works without error because array is not empty.

import cv2
import numpy as np

arr = np.array([
    [  0,   0,   0],
    [  0, 255,   0],
    [  0,   0,   0],
], dtype='uint8')

print('shape:', arr.shape)
print('size:', arr.size)
print('--- arr ---\n', arr, '\n--- end ---')

cv2.imwrite('img.png', arr)

cv2.imshow('image', arr)
cv2.waitKey(0)
cv2.destroyWindow()

Problem z wczytaniem pliku haarcascades w OpenCV

Czasami tutorial wczytuje plik haarcascades zainstalowany z OpenCV w ten sposób

eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

ale gdy my uruchamiamy taki kod

import numpy as np
import cv2

eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')

image = np.array([1,2,3], dtype='uint8')
eye_cascade.detectMultiScale(img, 1.3, 5)

to otrzymujemy błąd …

« Page: 1 / 1 »