使用cv2.imread:<内置函数imread>返回NULL而未设置错误",就好像它无法打开图片或获取数据一样 [英] 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
本文介绍了使用cv2.imread:<内置函数imread>返回NULL而未设置错误",就好像它无法打开图片或获取数据一样的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码中出现问题的部分.应该计算图片中绿色像素的数量:
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))
推荐答案
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)
您应该使用:
imgg = cv2.imread('path.tif', 1)
这篇关于使用cv2.imread:<内置函数imread>返回NULL而未设置错误",就好像它无法打开图片或获取数据一样的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文