def testCentroid(self):
     print 'Centroid testing'
     a = np.arange(12.)
     x = np.arange(12.) + 2
     ca = np.centroid(a)
     self.assertEquals(ca[0], 539./66)
     ca = np.centroid(a, x)
     self.assertEquals(ca[0], 539./66 + 1.5)
     a.shape = (3,4)
     ca = np.centroid(a)
     self.assertEquals(ca[0], 131./66)
     self.assertEquals(ca[1], 147./66)
     x = np.arange(3.) + 2
     y = np.arange(4.) + 3
     ca = np.centroid(a, [x,y])
     self.assertEquals(ca[0], 131./66 + 1.5)
     self.assertEquals(ca[1], 312./66) #147./66 + 2.5)
Example #2
0
 def testCentroid(self):
     print 'Centroid testing'
     a = np.arange(12.)
     x = np.arange(12.) + 2
     ca = np.centroid(a)
     self.assertEquals(ca[0], 539. / 66)
     ca = np.centroid(a, x)
     self.assertEquals(ca[0], 539. / 66 + 1.5)
     a.shape = (3, 4)
     ca = np.centroid(a)
     self.assertEquals(ca[0], 131. / 66)
     self.assertEquals(ca[1], 147. / 66)
     x = np.arange(3.) + 2
     y = np.arange(4.) + 3
     ca = np.centroid(a, [x, y])
     self.assertEquals(ca[0], 131. / 66 + 1.5)
     self.assertEquals(ca[1], 312. / 66)  #147./66 + 2.5)
def analyseData(dd):
    #ss = dd.sum(0)
    #dnp.plot.image(ss)
    ##ss is the flat field
    print "Calculating median for the dataset ..."
    med = dnp.median(dd, 0)
    print "Median calculated"
    if plotData: dnp.plot.image(med)
    
    Sleep.sleep(2000)
    #flatFieldCorrectedSum = ss/med
    #dnp.plot.image(flatFieldCorrectedSum)
    
    xVals = []
    yVals = []
    ##
    print "Calculating centroid ..."
    centroids = []
    for i in range(18):
        #dnp.plot.image(dd[i, :, :] / med)
        im = dd[i, :, :] / med
        im = javaImage.medianFilter(im, [3, 3])
        im = dnp.array(im)
        threshold = 0.85
        if plotData: dnp.plot.image(im)
        if plotData: Sleep.sleep(1000)
        if plotData: dnp.plot.image(im < threshold)
        if plotData: Sleep.sleep(1000)
        cent = dnp.centroid(im < threshold)
        yVals.append(cent[0])
        xVals.append(cent[1])
        centroids.append(cent)
    print "y:" + `yVals`
    print('')
    print "x:" + `xVals`
    #help(dnp.fit.ellipsefit)
    
    if plotData: dnp.plot.line(dnp.array(xVals), dnp.array(yVals))
    ellipseFitValues = dnp.fit.ellipsefit(xVals, yVals)
    print "major: " + `ellipseFitValues[0]`
    print "minor semi-axis: " + `ellipseFitValues[1]`
    print "major axis angle: " + `ellipseFitValues[2]`
    print "centre co-ord 1: " + `ellipseFitValues[3]`
    print "centre co-ord 2: " + `ellipseFitValues[4]`
    print "centroids:" + `centroids`
    xOffset = getXOffset(ellipseFitValues[1], ellipseFitValues[0])
    zOffset = getZOffset(ellipseFitValues[2])
    return xOffset, zOffset, centroids, ellipseFitValues
def analyseData(dd):
    #ss = dd.sum(0)
    #dnp.plot.image(ss)
    ##ss is the flat field
    print "Calculating median for the dataset ..."
    med = dnp.median(dd, 0)
    print "Median calculated"
    if plotData: dnp.plot.image(med)

    sleep(2)
    #flatFieldCorrectedSum = ss/med
    #dnp.plot.image(flatFieldCorrectedSum)

    xVals = []
    yVals = []
    ##
    print "Calculating centroid ..."
    centroids = []
    for i in range(18):
        #dnp.plot.image(dd[i, :, :] / med)
        im = dd[i, :, :] / med
        im = javaImage.medianFilter(im, [3, 3])
        im = dnp.array(im)
        threshold = 0.85
        if plotData: dnp.plot.image(im)
        if plotData: sleep(1)
        if plotData: dnp.plot.image(im < threshold)
        if plotData: sleep(1)
        cent = dnp.centroid(im < threshold)
        yVals.append(cent[0])
        xVals.append(cent[1])
        centroids.append(cent)
    print "y:" + ` yVals `
    print('')
    print "x:" + ` xVals `
    #help(dnp.fit.ellipsefit)

    if plotData: dnp.plot.line(dnp.array(xVals), dnp.array(yVals))
    ellipseFitValues = dnp.fit.ellipsefit(xVals, yVals)
    print "major: " + ` ellipseFitValues[0] `
    print "minor semi-axis: " + ` ellipseFitValues[1] `
    print "major axis angle: " + ` ellipseFitValues[2] `
    print "centre co-ord 1: " + ` ellipseFitValues[3] `
    print "centre co-ord 2: " + ` ellipseFitValues[4] `
    print "centroids:" + ` centroids `
    xOffset = getXOffset(ellipseFitValues[1], ellipseFitValues[0])
    zOffset = getZOffset(ellipseFitValues[2])
    return xOffset, zOffset, centroids, ellipseFitValues
