error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor‘:

error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’:

Traceback (most recent call last):
  File "d:/CalculationCode/DataArugment.py", line 75, in 
    img = get_img('001 (36).jpg')
  File "d:/CalculationCode/DataArugment.py", line 39, in get_img
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
cv2.error: OpenCV(4.5.1) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-1drr4hl0\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

原因分析:

问题很简单,其实就是等价于
FileNotFoundError: [Errno 2] No such file or directory


解决方案:

文件路径有错,修改路径。
原:

def get_img(path):
    img_bgr = cv2.imread(path)
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    return img_rgb
img = get_img('001 (36).jpg')

现在:

def get_img(path):
    img_bgr = cv2.imread(path)
    img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)
    return img_rgb
img = get_img('./test/001 (36).jpg')

你可能感兴趣的:(Debug小结,python,opencv)