Ejemplo n.º 1
0
    def OnAutoFocus(self, evt=None):
        """
        please read Chromagnon.alignfuncs.findBestRefZs() for detail of the logic
        """
        if self.doc.nt > 1:
            t = int(self.tSliderBox.GetValue())
        else:
            t = 0
        ws = [
            w for w, hist in enumerate(self.hist_toggleButton)
            if hist.GetValue()
        ]

        ms = N.zeros((len(ws), self.doc.nz), N.float32)

        # FFTW does not work with another program using it
        # here is the workaround for Chromagnon
        try:
            batch = self.GetParent().GetParent().GetParent()
            if hasattr(batch, 'th') and batch.th.isAlive():
                for wi, w in enumerate(ws):
                    arr = self.doc.get3DArr(t=t, w=w)
                    for z in range(self.doc.nz):
                        ms[wi, z] = N.prod(U.mmms(arr[z])[-2:])  # mean * std
                v, _, w, z = U.findMax(ms)
                self.zSliderBox.SetValue(str(z))
                self.OnZSliderBox()
                self.OnAutoScale()

                G.openMsg(
                    parent=self.parent,
                    msg=
                    'A clumsy focusing method was used since another program was using FFTW.\nPlease wait for the better method until the other program is done.',
                    title="Please wait")
                return
        except AttributeError:  # no parent?
            pass

        # Frequency-based caluculation starts
        from Priithon.all import F

        ring = F.ringArr(self.doc.shape[-2:],
                         radius1=self.doc.shape[-1] // 10,
                         radius2=self.doc.shape[-2] // 4,
                         orig=(0, 0),
                         wrap=1)

        for wi, w in enumerate(ws):
            arr = self.doc.get3DArr(t=t, w=w)
            arr = arr / arr.mean()
            for z in range(self.doc.nz):
                af = F.rfft(N.ascontiguousarray(arr[z]))
                ar = af * ring[:, :af.shape[-1]]
                ms[wi, z] = N.sum(N.abs(ar))
        v, _, w, z = U.findMax(ms)

        self.zSliderBox.SetValue(str(z))
        self.OnZSliderBox()
        self.OnAutoScale()
Ejemplo n.º 2
0
    def OnAutoFocus(self, evt=None):
        """
        please read Chromagnon.alignfuncs.findBestRefZs() for detail of the logic
        """
        if self.doc.nt > 1:
            t = int(self.tSliderBox.GetValue())
        else:
            t = 0
        ws = [w for w, hist in enumerate(self.hist_toggleButton) if hist.GetValue()]

        ms = N.zeros((len(ws),self.doc.nz), N.float32)

        # FFTW does not work with another program using it
        # here is the workaround for Chromagnon
        try:
            batch = self.GetParent().GetParent().GetParent()
            if hasattr(batch, 'th') and batch.th.isAlive():
                for wi, w in enumerate(ws):
                    arr = self.doc.get3DArr(t=t, w=w)
                    for z in range(self.doc.nz):
                        ms[wi,z] = N.prod(U.mmms(arr[z])[-2:]) # mean * std
                v,_,w,z = U.findMax(ms)
                self.zSliderBox.SetValue(str(z))
                self.OnZSliderBox()
                self.OnAutoScale()
                        
                G.openMsg(parent=self.parent, msg='A clumsy focusing method was used since another program was using FFTW.\nPlease wait for the better method until the other program is done.', title="Please wait")
                return
        except AttributeError: # no parent?
            pass

        # Frequency-based caluculation starts 
        from Priithon.all import F
        
        ring = F.ringArr(self.doc.shape[-2:], radius1=self.doc.shape[-1]//10, radius2=self.doc.shape[-2]//4, orig=(0,0), wrap=1)


        for wi, w in enumerate(ws):
            arr = self.doc.get3DArr(t=t, w=w)
            arr = arr / arr.mean()
            for z in range(self.doc.nz):
                af = F.rfft(N.ascontiguousarray(arr[z]))
                ar = af * ring[:,:af.shape[-1]]
                ms[wi,z] = N.sum(N.abs(ar))
        v,_,w,z = U.findMax(ms)

        self.zSliderBox.SetValue(str(z))
        self.OnZSliderBox()
        self.OnAutoScale()
Ejemplo n.º 3
0
def findBestRefZs(ref, sigma=0.5):
    """
    PSF spread along the Z axis is often tilted in the Y or X axis.
    Thus simple maximum intensity projection may lead to the wrong answer to estimate rotation and magnification.
    On the other hands, taking a single section may also contain out of focus flare from neighboring sections that are tilted.
    Therefore, here sections with high complexity are selected and projected.

    An intensity-based method does not work for most (eg. tissue or bright field) images.
    Using variance is not enough for bright field image where the right focus loses contrast.
    Thus here we used sections containing higher frequency.

    ref: 3D array
    return z idx at the focus
    """
    from Priithon.all import F
    nz = ref.shape[0]
    if ref.ndim == 2:
        return [0]
    elif nz <= 3:
        return range(nz)

    # ring array
    ring = F.ringArr(ref.shape[-2:],
                     radius1=ref.shape[-1] // 10,
                     radius2=ref.shape[-2] // 4,
                     orig=(0, 0),
                     wrap=1)

    # Due to the roll up after FFT, the edge sections in Z may contain different information among the channels. Thus these sections are left unused.
    ms = N.zeros((nz - 2, ), N.float32)
    for z in xrange(1, nz - 1):
        af = F.rfft(N.ascontiguousarray(ref[z]))
        ar = af * ring[:, :af.shape[-1]]
        ms[z - 1] = N.sum(N.abs(ar))

        #ms[z-1] = N.prod(U.mmms(arr)[-2:]) # mean * std
    del ring, af, ar

    mi, ma, me, st = U.mmms(ms)
    thr = me + st * sigma

    ids = [idx for idx in range(1, nz - 1) if ms[idx - 1] > thr]
    if not ids:
        ids = range(1, nz - 1)

    return ids