def sphere_alignment(threshold=0.55,
                     x_stage=ss1_x,
                     flat_field_position=-50,
                     projection_position=2.0,
                     theta_stage=ss1_theta,
                     exposure_time=0.06,
                     max_tries=15,
                     z_tilt_stage=ss1_rz,
                     x_tilt_stage=ss1_rx,
                     doscan=True,
                     working_directory='/dls/i12/data/2014/cm4963-2/rawdata',
                     flatfield_file=36466,
                     projection_file=36467):

    print "Running sphere alignment"
    # scan flat field
    # Run the scan, then call sphere_alignmet_processing(flatfilename, projectionfilename)

    if (doscan == True):
        print " Moving to flatfield position"  # this should be an optional step as it may not be possible to do this move without disrupting user equipment
        #pos ss1.x -200
        pos(x_stage, flat_field_position)

        print "Collecting flat field image"
        scan(ix, 1, 1, 1, pco4000_dio_hdf, exposure_time)
        time.sleep(5)
        working_dir = wd()
        current_file = cfn()
        filename = '%s/%d.nxs' % (working_dir, current_file)

        print "Loading flat field image to data processing pipeline"
        ff = None
        try_number = 0
        while (ff == None and try_number < max_tries):
            try_number += 1
            try:
                data = dnp.io.load(filename)
                ff = data['entry1']['pco4000_dio_hdf']['data'][...].mean(0)
            except:
                print("Failed to load %i, will try again in 5 seconds" %
                      (try_number))
                time.sleep(5)

        dnp.plot.image(ff)

        print "Moving to sample position"
        pos(x_stage, projection_position)

        print "Collecting projections"
        scan(theta_stage, 0, 360, 20, pco4000_dio_hdf, exposure_time)
        working_dir = wd()
        current_file = cfn()
        filename = '%s/%d.nxs' % (working_dir, current_file)

        time.sleep(5)

        print "Loading projections to data processing pipeline"
        #data = dnp.io.load(filename)
        dd = None
        try_number = 0
        while (dd == None and try_number < max_tries):
            try_number += 1
            try:
                data = dnp.io.load(filename)
                dd = data['entry1']['pco4000_dio_hdf']['data']
            except:
                print("Failed to load %i, will try again in 5 seconds" %
                      (try_number))
                time.sleep(5)

    else:
        print "Not Moving to flatfield position"  # this should be an optional step as it may not be possible to do this move without disrupting user equipment

        print "Not Collecting flat field image"
        time.sleep(5)
        working_dir = working_directory
        current_file = flatfield_file
        filename = '%s/%d.nxs' % (working_dir, current_file)

        print "Loading flat field image to data processing pipeline"
        ff = None
        try_number = 0
        while (ff == None and try_number < max_tries):
            try_number += 1
            try:
                data = dnp.io.load(filename)
                ff = data['entry1']['pco4000_dio_hdf']['data'][...].mean(0)
            except:
                print("Failed to load %i, will try again in 2 seconds" %
                      (try_number))
                time.sleep(2)

        dnp.plot.image(ff)

        print "Not Moving to sample position"

        print "Not Collecting projections"
        working_dir = working_directory
        current_file = projection_file
        filename = '%s/%d.nxs' % (working_dir, current_file)

        time.sleep(5)

        print "Loading projections into data processing pipeline"
        data = dnp.io.load(filename)
        dd = data['entry1']['pco4000_dio_hdf']['data']

    print "Processing data"

    # apply any filtering
    # exclude spheres which are not completely in the field of view

    print "slow look at all the data"
    preview = dd[:, :, :].mean(0) * 1.0 / ff
    dnp.plot.image(preview[10:-1, :])
    time.sleep(2)
    print "Generating centroid data"

    thresholdOK = False
    xs = []
    ys = []
    while not thresholdOK:
        xs = []
        ys = []
        for i in range(dd.shape[0]):
            cor = dd[i, :, :] * 1.0 / ff
            cor = cor[10:-1, :] < threshold
            dnp.plot.clear()
            #time.sleep(0.1)
            dnp.plot.image(cor)
            #time.sleep(.1)
            x, y = dnp.centroid(cor)
            xs.append(x)
            ys.append(y)

        xxx = dnp.array(ys)
        yyy = dnp.array(xs)

        responce = requestInput(
            "Current threshold is %f, y for ok, otherwise enter a new threshold value "
            % (threshold))
        if responce == 'y':
            thresholdOK = True
        else:
            try:
                threshold = float(responce)
            except:
                print "could not interpret %s as a float" % responce

    print "Fitting ellipse to centroid data"
    ellipseFitValues = dnp.fit.ellipsefit(xxx, yyy)

    t = dnp.arange(100) * dnp.pi / 50.
    fitplot = dnp.fit.makeellipse(ellipseFitValues, t._jdataset())

    dnp.plot.line(fitplot[0], fitplot[1])
    time.sleep(1)
    dnp.plot.addline(xxx, yyy)
    #print("XXX is:")
    #print (type(xxx))
    #print(xxx)
    print("ys is:")
    type(xs)
    print(xs)

    print "Results of ellipse fitting routine:"
    print "   Major: %f" % (ellipseFitValues[0])
    print "   Minor semi-axis: %f" % (ellipseFitValues[1])
    print "   Major axis angle: %f" % (ellipseFitValues[2])
    print "   Centre co-ord 1: %f" % (ellipseFitValues[3])
    print "   Centre co-ord 2: %f" % (ellipseFitValues[4])

    print "Calculating stage tilts"
    xangle = math.fabs(math.atan2(ellipseFitValues[1], ellipseFitValues[0]))
    #find out which direction
    npoints = len(xs)
    firstquarter = int(math.floor(npoints / 4.0))
    lastquarter = int(math.floor(3.0 * npoints / 4.0))
    print("   firstquarter %i %f lastquarter %i %f" %
          (firstquarter, xs[firstquarter], lastquarter, xs[lastquarter]))
    if (xs[firstquarter] > xs[lastquarter]):
        xangle = -math.degrees(xangle)
        print("   negative xangle %f" % xangle)
    else:
        xangle = math.degrees(xangle)
        print("   positive xangle %f" % xangle)

    #xangle = math.degrees(xangle) * -1.0

    # This needs to be calculated correctly
    zangle = math.degrees(ellipseFitValues[2])
    if zangle > 90.0:
        zangle -= 180.0

    print xangle
    print zangle

    responce = requestInput("Would you like to rotate the X tilt stage by %f" %
                            (xangle))
    if responce == 'y':
        print "Moving x tilt stage"
        pos(x_tilt_stage)
        inc(x_tilt_stage, xangle)
        pos(x_tilt_stage)
        print "In position."

    responce = requestInput("Would you like to rotate the Z tilt stage by %f" %
                            (zangle))
    if responce == 'y':
        print "Moving z tilt stage"
        pos(z_tilt_stage)
        inc(z_tilt_stage, zangle)
        pos(z_tilt_stage)
        print "In position."

    print "sphere_alignment finished"
