簡體   English   中英

使用 cv2.imread:“<built-in function imread> 沒有設置錯誤就返回NULL”,好像無法打開圖片或獲取數據</built-in>

[英]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

這是我的代碼中出現問題的部分。 它應該計算圖片中綠色像素的數量:

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%20 標志)

你應該使用:

imgg = cv2.imread('path.tif', 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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM