예제 #1
0
def main():
    normalized = i3.normalize(ct_series,
                              dfs,
                              obs,
                              workdir=os.path.join(workdir, 'normalization'))
    tilt_corrected = i3.correct_tilt(normalized,
                                     workdir=os.path.join(
                                         workdir, 'tilt-correction'))
    if_corrected = i3.correct_intensity_fluctuation(
        tilt_corrected,
        workdir=os.path.join(workdir, 'intensity-fluctuation-correction'))
    angles, sinograms = i3.build_sinograms(if_corrected,
                                           workdir=os.path.join(
                                               workdir, 'sinogram'))
    # take the middle part to calculate the center of rotation
    sino = [s.data for s in sinograms[900:1100]]
    sino = np.array(sino)
    proj = np.swapaxes(sino, 0, 1)
    rot_center = tomopy.find_center(proj,
                                    theta,
                                    emission=False,
                                    init=1024,
                                    tol=0.5)
    rot_center = rot_center[0]
    # reconstruct
    recon = i3.reconstruct(angles,
                           sinograms,
                           workdir=outdir,
                           center=rot_center)
    return
예제 #2
0
 def recon(self, workdir=None, outdir=None, tilt=None, crop_window=None,
           **kwds):
     workdir = workdir or self.workdir;  outdir = outdir or self.outdir
     # preprocess
     if_corrected = self.preprocess(workdir=workdir, outdir=outdir)
     if crop_window is None:
         # auto-cropping
         cropped = self.autoCrop(if_corrected)
     else:
         xmin, ymin, xmax, ymax = crop_window
         cropped = self.crop(
             if_corrected, 
             left=xmin, right=xmax, top=ymin, bottom=ymax)
     if self.clean_on_the_fly:
         if_corrected.removeAll()
     # smoothing
     # pre = smoothed = self.smooth(cropped, 5)
     pre = cropped
     if tilt is None:
         tilt_corrected, tilt = self.correctTilt_loop(
             pre, workdir=workdir)
     else:
         tilt_corrected, tilt = i3.correct_tilt(
             pre, tilt=tilt, 
             workdir=os.path.join(workdir, 'tilt-correction' ),
             max_npairs=None, parallel=self.parallel_preprocessing)
     if self.clean_on_the_fly:
         cropped.removeAll()
     # reconstruct
     self.reconstruct(tilt_corrected, workdir=workdir, outdir=outdir, **kwds)
     return
예제 #3
0
def test():
    normalized = i3.normalize(ct_series, dfs, obs, workdir=workdir)
    tilt_corrected = i3.correct_tilt(normalized, workdir=workdir)
    if_corrected = i3.correct_intensity_fluctuation(tilt_corrected,
                                                    workdir=workdir)
    angles, sinograms = i3.build_sinograms(if_corrected, workdir=workdir)
    recon = i3.reconstruct(angles, sinograms, workdir=outdir)
    return
예제 #4
0
def main():
    normalized = i3.normalize(ct_series, dfs, obs, workdir=os.path.join(workdir, 'normalization'))
    tilt_corrected = i3.correct_tilt(normalized, workdir=os.path.join(workdir, 'tilt-correction'))
    if_corrected = i3.correct_intensity_fluctuation(tilt_corrected, workdir=os.path.join(workdir, 'intensity-fluctuation-correction'))
    angles, sinograms = i3.build_sinograms(if_corrected, workdir=os.path.join(workdir, 'sinogram'))
    # take the middle part to calculate the center of rotation
    sino = [s.data for s in sinograms[900:1100]]
    sino= np.array(sino)
    proj = np.swapaxes(sino, 0, 1)
    rot_center = tomopy.find_center(proj, theta, emission=False, init=1024, tol=0.5)
    rot_center = rot_center[0]
    # reconstruct
    recon = i3.reconstruct(angles, sinograms, workdir=outdir, center=rot_center)
    return
예제 #5
0
 def correctTilt_loop(self, pre, workdir):
     # correct tilt
     MAX_TILT_ALLOWED = 0.05
     NROUNDS = 3
     for i in range(NROUNDS):
         tilt_corrected, tilt = i3.correct_tilt(
             pre, workdir=os.path.join(workdir, 'tilt-correction-%s' % i),
             max_npairs=None, parallel=self.parallel_preprocessing)
         if self.clean_on_the_fly:
             pre.removeAll()
         if abs(tilt) < MAX_TILT_ALLOWED: break
         pre = tilt_corrected
         continue
     if abs(tilt) >= MAX_TILT_ALLOWED:
         msg = "failed to bring tilt down to less than %s degrees in %s rounds" % (MAX_TILT_ALLOWED, NROUNDS)
         # raise RuntimeError(msg)
         import warnings
         warnings.warn(msg)
     return tilt_corrected, tilt
