使用cv2.imread函数时出现"<built-in function imread> returned NULL without setting an error"的错误,就像无法打开图片或获取数据一样。

10

这是导致问题的代码部分。它应该计算图片中绿色像素的数量:

img = Image.open('path.tif')

BLACK_MIN = np.array([0, 20, 20], np.uint8)

BLACK_MAX = np.array([120, 255, 255], np.uint8)

imgg = cv2.imread(img, 1)

dst = cv2.inRange(imgg, BLACK_MIN, BLACK_MAX)

no_black = cv2.countNonZero(dst)

print('The number of black pixels is: ' + str(no_black))
2个回答

7

6
如果传递 pathlib.Path,则会出现这种情况。需要将 Path 转换为 str。 - arun

-1

使用PIL已经读取了图像。现在img以数组格式存在,因此无法再次读取它。请以任何一种格式(pil或cv2)中的一种读取您的文件。

BLACK_MIN = np.array([0, 20, 20], np.uint8)

BLACK_MAX = np.array([120, 255, 255], np.uint8)

imgg = cv2.imread('path.tif', 1)

dst = cv2.inRange(imgg, BLACK_MIN, BLACK_MAX)

no_black = cv2.countNonZero(dst)

print('The number of black pixels is: ' + str(no_black))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接