我正在使用此代碼從該影像中洗掉此黃色標記:

import cv2
import numpy as np
# read image
img = cv2.imread('input.jpg')
# threshold on yellow
lower = (0, 200, 200)
upper = (100, 255, 255)
thresh = cv2.inRange(img, lower, upper)
# apply dilate morphology
kernel = np.ones((9, 9), np.uint8)
mask = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)
# get largest contour
contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(big_contour)
# draw filled white contour on input
result = img.copy()
cv2.drawContours(result, [big_contour], 0, (255, 255, 255), -1)
cv2.imwrite('yellow_removed.png', result)
# show the images
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()
結果是這樣的,正如預期的那樣:

但是當我將此影像傳遞給同一個腳本時,我收到以下錯誤:
big_contour = max(contours, key=cv2.contourArea) ValueError: max() arg 是一個空序列
顯然,它沒有檢測到任何輪廓,并且輪廓陣列是空的,但我無法弄清楚為什么會這樣或如何修復它。幫助表示贊賞!
uj5u.com熱心網友回復:
檢查你的下限。當我將下閾值更改為lower = (0, 120, 120).
uj5u.com熱心網友回復:
閾值是由于第二張影像較暗的原因。降低這些閾值會捕獲更多的黃色區域,但在繪制輪廓時仍會留下一些孔。
lower = (0, 130, 130)

您可以通過繪制邊界矩形來解決此問題。
cv2.rectangle(result,(x,y),(x w,y h),(255,255,255),-1)

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/478656.html

