예제 #1
0
 def checkArrayMinMax(self, arrayold, arraynew):
     openCVcaller.checkArrayMinMax(self, arrayold, arraynew)
예제 #2
0
    def trackStage(self, image0, tilt0, tilt, tilt0targets):
        self.logger.info('Running trackStage')
        retriesmax = 15
        retries = retriesmax
        blur = 3
        thresh = 0
        self.logger.info('Returning to state of image0')
        presetname = image0['preset']['name']
        emtarget = image0['emtarget']
        pausetime = self.settings['pause']
        self.presetsclient.toScope(presetname, emtarget)
        ### reset the tilt, just in case user changed it while picking targets
        self.instrument.tem.StagePosition = {'a': tilt0}
        if pausetime > 0.1:
            self.logger.info('Pausing %.1f seconds' % (pausetime, ))
            time.sleep(pausetime)

        ### calculate tilt steps
        maxstepsize = radians(self.settings['stepsize'])
        tilts = self.calculateTiltSteps(tilt0, tilt, maxstepsize)
        self.logger.info('Tilts: %s' % ([("%.1f" % degrees(t))
                                         for t in tilts], ))

        imageold = image0
        arrayold = numpy.asarray(imageold['image'], dtype=numpy.float32)
        self.setImage(arrayold, 'Image')
        runningresult = numpy.identity(3, numpy.float32)

        # transformTargets for display purposes only
        self.transformTargets(runningresult, tilt0targets)

        #for tilt in tilts:
        ### use while loop so we can backtrack
        i = 0
        while i < len(tilts) - 1:
            i += 1
            tilt = float("%.3f" % tilts[i])
            self.logger.info('Going to tilt angle: %.2f' % (degrees(tilt), ))
            self.instrument.tem.StagePosition = {'a': tilt}
            is_small_tilt_diff = self.isSmallTiltDifference(tilts, i, tilt0)
            if pausetime > 0.1:
                self.logger.info('Pausing %.1f seconds' % (pausetime, ))
                time.sleep(pausetime)
            self.logger.info('Acquire intermediate tilted parent image')
            imagenew = self.acquireCorrectedCameraImageData()
            arraynew = numpy.asarray(imagenew['image'], dtype=numpy.float32)
            self.setImage(arraynew, 'Image')

            if is_small_tilt_diff:
                self.logger.info('Use phase correlation on small tilt')
                result = numpy.array(
                    self.shiftmatrix_maker.register(arrayold, arraynew))
            else:

                print '============ openCV stuff ============'

                self.logger.info('openCV stuff')
                minsize = self.settings['minsize']
                maxsize = self.settings['maxsize']
                openCVcaller.checkArrayMinMax(self, arrayold, arraynew)
                print 'tilt', tilts[i] * 180 / 3.14159
                result = openCVcaller.MatchImages(arrayold, arraynew, blur,
                                                  thresh)
                self.logger.info(
                    "result matrix= " +
                    str(numpy.asarray(result * 100, dtype=numpy.int8).ravel()))
                print "RESULT", result

                check = openCVcaller.checkOpenCVResult(self, result,
                                                       is_small_tilt_diff)
                if check is False:
                    self.logger.warning("openCV failed: redoing tilt %.2f" %
                                        (tilt, ))
                    if retries:
                        i -= 1
                        retries -= 1
                        if retries <= retriesmax / 2:
                            thresh = 1
                            print "THRESH = 1"
                        print "retries =", retries, "out of", retriesmax
                    else:
                        print "Tilt openCV FAILED"
                        self.logger.error("openCV failed: giving up")
                        self.instrument.tem.StagePosition = {'a': tilt0}
                        return None, None
                    continue
                else:
                    retries = 0
                print '============ openCV stuff done ============'

            self.logger.info(
                "result matrix= " +
                str(numpy.asarray(result * 100, dtype=numpy.int8).ravel()))
            self.logger.info("Inter Matrix: " +
                             openCVcaller.affineToText(result))

            runningresult = numpy.dot(runningresult, result)
            # transformTargets for display purposes only
            self.transformTargets(runningresult, tilt0targets)
            self.logger.info("Running Matrix: " +
                             openCVcaller.affineToText(runningresult))
            self.logger.info("running result matrix= " + str(
                numpy.asarray(runningresult * 100, dtype=numpy.int8).ravel()))
            imageold = imagenew
            arrayold = arraynew

        ### copied from Acquisition.acquire:
        ## store EMData to DB to prevent referencing errors
        self.publish(imageold['scope'], database=True)
        self.publish(imageold['camera'], database=True)

        ## convert CameraImageData to AcquisitionImageData
        dim = image0['camera']['dimension']
        pixels = dim['x'] * dim['y']
        pixeltype = str(image0['image'].dtype)
        imagedata = leginondata.AcquisitionImageData(
            initializer=imageold,
            preset=image0['preset'],
            label=self.name,
            target=image0['target'],
            list=None,
            emtarget=image0['emtarget'],
            version=0,
            tiltnumber=self.tiltnumber,
            pixels=pixels,
            pixeltype=pixeltype)
        self.setTargets([], 'Peak')
        self.publishDisplayWait(imagedata)

        self.logger.info("FINAL Matrix: " +
                         openCVcaller.affineToText(runningresult))
        return (runningresult, imagedata)
	def checkArrayMinMax(self,arrayold, arraynew):
		openCVcaller.checkArrayMinMax(self, arrayold, arraynew)
    def trackStage(self, image0, tilt0, tilt, tilt0targets):
        self.logger.info('Running trackStage')
        retriesmax = 15
        retries = retriesmax
        blur = 3
        thresh = 0
        self.logger.info('Returning to state of image0')
        presetname = image0['preset']['name']
        emtarget = image0['emtarget']
        pausetime = self.settings['pause']
        self.presetsclient.toScope(presetname, emtarget)
        ### reset the tilt, just in case user changed it while picking targets
        self.instrument.tem.StagePosition = {'a': tilt0}
        if pausetime > 0.1:
            self.logger.info('Pausing %.1f seconds' %(pausetime,))
            time.sleep(pausetime)

        ### calculate tilt steps
        maxstepsize = radians(self.settings['stepsize'])
        tilts = self.calculateTiltSteps(tilt0, tilt, maxstepsize)
        self.logger.info('Tilts: %s' % ([("%.1f"%degrees(t)) for t in tilts],))

        imageold = image0
        arrayold = numpy.asarray(imageold['image'], dtype=numpy.float32)
        self.setImage(arrayold, 'Image')
        runningresult = numpy.identity(3, numpy.float32)

        # transformTargets for display purposes only
        self.transformTargets(runningresult, tilt0targets)

        #for tilt in tilts:
        ### use while loop so we can backtrack
        i = 0
        while i < len(tilts)-1:
            i+=1
            tilt = float("%.3f"%tilts[i])
            self.logger.info('Going to tilt angle: %.2f' % (degrees(tilt),))
            self.instrument.tem.StagePosition = {'a': tilt}
            is_small_tilt_diff = self.isSmallTiltDifference(tilts,i,tilt0)
            if pausetime > 0.1:
                self.logger.info('Pausing %.1f seconds' %(pausetime,))
                time.sleep(pausetime)
            self.logger.info('Acquire intermediate tilted parent image')
            imagenew = self.acquireCorrectedCameraImageData()
            arraynew = numpy.asarray(imagenew['image'], dtype=numpy.float32)
            self.setImage(arraynew, 'Image')

            if is_small_tilt_diff:
                self.logger.info('Use phase correlation on small tilt')
                result = numpy.array(self.shiftmatrix_maker.register(arrayold, arraynew))
            else:

                print '============ openCV stuff ============'

                self.logger.info('openCV stuff')
                minsize = self.settings['minsize']
                maxsize = self.settings['maxsize']
                openCVcaller.checkArrayMinMax(self, arrayold, arraynew)
                print 'tilt', tilts[i]*180/3.14159
                result = openCVcaller.MatchImages(arrayold, arraynew, blur, thresh)
                self.logger.info("result matrix= "+str(numpy.asarray(result*100, dtype=numpy.int8).ravel()))
                print "RESULT", result

                check = openCVcaller.checkOpenCVResult(self, result, is_small_tilt_diff)
                if check is False:
                    self.logger.warning("openCV failed: redoing tilt %.2f"%(tilt,))
                    if retries:
                        i -= 1
                        retries -= 1
                        if retries <= retriesmax/2:
                            thresh = 1
                            print "THRESH = 1"                         
                        print "retries =", retries, "out of", retriesmax
                    else:
                        print "Tilt openCV FAILED"
                        self.logger.error("openCV failed: giving up")
                        self.instrument.tem.StagePosition = {'a': tilt0}
                        return None, None
                    continue
                else:
                    retries = 0         
                print '============ openCV stuff done ============'

            self.logger.info("result matrix= "+str(numpy.asarray(result*100, dtype=numpy.int8).ravel()))
            self.logger.info( "Inter Matrix: "+openCVcaller.affineToText(result) )

            runningresult = numpy.dot(runningresult, result)
            # transformTargets for display purposes only
            self.transformTargets(runningresult, tilt0targets)
            self.logger.info( "Running Matrix: "+openCVcaller.affineToText(runningresult) )
            self.logger.info("running result matrix= "+str(numpy.asarray(runningresult*100, dtype=numpy.int8).ravel()))
            imageold = imagenew
            arrayold = arraynew

        ### copied from Acquisition.acquire:
        ## store EMData to DB to prevent referencing errors
        self.publish(imageold['scope'], database=True)
        self.publish(imageold['camera'], database=True)

        ## convert CameraImageData to AcquisitionImageData
        dim = image0['camera']['dimension']
        pixels = dim['x'] * dim['y']
        pixeltype = str(image0['image'].dtype)
        imagedata = leginondata.AcquisitionImageData(initializer=imageold, preset=image0['preset'], label=self.name,
            target=image0['target'], list=None, emtarget=image0['emtarget'], 
            version=0, tiltnumber=self.tiltnumber, pixels=pixels, pixeltype=pixeltype)
        self.setTargets([], 'Peak')
        self.publishDisplayWait(imagedata)

        self.logger.info( "FINAL Matrix: "+openCVcaller.affineToText(runningresult) )
        return (runningresult, imagedata)