我正在嘗試從散點圖中檢測點并獲取這些點的位置。為此,我正在嘗試使用 OpenCVfindContours
函式,但由于某種原因,它無法檢測和繪制圖形上淺色點的輪廓
這是我的代碼-
img = cv2.imread('808.png')
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# finding contours/shapes
_, threshold = cv2.threshold(imgGray, 127, 255, cv2.THRESH_BINARY)
# using a findContours() function
contours, _ = cv2.findContours(threshold, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
i = 0
# list for storing names of shapes
for contour in contours:
if i == 0:
i = 1
continue
approx = cv2.approxPolyDP(
contour, 0.01 * cv2.arcLength(contour, True), True)
cv2.drawContours(img, [contour], 0, (0, 0, 0), 5)
# finding center point of shape
M = cv2.moments(contour)
if M['m00'] != 0.0:
x = int(M['m10']/M['m00'])
y = int(M['m01']/M['m00'])
cv2.imshow('shapes', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
這是結果 - 第一個是輸入,第二個是結果
可以清楚地看到,該函式檢測并在深色點周圍繪制正確的輪廓,但未正確檢測到淺色點。任何對此或一般方法的幫助(檢測點,然后從散點圖中獲取值)將不勝感激。謝謝!
uj5u.com熱心網友回復:
這是你的門檻水平。它太低了。始終檢查您的中間值。
BGR2GRAY 之后:
在 127 閾值后:
選擇一個更高的閾值,比如 192:
現在你已經擁有了它們。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/506590.html