Using cv2.imread: “<built-in function imread> returned NULL without setting an error”, as if it can't open the picture or get the data

为君一笑 提交于 2021-02-07 06:05:57

问题


This is the part of my code that gives the problem. It is supposed to count the amount of green pixels in a picture:

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))

回答1:


You are passing a PIL image to imread but it expects a filepath (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)

You should use:

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



回答2:


Image is already readed using PIL.Now the img is in array format so you cannot able to read it again.Read your file in any one kind of format either pil or 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))


来源:https://stackoverflow.com/questions/58123915/using-cv2-imread-built-in-function-imread-returned-null-without-setting-an-e

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!