我試圖實現此方程以確定從三個用戶選擇的點的圓的中心:http://en.wikipedia.org/wiki/Circumscribed_circle#Cartesian_coordinates是一個左上角的原點座標系什麼阻止了這個方程的工作?
首先,點由這OpenCV的鼠標回調函數的裝置獲取並組裝成一個列表:
def setupPoints(event, x, y, flags, points):
#Populates a provided list 'points' with
#three coordinates representing the edge of
#a circle
if points[0] == 0:
points[0] = (x,y)
elif points[1] == 0:
points[1] = (x,y)
else:
points[2] = (x,y)
然後我通過點列表這個功能,它做的工作:
def findCircle(p):
#Returns the circle centre
#from three provided points provided as tuples
#in a list
#See http://en.wikipedia.org/wiki/Circumscribed_circle#Cartesian_coordinates
ax = float(p[0][0])
ay = float(p[0][1])
bx = float(p[1][0])
by = float(p[1][1])
cx = float(p[2][0])
cy = float(p[2][1])
d = 2*(ax*(by-cy)+bx*(cy-ay)+cx*(ay-by))
centrex = ((pow(ax,2)+pow(ay,2))*(by-cy)+(pow(bx,2)+pow(by,2))*(cy-ay)+(pow(cx,2)+pow(cy,2))*(ay-by))/d
centrey = ((pow(ax,2)+pow(ay,2))*(cx-bx)+(pow(bx,2)+pow(by,2))*(ax-cx)+(pow(cx,2)+pow(cy,2))*(bx-ax))/d
return (int(round(centrex)), int(round(centrey)), int(round(d)))
但是,它不工作。返回的數字不是大量的,但肯定是不正確的。這可能是因爲OpenCV使用的座標系統的原點位於圖像的左上角(圖像中的點仍然是正數,所以可以說它是「向後」計數,至少垂直)。
或者這是猜錯了?
你可以與一些你知道的數字測試,那麼你就可以在輸入的數字是錯誤的座標系的腳的問題。手動做數學運算,讓你知道答案,輸入數字。在錯誤的座標系中再次輸入數字。 – Paystey 2012-04-17 19:54:14
你說得對,我應該先在紙上檢查一遍。當它發生時,函數按預期工作,但至少在檢查後我可以確信問題不在findCircle函數中 - 這導致我找到解決方案(參見下文) – Alex 2012-04-17 21:30:47