Exemplo n.º 1
0
def reg(stack):

    sr = StackReg(StackReg.TRANSLATION)
    t_mats = sr.register_stack(gauss(stack), reference='first', n_frames=1)
    t_mats[:, 0, -1] = 0
    reg_stack = sr.transform_stack(stack, tmats=t_mats)

    return reg_stack
Exemplo n.º 2
0
def process(pth):

    print(f'\nProcessing {pth}')

    save_pth = pth / 'reg_stacks'
    #tmat_pth = pth / 'transformation_matrices'
    try:
        save_pth.mkdir()
        #tmat_pth.mkdir()
    except FileExistsError:
        ex('Save File for reg stacks or tmats already exists. Delete and re-run.'
           )

    #tell pystack reg that we will use a translational transformation
    #there shouldn't be intra-volume rotation or shear (there might be for rod blink)
    sr = StackReg(StackReg.TRANSLATION)
    #register to the first slice and output the transfomation matrix without transforming
    #iterate through the files in the stacks folder
    files = pth.glob('*.tif')
    loop_starts = time.time()

    for i, file in enumerate(files):
        #start_time = time.time()
        print(f'Processing: {file}')
        #Intravolume registration of the fixed volume

        #load first stack and do a 3d gaussian blur with a sigma=1
        #str needed for imread, otherwise only one frame is loaded
        fixed = io.imread(str(file))  #had gauss(), testing

        t_mats = sr.register_stack(gauss(fixed), reference='first', n_frames=1)
        #remove the x shift from all the matrices - horizontal movement isn't relevant here,
        #the volume should be acquired quickly enough that this isn't a problem.
        t_mats[:, 0, -1] = 0
        fixed = sr.transform_stack(fixed, tmats=t_mats)

        #Using previous to see if I could get rid of wigge. Didn't seem to work.
        #t_mats = sr.register_stack(gauss(fixed), reference='previous')
        #t_mats[:,1,-1] = 0
        #fixed = sr.transform_stack(fixed, tmats=t_mats)
        #save the register fixed volume in the parent directory for reference
        save_name = save_pth / f'reg_{file.name}'
        #io.imsave(arr=img_as_uint(fixed), fname=str(save_name))
        io.imsave(arr=img_as_float32(fixed), fname=str(save_name))
        #get fixed out of memory - may not be worth the time?
        print(f'Intravolume registration complete. File saved at {save_name}')

        #end_time = time.time()
        #print(f'{file} was processed in {(end_time - start_time):.2f}s. \
        #\n{((end_time - loop_starts)/60):.2f} minutes have elapsed.')

        #de;ete emumerate
        #if i==4:
        #ex('auto break')
    end_time = time.time()
    print(f'Run took {(end_time-loop_starts)/60:.2f} minutes')
