Reputation: 11
I coded for circle detection in an image using python. I have the centre coordinate of those circle in pixel. But I am not able to measure the centre to centre distance between two circles.
I am using the Hough transformation method to detect circles.
Can anyone please suggest me how should I do it?
Upvotes: 1
Views: 263
Reputation: 5992
First, you need to compute the height and width of a rectangle, that has two non-consecutive corners on the centers of your circles. Then, you can calculate the length of the diagonal of that rectangle using the Pythagorean theorem. I made a little sketch where you can see which variable is which point / distance:
And here comes the code (I assume you have the centre coordinates of the circles in form of two two-item tuples or lists):
import math
c1 = (20, 40) # replace these with the real values of your circle centers
c2 = (50, 50)
def calculate_diagonal(c1, c2):
x1, y1 = c1
x2, y2 = c2
if x1 < x2:
width = x2 - x1
elif x1 == x2:
width = 0
else:
width = x1 - x2
if y1 < y2:
height = y2 - y1
elif y1 == y2:
height = 0
else:
height = y1 - y2
if height == 0:
return width
elif width == 0:
return height
return math.sqrt((width ** 2) + (height ** 2))
d = calculate_diagonal(c1, c2)
print(d)
Upvotes: 1