def get_image_data(
    previewmean=False,
    preview_frames=True,
    visit_directory="/dls/i12/data/2014/cm4963-2/",
    flatfile=37384,
    projectionfile=37385,
    threshold=0.5,
    max_tries=15,
    prompt=False,
    request_input_command=request_DAWN_Input,
):

    working_directory = "%s/rawdata" % visit_directory
    flatfilename = "%s/%i.nxs" % (working_directory, flatfile)
    projectionfilename = "%s/%i.nxs" % (working_directory, projectionfile)

    # TODO put a unique time stamp on the otuput
    # TODO make a folder for the outputs

    print ("FlatFilename = %s" % flatfilename)
    print ("projectionfilename = %s" % projectionfilename)

    print "Loading flat field image %s\n\t to data processing pipeline" % flatfilename
    ff = None
    try_number = 0
    while ff == None and try_number < max_tries:
        try_number += 1
        try:
            print ("Trying to open the file %s" % (flatfilename))
            data = dnp.io.load(flatfilename)
            ff = data["entry1"]["pco4000_dio_hdf"]["data"][...].mean(0)
        except:
            print ("ff is not null is it %s" % ff)
            print ("ff is not null is of type %s" % type(ff))
            print ("Failed to load %i, will try again in 5 seconds" % (try_number))
            time.sleep(5)

    ff = dnp.array(javaImage.medianFilter(ff._jdataset(), [3, 3]))
    dnp.plot.image(ff)

    time.sleep(5)

    print "Loading projections %s into data processing pipeline" % projectionfilename
    data = dnp.io.load(projectionfilename)
    try:
        dd = data["entry1"]["pco4000_dio_hdf"]["data"]
    except:
        print "Couldn not find the data in %s" % projectionfilename
        raise

    print "Processing data"

    # apply any filtering
    # exclude spheres which are not completely in the field of view

    if previewmean:
        print "slow look at all the data"
        preview = dd[:, :, :].mean(0) * 1.0 / ff

        dnp.plot.image(preview[10:-1, :])
        time.sleep(2)
    print "Generating centroid data"

    thresholdOK = False
    xs = []
    ys = []
    while not thresholdOK:
        xs = []
        ys = []
        for i in range(dd.shape[0]):
            cor = dd[i, :, :] * 1.0 / ff
            # print "Applying median filter"
            cor = dnp.array(javaImage.medianFilter(cor._jdataset(), [3, 3]))
            cor = cor[10:-1, :] < threshold
            if preview_frames:
                dnp.plot.clear()
                time.sleep(0.01)
                dnp.plot.image(cor)
                time.sleep(0.2)
            y, x = dnp.centroid(cor)
            xs.append(x)
            ys.append(y)

        if prompt == False:
            thresholdOK = True
        else:
            response = request_input_command(
                "Current threshold is %f, y for ok, otherwise enter a new threshold value " % (threshold)
            )
            if response == "y":
                thresholdOK = True
            else:
                try:
                    threshold = float(response)
                except:
                    print "could not interpret %s as a float" % response

    return (xs, ys)
