我想在 python 中使用 OpenCV 識別給定影像中的托盤是否為空。
以下是我嘗試過的
- 檢測最大的矩形并使用以下代碼進行裁剪
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
thresh = cv2.erode(thresh, kernel, iterations=4)
thresh = cv2.dilate(thresh, kernel, iterations=4)
cnts, hier = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Find the biggest contour
max_area = -1
max_c = 0
for i in range(len(cnts)):
contour = cnts[i]
area = cv2.contourArea(contour)
if (area > max_area):
max_area = area
max_c = i
contour = cnts[max_c]
areas = [cv2.contourArea(c) for c in cnts]
max_index = np.argmax(areas)
cnt=cnts[max_index]
x,y,w,h = cv2.boundingRect(cnt)
crop_img = img[y:y h, x:x w]
- 然后嘗試通過在應用精明邊緣檢測并應用掩碼和噪聲去除方法后獲取輪廓來找到黑點
#Create empty mask and flood fill
mask = np.zeros(edges.shape)
#Create 3-channel alpha mask
mask_stack = np.dstack([mask]*3)
- 并再次閾值影像并找到輪廓。
它適用于中型和大型物體,但是當我放置硬幣等小物體時,這種方法不起作用,因為托盤也有一些劃痕和灰塵。這是我在 python 中使用 OpenCV 的第一個專案
請幫助找到實作此要求的解決方案。
輸入:

輸出:

空托盤:

uj5u.com熱心網友回復:
我建議你:
- 進行
請注意,此解決方案受不同引數的影響,為了更好地控制它們并獲得穩定的結果,我建議您考慮上述建議。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/434246.html