Exemplo n.º 3
0
def process(pth):
    
    print(f'\nProcessing {pth}')
    
    save_pth = pth / 'vol_reg_stacks'
    tmat_pth = pth / 'vol_transformation_matrices'
    try:
        save_pth.mkdir()
        tmat_pth.mkdir()
    except FileExistsError:
        ex('Save File for reg stacks or tmats already exists. Delete and re-run.')
        
    #Intravolume registration of the fixed volume
    
    print('Doing intravolume registration.')
    #load first stack and do a 3d gaussian blur with a sigma=1
    #str needed for imread, otherwise only one frame is loaded
    fixed = io.imread(str(pth/'stack0.tif')) #had gauss(), testing
    #tell pystack reg that we will use a translational transformation
    #there shouldn't be intra-volume rotation or shear (there might be for rod blink)
    sr = StackReg(StackReg.TRANSLATION)
    #register to the first slice and output the transfomation matrix without transforming
    t_mats = sr.register_stack(gauss(fixed), reference='first', n_frames=1)
    #remove the x shift from all the matrices - horizontal movement isn't relevant here, 
    #the volume should be acquired quickly enough that this isn't a problem.
    t_mats[:,0,-1] = 0
    fixed = sr.transform_stack(fixed, tmats=t_mats)
    #save the register fixed volume in the parent directory for reference
    save_name = pth.parent / 'fixed.tif'
    #io.imsave(arr=img_as_uint(fixed), fname=str(save_name))
    io.imsave(arr=fixed.astype('float32'), fname=str(save_name))
    #get fixed out of memory - may not be worth the time?
    del fixed
    print(f'Intravolume registration complete. File saved at {save_name}')
    
    #Volume registration
    
    #switch to tmat folder because i dont know how to set the output folder to save 
    #the transformation mats. elastix.Execute() automatically saves
    #TransformParameters.0.txt in the current directory
    os.chdir(tmat_pth)
    
    #reload fixed image with sitk so it is in the right format
    fixed = sitk.ReadImage(str(save_name))
    #set up for multivolume registration
    parameterMap = sitk.GetDefaultParameterMap('affine')
    elastixImageFilter = sitk.ElastixImageFilter()
    elastixImageFilter.SetFixedImage(fixed)
    elastixImageFilter.SetParameterMap(parameterMap)
    #test 11-8-2019
    parameterMap['Image Sampler'] = ['Full']
    #parameterMap['Metric'] = ['AdvancedMeanSquares']
    parameterMap['Optimiser'] = ['FullSearch']
    
    #iterate through the files in the stacks folder
    files = sorted(list(pth.glob('*.tif')), key=sort_key)
    loop_starts  = time.time()
    for i,file in enumerate(files):
        #i=0 #use first stack
        #register to previous stack
        if i > 1:
            fixed = sitk.ReadImage(str(save_name))
            elastixImageFilter.SetFixedImage(fixed)
        start_time = time.time()
        print(f'Processing: {file}')
        #load the volume
        #Do affine registration so that volume can shear to do the intravolume registration
        #this code is equivalent to the lines below, but allows the export of the
        #transformation matrices
        #moving = sitk.ReadImage(file)
        #reg_volume = sitk.Elastix(fixed, moving, "affine")
        elastixImageFilter.SetMovingImage(sitk.ReadImage(str(file)))
        elastixImageFilter.Execute()

        reg_volume = elastixImageFilter.GetResultImage()
        #can get the transform parameters, but have no idea how to save them
        #maybe just through normal python file manipulation?
        #transformParameterMap = elastixImageFilter.GetTransformParameterMap()
        
        #save volume
        save_name = save_pth / f'reg_{file.name}'
        sitk.WriteImage(reg_volume, str(save_name))
        
        #transformation was saved on execute, now rename.
        #tmat_name = tmat_pth / f'tmat_{file.name}'
        os.rename('TransformParameters.0.txt', f'tmat_{file.stem}.txt')
        
        end_time = time.time()
        print(f'{file} was processed in {(end_time - start_time):.2f}s. \
              \n{((end_time - loop_starts)/60):.2f} minutes have elapsed.')
Exemplo n.º 4
0
    print('Unzipping the database...')
    zip_Archivo = zipfile.ZipFile(cwd + '\Hybrid_Lab4.zip', 'r')
    zip_Archivo.extractall(cwd)
    zip_Archivo.close()
    print('Unzipping done.')

A = ndimage.imread("parte6.jpg", flatten=False)
B = ndimage.imread("parte8.jpg", flatten=False)

test = numpy.concatenate((A[:, 0:512, :], B[:, 512:, :]), axis=1)
misc.imsave("Blended_NotPyr.jpg", numpy.real(test))
plt.imshow(test)

#downsampling:
x1_A = misc.imresize(gauss(A,
                           sigma=0.1,
                           multichannel=True,
                           preserve_range=True), (512, 512),
                     interp='bicubic',
                     mode=None)
s1_A = cv2.pyrUp(x1_A)
A_fl = A.astype(float)
s1_A_fl = s1_A.astype(float)
l1_A_fl = numpy.absolute(A_fl - s1_A_fl)
l1_A = l1_A_fl.astype(numpy.uint8)
plt.imshow(x1_A)

x2_A = misc.imresize(gauss(x1_A,
                           sigma=0.1,
                           multichannel=True,
                           preserve_range=True), (256, 256),
                     interp='bicubic',