예제 #6
0
 def correctTilt_loop(self, pre, workdir):
     # correct tilt
     MAX_TILT_ALLOWED = 0.05
     NROUNDS = 3
     for i in range(NROUNDS):
         tilt_corrected, tilt = i3.correct_tilt(
             pre, workdir=os.path.join(workdir, 'tilt-correction-%s' % i),
             max_npairs=None, parallel=self.parallel_preprocessing)
         if self.clean_intermediate_files == 'on_the_fly':
             pre.removeAll()
         if abs(tilt) < MAX_TILT_ALLOWED: break
         pre = tilt_corrected
         continue
     if abs(tilt) >= MAX_TILT_ALLOWED:
         msg = "failed to bring tilt down to less than %s degrees in %s rounds" % (MAX_TILT_ALLOWED, NROUNDS)
         # raise RuntimeError(msg)
         import warnings
         warnings.warn(msg)
     return tilt_corrected, tilt
예제 #7
0
    def recon(self,
              workdir=None, outdir=None, outfilename_template=None,
              tilt=None, crop_window=None,
              smooth_projection=None, remove_rings_at_sinograms=None,
              smooth_recon=None, remove_rings=None,
              **kwds):
        """Run CT reconstruction workflow

    Parameters:
        * workdir: fast work dir
        * outdir: output dir. can be at a slower disk
        * crop_window: (xmin, ymin, xmax, ymax)
        * remove_rings_at_sinograms: remove ring artifacts by filtering sinograms
          - if ==False, no filtering
          - if is True, filtering with default parameters
          - if is a dictionary, will be used as kwd arguments for filtering component
            e.g. dict(average_window_size=20, Nsubsets=10, correction_range=(0.9, 1.1))
        * smooth_recon: smooth the reconstruction result
          - if ==False, no smoothing
          - if is True, smoothing with default parameters
          - if is a dictionary, will be used as kwd arguments for smoothing component
            e.g. dict(algorithm='bilateral', sigma_color=0.0005, sigma_spatial=5)
        * smooth_projection: extra smoothing of projection
          - if ==False, no smoothing
          - if is True, smoothing with default parameters
          - if is a dictionary, will be used as kwd arguments for smoothing component
            e.g. dict(algorithm='bilateral', sigma_color=0.02, sigma_spatial=5)

    Default processing chain:
        * preprocess
          - gamma filtering
          - normalization
        * crop
        * median-filter (size 3)
        * (optional) smooth: by default, use bilateral filter
        * intensity fluctuation correction
        * tilt correction
        * create sinograms
        * (optional) apply ring-artifact-removal filter to sinograms
        * find center of rotation
        * reconstruction
        * (optional) smooth reconstruction
        """
        workdir = workdir or self.workdir;  outdir = outdir or self.outdir
        # preprocess
        pre = self.preprocess(workdir=workdir, outdir=outdir)
        # crop
        if crop_window is None:
            # auto-cropping
            cropped = self.autoCrop(pre)
        else:
            xmin, ymin, xmax, ymax = crop_window
            cropped = self.crop(
                pre, 
                left=xmin, right=xmax, top=ymin, bottom=ymax)
        if self.clean_intermediate_files == 'on_the_fly':
            pre.removeAll()
        # median filter
        self.r.median_filtered = median_filtered = self.smooth(
            cropped, outname='median_filtered', algorithm='median', size=3)
        if smooth_projection:
            if smooth_projection is True:
                # default smooth
                smooth_projection = dict(algorithm='bilateral', sigma_color=0.02, sigma_spatial=5)
            pre = smoothed = self.smooth(median_filtered, outname='smoothed', **smooth_projection)
            self.r.smoothed_projection = smoothed
        else:
            pre = median_filtered
        # correct intensity fluctuation
        if_corrected = i3.correct_intensity_fluctuation(
            pre, workdir=os.path.join(workdir, 'intensity-fluctuation-correction'))
        if self.clean_intermediate_files == 'on_the_fly':
            pre.removeAll()
        # correct tilt
        pre = if_corrected
        if tilt is None:
            tilt_corrected, tilt = self.correctTilt_loop(
                pre, workdir=workdir)
        else:
            tilt_corrected, tilt = i3.correct_tilt(
                pre, tilt=tilt, 
                workdir=os.path.join(workdir, 'tilt-correction' ),
                max_npairs=None, parallel=self.parallel_preprocessing)
        if self.clean_intermediate_files == 'on_the_fly':
            pre.removeAll()
        #
        self.r.cropped = cropped
        self.r.if_corrected = if_corrected
        self.r.tilt_corrected = tilt_corrected
        # reconstruct
        outfilename_template = outfilename_template or "recon_%04d.tiff"
        recon = self.reconstruct(
            tilt_corrected,
            workdir=workdir, outdir=outdir,
            remove_rings_at_sinograms=remove_rings_at_sinograms,
            outfilename_template=outfilename_template,
            **kwds)
        if smooth_recon:
            if smooth_recon is True:
                smooth_recon = dict(algorithm='bilateral', sigma_color=0.0005, sigma_spatial=5)
            from . import smooth
            recon = self.r.sm_recon = smooth(
                recon, workdir=os.path.join(self.outdir, 'smoothed'),
                parallel = self.parallel_preprocessing,
                filename_template='sm_' + outfilename_template,
                **smooth_recon)
        if remove_rings:
            self.removeRings(recon)
        # clean up
        import shutil
        if self.clean_intermediate_files == 'archive':
            archive_bg(workdir, outdir)
        elif self.clean_intermediate_files == 'on_the_fly':
            shutil.rmtree(workdir)
        return