这是我代码中出现问题的部分。应该计算图片中绿色像素的数量:
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))
答案 0 :(得分:1)
您正在传递要读取的PIL图像,但它需要一个文件路径(https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imread#Mat%20imread(const%20string&%20filename,%20int%20flags)
您应该使用:
imgg = cv2.imread('path.tif', 1)
答案 1 :(得分:0)
图像已使用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))