def main(): # make a tuple of strings - input file names t_input_files = ("CW2003_H14_M57_S54_msCam1_frame0to99.tiff", ) # notice comma(!) for s_fname in t_input_files: print("Input file: %s" % s_fname) # create a multi-part movie object oc_movie = CMuPaMovieTiff(t_input_files) print("\nMovie information:") print(oc_movie.df_info) print() l_frames = [] i_frame_id = 0 # read first N frames while (oc_movie.read_next_frame()): print("%s" % oc_movie.get_frame_stat()) l_frames.append(oc_movie.na_frame) i_frame_id += 1 # if i_frame_id >= 30: break # show the frames print("\nUsage: (p)revious frame, (n)ext frame, (q)uit.") moshow("frames", l_frames)
def show_diff(s_in_fname, i_frame_1, i_frame_2): oc_movie = CMuPaMovieTiff((s_in_fname,)) print("\nMovie information:") print(oc_movie.df_info) print() i_frame_id = 0 b_frame_1_found = False b_frame_2_found = False while(oc_movie.read_next_frame()): print("%s" % oc_movie.get_frame_stat()) if i_frame_id == i_frame_1: na_frame_1 = oc_movie.na_frame.copy().astype(np.float32) b_frame_1_found = True if i_frame_id == i_frame_2: na_frame_2 = oc_movie.na_frame.copy().astype(np.float32) b_frame_2_found = True if b_frame_1_found and b_frame_2_found: break i_frame_id += 1 # show difference between the requested pair of frames plt.imshow(na_frame_1 - na_frame_2) plt.show()
def pre_filter_all_with_mocorr(s_in_fname, oc_rec_cfg): oc_movie = CMuPaMovieTiff((s_in_fname,)) l_frames = [] i_frame_id = 0 oc_pw_ecc = None oc_motion_field = None i_median_flt_sz = int(oc_rec_cfg["median_blur"]) while(oc_movie.read_next_frame()): # print("Frame number:", i_frame_id) # pre-filter each frame locally (used for debugging) # na_frame = pre_filter_single_frame(oc_movie.na_frame) na_frame = cv.medianBlur(oc_movie.na_frame, i_median_flt_sz) na_frame_to_register = cv.normalize(na_frame, None, alpha=0, beta=1, norm_type=cv.NORM_MINMAX, dtype=cv.CV_32F) if oc_pw_ecc is None: oc_pw_ecc = CPieceWiseECC( na_frame.shape[0], # frame height! na_frame.shape[1], # frame width! na_frame.dtype, oc_rec_cfg ) oc_motion_field = CMotionFieldDrawer( na_frame.shape[0], # frame height! na_frame.shape[1], # frame width! int(oc_rec_cfg["pw_ecc_nrow_tiles"]), int(oc_rec_cfg["pw_ecc_ncol_tiles"]), f_zoom_coef=10.0 ) oc_pw_ecc.process_frame(na_frame, b_verbose=True) # normal way to register the frames oc_pw_ecc.register_frame() # you can register frames with background left untouched, for debugging # oc_pw_ecc.register_frame(na_input=na_frame_to_register) if i_frame_id < 3: plt.subplot(221) plt.imshow(oc_pw_ecc.d_REG['PW_REG_corr_coef'][-1]) plt.subplot(222) plt.imshow(oc_pw_ecc.d_REG['PW_REG_warp_matrix'][-1]) plt.subplot(223) plt.imshow(oc_pw_ecc.d_REG['PW_REG_inter_patch_dist'][-1]) plt.subplot(224) oc_motion_field.process_frame(oc_pw_ecc.oc_twM) plt.imshow(oc_motion_field.na_out) plt.show() # l_frames.append(oc_pw_ecc.na_out.copy()) # l_frames.append(oc_pw_ecc.na_out_reg.copy()) l_frames.append(np.hstack([na_frame_to_register, oc_pw_ecc.na_out_reg])) i_frame_id += 1 # if i_frame_id >= 10: break print(oc_pw_ecc.d_REG['PW_REG_not_converged']) print(oc_pw_ecc.d_REG['PW_REG_high_jumps']) moshow("frames", l_frames)
def test_stibord_real_data(s_in_fname): oc_movie = CMuPaMovieTiff((s_in_fname, )) oc_movie.read_next_frame() na_frame = oc_movie.na_frame.astype(np.float32) DVAR(na_frame) oc_stibord_image = CStiBordFrame(na_frame, 8, 8, 16, cv.BORDER_REFLECT_101) print(oc_stibord_image) for ix, iy in np.ndindex(oc_stibord_image.shape): # oc_stibord_image[ix, iy] = oc_stibord_image[ix, iy] oc_stibord_image[ix, iy] = np.flipud(oc_stibord_image[ix, iy]) plt.figure(figsize=(8, 8)) # figure size (W,H) in inches plt.subplot(221) plt.imshow(na_frame) plt.subplot(222) plt.imshow(oc_stibord_image['inner']) oc_stibord_image.stitch() plt.subplot(223) plt.imshow(oc_stibord_image['inner']) plt.subplot(224) plt.imshow(na_frame - oc_stibord_image['inner']) plt.tight_layout() plt.show() sys.exit()
def plot_result(s_input_dir, s_input_ini_file, s_fname_prefix): # s_roi_fluo_fname = os.path.join(s_input_dir, s_fname_prefix + "roi_fluo.tiff") # oc_fluo_movie = CMuPaMovieTiff((s_roi_fluo_fname,)) # s_roi_mask_fname = os.path.join(s_input_dir, s_fname_prefix + "roi_mask.tiff") # oc_mask_movie = CMuPaMovieTiff((s_roi_mask_fname,)) s_register_fname = os.path.join(s_input_dir, s_fname_prefix + "register.tiff") oc_register_movie = CMuPaMovieTiff((s_register_fname, )) s_fluo_data_fname = os.path.join(s_input_dir, s_fname_prefix + "fluo.npy") d_fluo_data = np.load(s_fluo_data_fname, allow_pickle=True).item() for s_key in d_fluo_data.keys(): DVAR(d_fluo_data[s_key], s_var_name=s_key) oc_iproj = None i_frame_id = 0 while (oc_register_movie.read_next_frame()): if i_frame_id % 100 == 0: print("%s" % oc_register_movie.get_frame_stat()) if i_frame_id == 0: oc_iproj = CROISpecificIntensityProjector( oc_register_movie.na_frame.shape[0], # frame height oc_register_movie.na_frame.shape[1], # frame width d_fluo_data) oc_iproj.process_frame(oc_register_movie.na_frame) i_frame_id += 1 # if i_frame_id >= 30: break oc_iproj.finalize_projection() oc_cmap = plt.cm.jet.copy() oc_cmap.set_bad(color='black') _, ax = plt.subplots(nrows=1, ncols=3, figsize=(14, 5)) ax[0].imshow(oc_iproj.d_IPROJ['IPROJ_ROI_fluo_raw'], cmap=oc_cmap) ax[1].imshow(oc_iproj.d_IPROJ['IPROJ_ROI_fluo_max'], cmap=oc_cmap) ax[2].imshow(oc_iproj.d_IPROJ['IPROJ_ROI_fluo_norm'], cmap=oc_cmap) ax[0].set_title("ROI fluo (raw)") ax[1].set_title("ROI fluo (at dF/F peak)") ax[2].set_title("ROI fluo (norm by peak)") s_out_fname = os.path.join(s_input_dir, s_fname_prefix + "IPROJ_ROI_fluo.png") plt.savefig(s_out_fname, dpi=300, bbox_inches='tight') plt.show()
def show_whole_input(s_in_fname): # create a multi-part movie object oc_movie = CMuPaMovieTiff((s_in_fname,)) print("\nMovie information:") print(oc_movie.df_info) print() l_frames = [] # read first N frames while(oc_movie.read_next_frame()): print("%s" % oc_movie.get_frame_stat()) l_frames.append(oc_movie.na_frame) # show the frames print("\nUsage: (p)revious frame, (n)ext frame, (q)uit.") moshow("frames", l_frames)
def pre_filter_all_frames(s_in_fname): l_frames = [] i_frame_id = 0 i_niter = 3 i_median_blur_sz = 5 oc_filter = CFastGuidedFilter(3) oc_strel_kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (7,7)) oc_movie = CMuPaMovieTiff((s_in_fname,)) while(oc_movie.read_next_frame()): na_frame = cv.medianBlur(oc_movie.na_frame, i_median_blur_sz) oc_filter.process_frame(cv.normalize(na_frame, None, alpha=0, beta=1, norm_type=cv.NORM_MINMAX, dtype=cv.CV_32F)) na_background = cv.morphologyEx(oc_filter.na_out, cv.MORPH_OPEN, oc_strel_kernel, iterations=i_niter) na_signal = cv.normalize(oc_filter.na_out - na_background, None, alpha=0, beta=255, norm_type=cv.NORM_MINMAX, dtype=cv.CV_8U) # l_frames.append(na_signal) l_frames.append(np.hstack([ cv.normalize(na_frame, None, alpha=0, beta=1, norm_type=cv.NORM_MINMAX, dtype=cv.CV_32F), cv.normalize(na_signal, None, alpha=0, beta=1, norm_type=cv.NORM_MINMAX, dtype=cv.CV_32F), ])) if i_frame_id % 10 == 0: print(i_frame_id) i_frame_id += 1 moshow("frames", l_frames)
def main(): """ The input directory contain files named similar to following pattern: downsampled-McHugh_Lab_181_614_S000001_L01_ch01.tif downsampled-McHugh_Lab_181_614_S000001_L01_ch02.tif downsampled-McHugh_Lab_181_614_S000001_L01_ch03.tif downsampled-McHugh_Lab_181_614_S000001_L01_ch04.tif downsampled-McHugh_Lab_181_614_S000002_L01_ch01.tif downsampled-McHugh_Lab_181_614_S000002_L01_ch02.tif downsampled-McHugh_Lab_181_614_S000002_L01_ch03.tif downsampled-McHugh_Lab_181_614_S000002_L01_ch04.tif downsampled-McHugh_Lab_181_614_S000003_L01_ch01.tif downsampled-McHugh_Lab_181_614_S000003_L01_ch02.tif downsampled-McHugh_Lab_181_614_S000003_L01_ch03.tif downsampled-McHugh_Lab_181_614_S000003_L01_ch04.tif and so on. """ s_input_dir = "D:\\data\\hongshen\\McHugh_lab_181_614_stitching\\downsampled" s_wcard_in = "downsampled-McHugh_Lab_181_614_S000*_L01_ch03.tif" s_fname_out = "downsampled-McHugh_Lab_181_614_L01_ch03.tif" if not os.path.isdir(s_input_dir): print("This script require presence of big size input files.") print( "Seems like you don't have these files stored at expected location." ) print("If you REALLY need these files please contact me. Exiting...") sys.exit(0) print("Input directory:\t%s" % s_input_dir) t_input_files = enum_video_files(s_input_dir, s_wcard_in, i_num_pos=2) print("Found %d input file(s)." % len(t_input_files)) # create a multi-part movie object oc_movie = CMuPaMovieTiff(t_input_files) print("\nMovie information:") print(oc_movie.df_info) # create tiff writer object for output data storage oc_tiff_writer = CSingleTiffWriter(s_fname_out, b_delete_existing=True) # initial values of top, bottom, left and right borders for frame cropping i_min_top = 0 i_max_bot = 0 i_min_lef = 0 i_max_rig = 0 # the frame counter i_frame_id = 0 # Stage 1: # read and process frames one by one until the end # calculate global values of the i_min_top, i_max_bot, i_min_lef, i_max_rig while (oc_movie.read_next_frame()): print("%s" % oc_movie.get_frame_stat()) _, na_borders = get_contour_mask(oc_movie.na_frame) i_top, i_bot, i_lef, i_rig = na_borders if i_frame_id == 0: i_min_top = oc_movie.na_frame.shape[0] i_min_lef = oc_movie.na_frame.shape[1] else: i_min_top = min(i_min_top, i_top) i_max_bot = max(i_max_bot, i_bot) i_min_lef = min(i_min_lef, i_lef) i_max_rig = max(i_max_rig, i_rig) i_frame_id += 1 if i_frame_id >= len(t_input_files): break # reset the oc_movie object's state and the frame counter oc_movie = CMuPaMovieTiff(t_input_files) i_frame_id = 0 # Stage 2: # read and process frames one by one until the end # crop each frame according to the i_min_top, i_max_bot, i_min_lef, i_max_rig # calculated above and erase pixels that does not belong to the contour mask while (oc_movie.read_next_frame()): print("%s" % oc_movie.get_frame_stat()) na_mask_image, _ = get_contour_mask(oc_movie.na_frame) oc_movie.na_frame[np.where(na_mask_image == 0)] = 0 # write new contour frame into tiff file oc_tiff_writer.write_next_frame(oc_movie.na_frame[i_min_lef:i_max_rig, i_min_top:i_max_bot]) i_frame_id += 1 if i_frame_id >= len(t_input_files): break # always close all tiff writers after usage! oc_tiff_writer.close()
def main(): # load local configuration file oc_rec_cfg = configparser.ConfigParser() oc_rec_cfg.read("CW2003_H14_M57_S54_msCam1_frame0to99.ini") # make a tuple of strings - input file names t_input_files = ("CW2003_H14_M57_S54_msCam1_frame0to99.tiff", ) # notice comma(!) for s_fname in t_input_files: print("Input file: %s" % s_fname) # create a multi-part movie object oc_movie = CMuPaMovieTiff(t_input_files) print("\nMovie information:") print(oc_movie.df_info) print() oc_pc1_wiper = None oc_register = None l_frames = [] i_frame_id = 0 # read the movie frame by frame, register frames, stack with raw frame and add into the l_frames while (oc_movie.read_next_frame()): print("%s" % oc_movie.get_frame_stat()) if i_frame_id == 0: oc_pc1_wiper = CPrinCompWiper( oc_movie.na_frame.shape[0], # frame height! oc_movie.na_frame.shape[1] # frame width! ) oc_register = CFrameRegECC( oc_movie.na_frame.shape[0], # frame height! oc_movie.na_frame.shape[1], # frame width! oc_movie.na_frame.dtype, oc_rec_cfg["frame_registration"]) oc_pc1_wiper.process_frame(oc_movie.na_frame) oc_register.process_frame(oc_pc1_wiper.na_out) oc_register.register_frame() na_frame_before = cv2.normalize(oc_movie.na_frame, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) na_frame_after = cv2.normalize(oc_register.na_out_reg, None, alpha=0, beta=1, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_32F) l_frames.append(np.vstack([na_frame_before, na_frame_after])) i_frame_id += 1 # if i_frame_id >= 30: break # show the frames print("\nUsage: (p)revious frame, (n)ext frame, (q)uit.") moshow("frames", l_frames)