2012-03-12 23 views
1

我一直在試圖弄清楚如何在OpenCV中平均2點,但每當我嘗試一些東西時,我通常會得到一個涉及不正確使用元組的錯誤。我試圖找到cv.HoughLines2返回的行的中心點。我知道line [0]存儲(x1,y1)和line [1]存儲(x2,y2)項,但我不知道如何平均以便我可以在cv2.circle()中使用它們。如何在OpenCV中平均python兩點?

如何在OpenCV中平均兩點?

lines = cv.HoughLines2(dst, storage, cv.CV_HOUGH_PROBABILISTIC, rho, cv.CV_PI/(theta+1), thresh_1, min_len, max_dist) 

i = 0 
avr = 0 
for line in lines: 
     #print line 

     cv.Line(image, line[0], line[1], cv.CV_RGB(255, 0, 0), 1, 8) 
     i = i+1 
     avr = (line[0] + line [1])/2 

     cv2.circle(image, avr, 4, cv.CV_RGB(0, 255, 0), 1, 8, 0) 

回答

0

您將x和y分別平均並重新組合到一個元組中。

我忘了Python語法,所以僞代碼將是:

avg=[ (point1_x+point2_x)/2, (point1_y+point2_y)/2 ] 
0

Python中添加元組似乎將它們串聯:

In [1]: (1, 2) + (2, 1) 
Out[1]: (1, 2, 2, 1) 

這不是你想要的。嘗試:

avr = ((line[0][0] + line[1][0])/2, (line[0][1] + line[1][1])/2) 
0

嘗試:

avr = cv.cvPoint((line[0].x + line [1].x)/2, (line[0].y + line [1].y)/2)