from gamera.core import *
from gamera.core import FloatPoint, RGBPixel


init_gamera()
image = load_image('pre.png')
image.despeckle(8)
image.dilate()
image.erode()


imgrgb = image.to_rgb()
seg = image.cc_analysis()

polars = []
for i, s1 in enumerate(seg):
    subpolars = []
    for j, s2 in enumerate(seg):
        if i < j:
            pol = s1.polar_distance(s2)
            if abs(pol[0]) < 6E-05 * pol[2]*pol[2] - 0.0108 * abs(pol[2]) + 0.9836 and abs(pol[2]) < 100:
                imgrgb.draw_line(FloatPoint(s1.center_x, s1.center_y), FloatPoint(s2.center_x, s2.center_y), RGBPixel(255, 0, 0), 1)
            polars.append(pol)
    print i

imgrgb.save_PNG("polar.png")