cv2.error: OpenCV(4.5.2) 出错: loadsave.cpp:721: error: (-215:Assertion failed) !_img.empty() in funct

在运行 PixelLib 官方示例的时候, 出现这个错误:

  File "/usr/local/lib/python3.9/site-packages/pixellib/instance/__init__.py", line 260, in segmentImage
    cv2.imwrite(save_path, extracted_objects)

cv2.error: OpenCV(4.5.2) /private/var/folders/24/8k48jl6d249_n_qfxwsl6xvm0000gn/T/pip-req-build-v0gme71y/opencv/modules/imgcodecs/src/loadsave.cpp:721: error: (-215:Assertion failed) !_img.empty() in function 'imwrite'

查了 pixellib 的源码 260 行:

extracted_objects = img[np.ix_(mask[:,:,a].any(1), mask[:,:,a].any(0))]

ex.append(extracted_objects)
if save_extracted_objects == True :
    save_path = os.path.join("segmented_object" + "_" + str(m) + ".jpg")
    cv2.imwrite(save_path, extracted_objects)

这里没有对提取的对象没有内容的情况做判断, 加一个判断即可成功运行,如下:

extracted_objects = img[np.ix_(mask[:,:,a].any(1), mask[:,:,a].any(0))]

ex.append(extracted_objects)
if save_extracted_objects == True and len(extracted_objects) > 0:
    save_path = os.path.join("segmented_object" + "_" + str(m) + ".jpg")
    cv2.imwrite(save_path, extracted_objects)

你可能感兴趣的:(图像处理,深度学习,Python,opencv,深度学习)