def sphere_alignment(threshold=0.55, x_stage=ss1_x, flat_field_position=-50, projection_position=2.0, theta_stage=ss1_theta, exposure_time=0.06, max_tries=15, z_tilt_stage=ss1_rz, x_tilt_stage=ss1_rx,doscan=True, working_directory='/dls/i12/data/2014/cm4963-2/rawdata', flatfield_file=36466, projection_file=36467):

    print "Running sphere alignment"
    # scan flat field
    # Run the scan, then call sphere_alignmet_processing(flatfilename, projectionfilename)
    
    if (doscan == True):
        print " Moving to flatfield position" # this should be an optional step as it may not be possible to do this move without disrupting user equipment
        #pos ss1.x -200
        pos(x_stage,flat_field_position)
        
        print "Collecting flat field image"
        scan(ix,1,1,1,pco4000_dio_hdf,exposure_time)
        time.sleep(5)
        working_dir = wd()
        current_file = cfn()
        filename = '%s/%d.nxs' % (working_dir, current_file)
        
        print "Loading flat field image to data processing pipeline"
        ff = None
        try_number = 0
        while (ff == None and try_number < max_tries):
            try_number += 1
            try:
                data = dnp.io.load(filename)
                ff = data['entry1']['pco4000_dio_hdf']['data'][...].mean(0)
            except:
                print("Failed to load %i, will try again in 5 seconds" % (try_number))
                time.sleep(5)
        
        dnp.plot.image(ff)
        
        print "Moving to sample position"
        pos(x_stage,projection_position)
        
        print "Collecting projections"
        scan(theta_stage,0,360,20,pco4000_dio_hdf,exposure_time)
        working_dir = wd()
        current_file = cfn()
        filename = '%s/%d.nxs' % (working_dir, current_file)
        
        time.sleep(5)
        
        print "Loading projections to data processing pipeline"
        #data = dnp.io.load(filename)
        dd = None
        try_number = 0
        while (dd == None and try_number < max_tries):
            try_number += 1
            try:
                data = dnp.io.load(filename)
                dd = data['entry1']['pco4000_dio_hdf']['data']
            except:
                print("Failed to load %i, will try again in 5 seconds" % (try_number))
                time.sleep(5)
        
                       
    else:
        print "Not Moving to flatfield position" # this should be an optional step as it may not be possible to do this move without disrupting user equipment
        
        print "Not Collecting flat field image"
        time.sleep(5)
        working_dir = working_directory
        current_file=flatfield_file
        filename = '%s/%d.nxs' % (working_dir, current_file)
        
        print "Loading flat field image to data processing pipeline"
        ff = None
        try_number = 0
        while (ff == None and try_number < max_tries):
            try_number += 1
            try:
                data = dnp.io.load(filename)
                ff = data['entry1']['pco4000_dio_hdf']['data'][...].mean(0)
            except:
                print("Failed to load %i, will try again in 2 seconds" % (try_number))
                time.sleep(2)
        
        dnp.plot.image(ff)
        
        print "Not Moving to sample position"
            
        print "Not Collecting projections"
        working_dir = working_directory
        current_file=projection_file
        filename = '%s/%d.nxs' % (working_dir, current_file)
        
        time.sleep(5)
        
        print "Loading projections into data processing pipeline"
        data = dnp.io.load(filename)
        dd = data['entry1']['pco4000_dio_hdf']['data']
    
    
    
    print "Processing data"
    
    # apply any filtering
    # exclude spheres which are not completely in the field of view
    
    print "slow look at all the data"
    preview = dd[:,:,:].mean(0)*1.0/ff
    dnp.plot.image(preview[10:-1,:])
    time.sleep(2)
    print "Generating centroid data"
    
    thresholdOK = False
    xs = []
    ys = []
    while not thresholdOK:
        xs = []
        ys = []
        for i in range(dd.shape[0]):
            cor = dd[i,:,:]*1.0/ff
            cor = cor[10:-1,:]<threshold
            dnp.plot.clear()
            #time.sleep(0.1)
            dnp.plot.image(cor)
            #time.sleep(.1)
            x,y = dnp.centroid(cor)
            xs.append(x)
            ys.append(y)
        
        xxx = dnp.array(ys)
        yyy = dnp.array(xs)
        
        responce = requestInput("Current threshold is %f, y for ok, otherwise enter a new threshold value " % (threshold))
        if responce == 'y' :
            thresholdOK = True
        else :
            try :
                threshold = float(responce)
            except :
                print "could not interpret %s as a float" % responce
    
    print "Fitting ellipse to centroid data"
    ellipseFitValues = dnp.fit.ellipsefit(xxx, yyy)
    
    t = dnp.arange(100)*dnp.pi/50.
    fitplot = dnp.fit.makeellipse(ellipseFitValues, t._jdataset())

    dnp.plot.line(fitplot[0], fitplot[1])
    time.sleep(1)
    dnp.plot.addline(xxx,yyy)
    #print("XXX is:")
    #print (type(xxx))
    #print(xxx)
    print ("ys is:")
    type (xs)
    print(xs)
    
    
    print "Results of ellipse fitting routine:"
    print "   Major: %f" % (ellipseFitValues[0])
    print "   Minor semi-axis: %f" % (ellipseFitValues[1])
    print "   Major axis angle: %f" % (ellipseFitValues[2])
    print "   Centre co-ord 1: %f" % (ellipseFitValues[3])
    print "   Centre co-ord 2: %f" % (ellipseFitValues[4])
    
    print "Calculating stage tilts"
    xangle = math.fabs( math.atan2(ellipseFitValues[1], ellipseFitValues[0]))
    #find out which direction
    npoints=len(xs)
    firstquarter=int(math.floor(npoints/4.0))
    lastquarter=int(math.floor(3.0*npoints/4.0))
    print("   firstquarter %i %f lastquarter %i %f"%(firstquarter,xs[firstquarter],lastquarter,xs[lastquarter]) )
    if (xs[firstquarter] > xs[lastquarter]):
        xangle=-math.degrees(xangle)
        print("   negative xangle %f"%xangle)
    else:
        xangle=math.degrees(xangle)
        print("   positive xangle %f"%xangle)

    #xangle = math.degrees(xangle) * -1.0
    
    # This needs to be calculated correctly
    zangle = math.degrees(ellipseFitValues[2])
    if zangle > 90.0:
        zangle -= 180.0
    
    print xangle
    print zangle
    
    
    responce = requestInput("Would you like to rotate the X tilt stage by %f" % (xangle))
    if responce == 'y' :
        print "Moving x tilt stage"
        pos(x_tilt_stage)
        inc(x_tilt_stage,xangle)
        pos(x_tilt_stage)
        print "In position."


    responce = requestInput("Would you like to rotate the Z tilt stage by %f" % (zangle))
    if responce == 'y' :
        print "Moving z tilt stage"
        pos(z_tilt_stage)
        inc(z_tilt_stage,zangle)
        pos(z_tilt_stage)
        print "In position."
        
    print "sphere_alignment finished"
