2016-07-11 315 views
0

我想在下面的圖像中檢測到圓形。圖像python中的圓形輪廓檢測opencv

enter image description here

所以,我沒有顏色的閾值,並終於得到了這樣的結果。

enter image description here

因爲在中心的線的被去除,圓被分成許多小部分,因此,如果我在此輪廓檢測,它只能分別給我每個輪廓。

enter image description here

但是,有沒有辦法,我能以某種方式結合的輪廓,所以我可以得到一個圓,而不是僅僅將它的一部分?

這裏是我的顏色閾值碼:

blurred = cv2.GaussianBlur(img, (9,9), 9) 

ORANGE_MIN = np.array((12, 182, 221),np.uint8) 
ORANGE_MAX = np.array((16, 227, 255),np.uint8) 

hsv_disk = cv2.cvtColor(blurred,cv2.COLOR_BGR2HSV) 
disk_threshed = cv2.inRange(hsv_disk, ORANGE_MIN, ORANGE_MAX) 
+0

我會嘗試對邊緣像素進行hough變換。可能需要找到一個橢球體,因爲這不是一個圓圈。 – Photon

回答

0

我想有一個與色彩分割閾值問題,所以這裏的想法是生成二進制掩碼。通過檢查您感興趣的區域似乎比輸入圖像的其他區域更亮,因此可以在grayScale圖像上簡單地完成閾值處理以簡化上下文。 注意:您可以按照您的要求更改此步驟。在滿足閾值輸出後,您可以使用cv2.convexHull()來獲得輪廓的凸面形狀。

還要記住選擇最大的輪廓並忽略小輪廓。下面的代碼可以用來生成所需的輸出:只有紅色的平面上進行,當

import cv2 
import numpy as np 

# Loading the input_image 
img = cv2.imread("/Users/anmoluppal/Downloads/3xGG4.jpg") 
# Converting the input image to grayScale 
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 
# Thresholding the image to get binary mask. 
ret, img_thresh = cv2.threshold(img_gray, 145, 255, cv2.THRESH_BINARY) 

# Dilating the mask image 
kernel = np.ones((3,3),np.uint8) 
dilation = cv2.dilate(img_thresh,kernel,iterations = 3) 

# Getting all the contours 
_, contours, __ = cv2.findContours(dilation, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 

# Finding the largest contour Id 
largest_contour_area = 0 
largest_contour_area_idx = 0 

for i in xrange(len(contours)): 
    if (cv2.contourArea(contours[i]) > largest_contour_area): 
     largest_contour_area = cv2.contourArea(contours[i]) 
     largest_contour_area_idx = i 

# Get the convex Hull for the largest contour 
hull = cv2.convexHull(contours[largest_contour_area_idx]) 

# Drawing the contours for debugging purposes. 
img = cv2.drawContours(img, [hull], 0, [0, 255, 0]) 
cv2.imwrite("./garbage.png", img) 

enter image description here

1

任務要容易得多。

enter image description here