def main(): secNum = sys.argv[1] mkyNum = sys.argv[2] channel = sys.argv[3] region = str(sys.argv[4]) conf_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/confocal/src_registration/' side_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/side_light_microscope/src_registration/' save_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/confocal/sidelight_registered/' # DIC = '/home/sci/blakez/Reflect Affine/DIC_to_Reflect.txt' src_pt = conf_dir + 'M{0}/section_{1}/{2}/section_{1}_confocal_relation_with_sidelight.txt'.format(mkyNum, secNum, region) tar_pt = side_dir + 'M{0}/section_{1}/section_{1}_sidelight_relation_with_confocal.txt'.format(mkyNum, secNum) # SID = '/home/sci/blakez/Reflect Affine/sidelight_to_DIC.txt' src_im = common.LoadITKImage(conf_dir + 'M{0}/section_{1}/{3}/Ch{2}/M{0}_{1}_LGN_RHS_Ch{2}_z00.tif'.format(mkyNum, secNum, channel, region)) # tar_im = common.LoadITKImage('M{0}/{1}/Crop_ThirdNerve_EGFP_z16.tiff'.format(mkyNum, secNum)) # The points need to be chosen in the origin corrected sidescape for downstream purposes affine = load_and_solve(tar_pt, src_pt) out_grid = bb_grid_solver(src_im, affine) z_stack = [] num_slices = len(glob.glob(conf_dir + 'M{0}/section_{1}/{3}/Ch{2}/*'.format(mkyNum, secNum, channel, region))) for z in range(0, num_slices): src_im = common.LoadITKImage(conf_dir + 'M{0}/section_{1}/{4}/Ch{2}/M{0}_{1}_LGN_RHS_Ch{2}_z{3}.tif'.format(mkyNum, secNum, channel, str(z).zfill(2), region)) aff_im = ca.Image3D(out_grid, ca.MEM_HOST) cc.ApplyAffineReal(aff_im, src_im, affine) common.SaveITKImage(aff_im, save_dir + 'M{0}/section_{1}/{4}/Ch{2}/M{0}_01_section_{1}_LGN_RHS_Ch{2}_conf_aff_sidelight_z{3}.tiff'.format(mkyNum, secNum, channel, str(z).zfill(2), region)) z_stack.append(aff_im) print('==> Done with {0}/{1}'.format(z, num_slices - 1)) stacked = cc.Imlist_to_Im(z_stack) stacked.setSpacing(ca.Vec3Df(out_grid.spacing()[0], out_grid.spacing()[1], 0.03/num_slices)) common.SaveITKImage(stacked, save_dir + 'M{0}/section_{1}/{3}/Ch{2}/M{0}_01_section_{1}_Ch{2}_conf_aff_sidelight_stack.nrrd'.format(mkyNum, secNum, channel, region)) common.DebugHere() if channel==0: cc.WriteGrid(stacked.grid(), save_dir + 'M{0}/section_{1}/{2}/affine_registration_grid.txt'.format(mkyNum, secNum, region))
def main(): secNum = sys.argv[1] mkyNum = sys.argv[2] region = str(sys.argv[3]) # channel = sys.argv[3] ext = 'M{0}/section_{1}/{2}/'.format(mkyNum, secNum, region) ss_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/side_light_microscope/' conf_dir = '/home/sci/blakez/korenbergNAS/3D_database/Working/Microscopic/confocal/' memT = ca.MEM_DEVICE try: with open( ss_dir + 'src_registration/M{0}/section_{1}/M{0}_01_section_{1}_regions.txt' .format(mkyNum, secNum), 'r') as f: region_dict = json.load(f) f.close() except IOError: region_dict = {} region_dict[region] = {} region_dict['size'] = map( int, raw_input("What is the size of the full resolution image x,y? "). split(',')) region_dict[region]['bbx'] = map( int, raw_input( "What are the x indicies of the bounding box (Matlab Format x_start,x_stop? " ).split(',')) region_dict[region]['bby'] = map( int, raw_input( "What are the y indicies of the bounding box (Matlab Format y_start,y_stop? " ).split(',')) if region not in region_dict: region_dict[region] = {} region_dict[region]['bbx'] = map( int, raw_input( "What are the x indicies of the bounding box (Matlab Format x_start,x_stop? " ).split(',')) region_dict[region]['bby'] = map( int, raw_input( "What are the y indicies of the bounding box (Matlab Format y_start,y_stop? " ).split(',')) img_region = common.LoadITKImage( ss_dir + 'src_registration/M{0}/section_{1}/M{0}_01_section_{1}_{2}.tiff'. format(mkyNum, secNum, region), ca.MEM_HOST) ssiSrc = common.LoadITKImage( ss_dir + 'src_registration/M{0}/section_{1}/frag0/M{0}_01_ssi_section_{1}_frag0.nrrd' .format(mkyNum, secNum), ca.MEM_HOST) bfi_df = common.LoadITKField( ss_dir + 'Blockface_registered/M{0}/section_{1}/frag0/M{0}_01_ssi_section_{1}_frag0_to_bfi_real.mha' .format(mkyNum, secNum), ca.MEM_DEVICE) # Figure out the same region in the low resolution image: There is a transpose from here to matlab so dimensions are flipped low_sz = ssiSrc.size().tolist() yrng_raw = [(low_sz[1] * region_dict[region]['bbx'][0]) / np.float(region_dict['size'][0]), (low_sz[1] * region_dict[region]['bbx'][1]) / np.float(region_dict['size'][0])] xrng_raw = [(low_sz[0] * region_dict[region]['bby'][0]) / np.float(region_dict['size'][1]), (low_sz[0] * region_dict[region]['bby'][1]) / np.float(region_dict['size'][1])] yrng = [np.int(np.floor(yrng_raw[0])), np.int(np.ceil(yrng_raw[1]))] xrng = [np.int(np.floor(xrng_raw[0])), np.int(np.ceil(xrng_raw[1]))] low_sub = cc.SubVol(ssiSrc, xrng, yrng) # Figure out the grid for the sub region in relation to the sidescape originout = [ ssiSrc.origin().x + ssiSrc.spacing().x * xrng[0], ssiSrc.origin().y + ssiSrc.spacing().y * yrng[0], 0 ] spacingout = [ (low_sub.size().x * ssiSrc.spacing().x) / (img_region.size().x), (low_sub.size().y * ssiSrc.spacing().y) / (img_region.size().y), 1 ] gridout = cc.MakeGrid(img_region.size().tolist(), spacingout, originout) img_region.setGrid(gridout) only_sub = np.zeros(ssiSrc.size().tolist()[0:2]) only_sub[xrng[0]:xrng[1], yrng[0]:yrng[1]] = np.squeeze(low_sub.asnp()) only_sub = common.ImFromNPArr(only_sub) only_sub.setGrid(ssiSrc.grid()) # Deform the only sub region to only_sub.toType(ca.MEM_DEVICE) def_sub = ca.Image3D(bfi_df.grid(), bfi_df.memType()) cc.ApplyHReal(def_sub, only_sub, bfi_df) def_sub.toType(ca.MEM_HOST) # Now have to find the bounding box in the deformation space (bfi space) if 'deformation_bbx' not in region_dict[region]: bb_def = np.squeeze(pp.LandmarkPicker([np.squeeze(def_sub.asnp())])) bb_def_y = [bb_def[0][0], bb_def[1][0]] bb_def_x = [bb_def[0][1], bb_def[1][1]] region_dict[region]['deformation_bbx'] = bb_def_x region_dict[region]['deformation_bby'] = bb_def_y with open( ss_dir + 'src_registration/M{0}/section_{1}/M{0}_01_section_{1}_regions.txt' .format(mkyNum, secNum), 'w') as f: json.dump(region_dict, f) f.close() # Now need to extract the region and create a deformation and image that have the same resolution as the img_region deform_sub = cc.SubVol(bfi_df, region_dict[region]['deformation_bbx'], region_dict[region]['deformation_bby']) common.DebugHere() sizeout = [ int( np.ceil((deform_sub.size().x * deform_sub.spacing().x) / img_region.spacing().x)), int( np.ceil((deform_sub.size().y * deform_sub.spacing().y) / img_region.spacing().y)), 1 ] region_grid = cc.MakeGrid(sizeout, img_region.spacing().tolist(), deform_sub.origin().tolist()) def_im_region = ca.Image3D(region_grid, deform_sub.memType()) up_deformation = ca.Field3D(region_grid, deform_sub.memType()) img_region.toType(ca.MEM_DEVICE) cc.ResampleWorld(up_deformation, deform_sub, ca.BACKGROUND_STRATEGY_PARTIAL_ZERO) cc.ApplyHReal(def_im_region, img_region, up_deformation) ss_out = ss_dir + 'Blockface_registered/M{0}/section_{1}/{2}/'.format( mkyNum, secNum, region) if not pth.exists(pth.expanduser(ss_out)): os.mkdir(pth.expanduser(ss_out)) common.SaveITKImage( def_im_region, pth.expanduser(ss_out) + 'M{0}_01_section_{1}_{2}_def_to_bfi.nrrd'.format( mkyNum, secNum, region)) common.SaveITKImage( def_im_region, pth.expanduser(ss_out) + 'M{0}_01_section_{1}_{2}_def_to_bfi.tiff'.format( mkyNum, secNum, region)) del img_region, def_im_region, ssiSrc, deform_sub # Now apply the same deformation to the confocal images conf_grid = cc.LoadGrid( conf_dir + 'sidelight_registered/M{0}/section_{1}/{2}/affine_registration_grid.txt' .format(mkyNum, secNum, region)) cf_out = conf_dir + 'blockface_registered/M{0}/section_{1}/{2}/'.format( mkyNum, secNum, region) # confocal.toType(ca.MEM_DEVICE) # def_conf = ca.Image3D(region_grid, deform_sub.memType()) # cc.ApplyHReal(def_conf, confocal, up_deformation) for channel in range(0, 4): z_stack = [] num_slices = len( glob.glob(conf_dir + 'sidelight_registered/M{0}/section_{1}/{3}/Ch{2}/*.tiff'. format(mkyNum, secNum, channel, region))) for z in range(0, num_slices): src_im = common.LoadITKImage( conf_dir + 'sidelight_registered/M{0}/section_{1}/{3}/Ch{2}/M{0}_01_section_{1}_LGN_RHS_Ch{2}_conf_aff_sidelight_z{4}.tiff' .format(mkyNum, secNum, channel, region, str(z).zfill(2))) src_im.setGrid( cc.MakeGrid( ca.Vec3Di(conf_grid.size().x, conf_grid.size().y, 1), conf_grid.spacing(), conf_grid.origin())) src_im.toType(ca.MEM_DEVICE) def_im = ca.Image3D(region_grid, ca.MEM_DEVICE) cc.ApplyHReal(def_im, src_im, up_deformation) def_im.toType(ca.MEM_HOST) common.SaveITKImage( def_im, cf_out + 'Ch{2}/M{0}_01_section_{1}_{3}_Ch{2}_conf_def_blockface_z{4}.tiff' .format(mkyNum, secNum, channel, region, str(z).zfill(2))) if z == 0: common.SaveITKImage( def_im, cf_out + 'Ch{2}/M{0}_01_section_{1}_{3}_Ch{2}_conf_def_blockface_z{4}.nrrd' .format(mkyNum, secNum, channel, region, str(z).zfill(2))) z_stack.append(def_im) print('==> Done with Ch {0}: {1}/{2}'.format( channel, z, num_slices - 1)) stacked = cc.Imlist_to_Im(z_stack) stacked.setSpacing( ca.Vec3Df(region_grid.spacing().x, region_grid.spacing().y, conf_grid.spacing().z)) common.SaveITKImage( stacked, cf_out + 'Ch{2}/M{0}_01_section_{1}_{3}_Ch{2}_conf_def_blockface_stack.nrrd' .format(mkyNum, secNum, channel, region)) if channel == 0: cc.WriteGrid( stacked.grid(), cf_out + 'deformed_registration_grid.txt'.format( mkyNum, secNum, region))
aff_list.append(ssi_aff) print "Registered Grid: " print ssi_reg.grid() print "Affine Grid: " print ssi_aff.grid() print "Added Section {0}".format(sec) else: stack_temp = ca.Image3D(inplane_grid, ca.MEM_HOST) cc.ResampleWorld(stack_temp, ssi_reg) reg_list.append(stack_temp) aff_list.append(ssi_aff) print "Added Section {0}".format(sec) print 'Attempting to make volumes' ssi_reg_vol = cc.Imlist_to_Im(reg_list) ssi_aff_vol = cc.Imlist_to_Im(aff_list) bfi_org_vol = cc.Imlist_to_Im(bfi_list) ssi_reg_vol.setSpacing( ca.Vec3Df(ssi_reg_vol.spacing()[0], ssi_reg_vol.spacing()[1], 0.030)) ssi_aff_vol.setSpacing( ca.Vec3Df(ssi_aff_vol.spacing()[0], ssi_aff_vol.spacing()[1], 0.030)) bfi_org_vol.setSpacing( ca.Vec3Df(bfi_org_vol.spacing()[0], bfi_org_vol.spacing()[1], 0.030)) print 'Volumes Created and Attempting to Save'