def get_image_data(previewmean=False, preview_frames=True,visit_directory='/dls/i12/data/2014/cm4963-2/',flatfile=37384, projectionfile=37385, threshold=0.5, max_tries=15,prompt=False, request_input_command=request_DAWN_Input):

    working_directory="%s/rawdata"%visit_directory
    flatfilename="%s/%i.nxs"%(working_directory,flatfile)
    projectionfilename="%s/%i.nxs"%(working_directory,projectionfile)
    
    #TODO put a unique time stamp on the otuput
    #TODO make a folder for the outputs
    
    
    print("FlatFilename = %s"% flatfilename)
    print("projectionfilename = %s"% projectionfilename)
    
    print "Loading flat field image %s\n\t to data processing pipeline" % flatfilename
    ff = None
    try_number = 0
    while (ff == None and try_number < max_tries):
        try_number += 1
        try:
            print("Trying to open the file %s" % (flatfilename))
            data = dnp.io.load(flatfilename)
            ff = data['entry1']['pco4000_dio_hdf']['data'][...].mean(0)
        except:
            print("ff is not null is it %s" % ff)
            print("ff is not null is of type %s" % type(ff))
            print("Failed to load %i, will try again in 5 seconds" % (try_number))
            time.sleep(5)
    
    ff = dnp.array(javaImage.medianFilter(ff._jdataset(), [3, 3]))
    dnp.plot.image(ff)
    
    time.sleep(5)

    
    print "Loading projections %s into data processing pipeline"% projectionfilename
    data = dnp.io.load(projectionfilename)
    try:
        dd = data['entry1']['pco4000_dio_hdf']['data']
    except:
        print"Couldn not find the data in %s"%projectionfilename
        raise
    
    
    print "Processing data"
    
    # apply any filtering
    # exclude spheres which are not completely in the field of view
    
    if (previewmean):
        print "slow look at all the data"
        preview = dd[:,:,:].mean(0)*1.0/ff
        
        dnp.plot.image(preview[10:-1,:])
        time.sleep(2)
    print "Generating centroid data"
    
    thresholdOK = False
    xs = []
    ys = []
    while not thresholdOK:
        xs = []
        ys = []
        for i in range(dd.shape[0]):
            cor = dd[i,:,:]*1.0/ff
            #print "Applying median filter"
            cor = dnp.array(javaImage.medianFilter(cor._jdataset(), [3, 3]))
            cor = cor[10:-1,:]<threshold
            if (preview_frames):
                dnp.plot.clear()
                time.sleep(0.01)
                dnp.plot.image(cor)
                time.sleep(.2)
            y,x = dnp.centroid(cor)
            xs.append(x)
            ys.append(y)
        

        if (prompt==False):
            thresholdOK=True
        else:
            response = request_input_command("Current threshold is %f, y for ok, otherwise enter a new threshold value " % (threshold))
            if response == 'y' :
                thresholdOK = True
            else :
                try :
                    threshold = float(response)
                except :
                    print "could not interpret %s as a float" % response
                    
    return(xs,ys)
#res = javaImage.medianFilter(res, [3, 3])
#

dnp.plot.image(res)

res = res > 0.5

dnp.plot.image(res)

print "Calculting coms"

points = []
for i in range(res.shape[1]):
    a = res[:,i:i+1].mean(1)
    points.append(dnp.centroid(a))

coms = dnp.array(points)
coms.squeeze()
dnp.plot.line(coms)

p = dnp.fit.polyfit(dnp.arange(coms.shape[0]), coms, 1, full=True)

p[1].plot()

rotation = math.degrees(math.atan2(p[0][0],1.))

responce = requestInput("Would you like to rotate the camera stage by %f" % (rotation))

if responce == 'y' :
    print "Moving cam1.roll"