Я новичок, пытающийся изучать opencv.Я успешно установил opencv в моей системе Ubuntu и пытался запустить несколько кодов из Интернета, и я столкнулся с этой проблемой
# import the necessary packages
from __future__ import print_function
import imutils
import cv2
# load the Tetris block image, convert it to grayscale, and threshold
# the image
print("OpenCV Version: {}".format(cv2.__version__))
image = cv2.imread("tetris_blocks.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 225, 255, cv2.THRESH_BINARY_INV)[1]
# check to see if we are using OpenCV 2.X or OpenCV 4
if imutils.is_cv2() or imutils.is_cv4():
(cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# check to see if we are using OpenCV 3
elif imutils.is_cv3():
(_, cnts, _) = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
# draw the contours on the image
cv2.drawContours(image, cnts, -1, (240, 0, 159), 3)
cv2.imshow("Image", image)
cv2.waitKey(0)
В результате должно быть изображение, но появляется следующая ошибка:
runfile('/home/viper_36/pythontemp/temp.py', wdir='/home/viper_36/pythontemp')
OpenCV Version: 4.0.0
Traceback (most recent call last):
File "<ipython-input-1-202e5c8bcd5b>", line 1, in <module>
runfile('/home/viper_36/pythontemp/temp.py', wdir='/home/viper_36/pythontemp')
File "/home/viper_36/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "/home/viper_36/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "/home/viper_36/pythontemp/temp.py", line 11, in <module>
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
error: OpenCV(4.0.0) /io/opencv/modules/imgproc/src/color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'