2017-04-16 34 views
1

給定一個固定大小的二進制正方形數組,如下圖所示。 預先假定該數組包含一個圓或其一部分的圖像。這個圈子總是以圖像爲中心是很重要的。如何將圓弧延伸至完整的圓形?

Example

有必要找到弧補充到整圓的有效方法,如果可能的話。

我試圖統計計算從中心到白點的平均距離並完成圓圈。它的工作原理。我也嘗試了霍夫變換來擬合橢圓並確定其大小。但是這兩種方法都非常耗費資源。

1方法的草圖:

points = np.transpose(np.array(np.nonzero(array))).tolist() # array of one-value points 
random.shuffle(points) 
points = np.array(points[:500]).astype('uint8') # take into account only 500 random points 

distances = np.zeros(points.shape[0], dtype='int32') # array of distances from the centre of image (40, 40) to some point 
for i in xrange(points.shape[0]): 
    distances[i] = int(np.sqrt((points[i][0] - 40) ** 2 + (points[i][1] - 40) ** 2)) 

u, indices = np.unique(distances, return_inverse=True) 
mean_dist = u[np.argmax(np.bincount(indices))] # most probable distance 
# use this mean_dist in order to draw a complete circle 

1 method result

2方法的草圖:

from skimage.transform import hough_ellipse 

result = hough_ellipse(array, min_size=..., max_size=...) 
result.sort(order='accumulator') 
# ... extract the necessary info from result variable if it's not empty 

可能有人建議另一個和有效的解決方案?謝謝!

+0

顯示您的嘗試! – Astrom

+0

這是一個有效且很好的問題,但沒有代碼,它看起來非常寬泛,並且沒有針對任何給定的標籤。它可能更適合其他SE站點,如DSP。 – iled

回答

0

我試着統計計算從中心到白點的平均距離並完成圓圈。

那麼這似乎是一個好的開始。給定n像素的圖像,該算法是O(n),這已經非常有效。

如果你想有一個更快的實現,請嘗試使用隨機化:

採取從圖像m隨機採樣點,並使用這些來計算白點的平均半徑。然後用這個半徑完成圓。

此算法將有O(m)這意味着它對所有m < n更快。爲m選擇合適的值可能會非常棘手,因爲您必須在運行時間和輸出質量之間做出妥協。

+0

是的,這很清楚。但它不夠好,因爲有數以百萬計的這樣的數組,並且它們都不相同。因此這種方法並不適合。還有一個瘋狂的想法來訓練一個神經網絡來補充圓的弧度:) –

+0

你的意思是它不夠快,或者它不能補充所有情況下的圓圈? – Felix

+0

如果是關於速度,請顯示您的代碼。如果是關於結果,則顯示一些不起作用的情況。 – Felix