from ijutils import *
from mathutils import fit_gaussian
from ch.psi.pshell.imaging.Overlays import *
import ch.psi.pshell.imaging.Pen as Pen


image = img
image = tst

def wait_next():
    camera.waitNext(250)

def get_centroid():
    bi = image.getImage()
    op = show_panel(bi, "Original")
    if bi is None:
        return None
    ip = load_image(bi)    
    grayscale(ip)    
    invert(ip)
    gaussian_blur(ip)    
    auto_threshold(ip)
    
    #binary_erode(ip)        
    show_panel(ip.getBufferedImage(), "Image")
    (results,output_img)=analyse_particles(ip, 1000,20000, print_table=True)    
    op.clearOverlays()
    show_panel(output_img.getBufferedImage(), "Outlines")
    if results.size()>0:
        print results        
        centroid  = (results.getValue("XM",0), results.getValue("YM",0))
        ov = Crosshairs(Pen(java.awt.Color.ORANGE),  java.awt.Point(int(centroid[0]),int(centroid[1])), java.awt.Dimension(15,15))
        op.addOverlay(ov)
        return centroid
        
       

get_centroid()    