def single_run(imgs_p, flag_junc=False, flag_seg=False, flag_ins=True): #def single_run(imgs_p,flag_junc = True,flag_seg = True,flag_ins = True): colors = np.array([0, 500], np.uint16) tiff_p, ins_p, seg_p = imgs_p fname = osp.basename(ins_p) ins = tifffile.imread(ins_p) print(fname, len(np.unique(ins))) seg = tifffile.imread(ins_p) seg = (ins > 0).astype(int) junc_generate, ins_filter = juncs_label(ins, seg) ins_generate_p = osp.join(target_dir, "ins_filter/{}".format(fname)) seg_generate_p = osp.join(target_dir, "segs/{}".format(fname)) seg_generate_visual_p = osp.join(target_dir, "segs_visual/{}".format(fname)) junc_generate_p = osp.join(target_dir, "juncs/{}".format(fname)) junc_generate_visual_p = osp.join(target_dir, "juncs_visual/{}".format(fname)) if flag_ins: mkdir_if_missing(osp.join(target_dir, "ins_filter")) tifffile.imsave(ins_generate_p, ins_filter) mkdir_if_missing(osp.join(target_dir, "juncs")) tifffile.imsave(junc_generate_p, junc_generate.astype(np.uint8)) if flag_junc: mkdir_if_missing(osp.join(target_dir, "juncs_visual")) tifffile.imsave(junc_generate_visual_p, colors[junc_generate]) mkdir_if_missing(osp.join(target_dir, "segs")) tifffile.imsave(seg_generate_p, (ins_filter > 0).astype(np.uint8)) if flag_seg: mkdir_if_missing(osp.join(target_dir, "segs_visual")) tifffile.imsave(seg_generate_visual_p, colors[seg]) return [seg_generate_p, junc_generate_p]
from torch.utils.data import DataLoader from neuralTrack.utils.osutils import mkdir_if_missing from neuralTrack.utils.patches_utils import patches_split, patches_merge parser = argparse.ArgumentParser(description="Split tiffs into patches") working_dir = osp.dirname(osp.abspath(__file__)) parser.add_argument('--logs-dir', type=str, metavar='PATH', default=osp.join(working_dir, 'logs')) args = parser.parse_args() mkdir_if_missing(args.logs_dir) def single_run(i, img, seg, end, ins, junc, centerline, thres=2): img = img.numpy() seg = seg.numpy() end = end.numpy() ins = ins.numpy() junc = junc.numpy() centerline = centerline.numpy() edge_mask = np.zeros_like(end, dtype=np.bool) edge_mask[:thres] = 1
mkdir_if_missing(osp.join(target_dir, "juncs_visual")) tifffile.imsave(junc_generate_visual_p, colors[junc_generate]) mkdir_if_missing(osp.join(target_dir, "segs")) tifffile.imsave(seg_generate_p, (ins_filter > 0).astype(np.uint8)) if flag_seg: mkdir_if_missing(osp.join(target_dir, "segs_visual")) tifffile.imsave(seg_generate_visual_p, colors[seg]) return [seg_generate_p, junc_generate_p] if __name__ == "__main__": target_dir = "/home/jjx/Biology/data_synthesize_junc_100" mkdir_if_missing(target_dir) sys.stdout = Logger(osp.join(target_dir, "log.txt")) infos_tiffs = glob("/home/jjx/Biology/data_synthesize_100/tiffs/*") infos_ins = glob("/home/jjx/Biology/data_synthesize_100/ins/*") infos_segs = glob("/home/jjx/Biology/data_synthesize_100/gts/*") infos_tiffs.sort() infos_ins.sort() infos_segs.sort() imgs_p = list(zip(infos_tiffs, infos_ins, infos_segs)) imgs_info = [] with Pool(8) as pool: imgs_info = pool.starmap(single_run, zip(imgs_p))
def ins_seg(img_path, junc_path, target_dir, flag_visual=False): fname = osp.splitext(osp.basename(img_path))[0] fname_dir = osp.join(target_dir, fname) mkdir_if_missing(fname_dir) fname_tiff_dir = osp.join(fname_dir, "tiffs") fname_gt_dir = osp.join(fname_dir, "gts") if flag_visual: fname_tiff_visual_dir = osp.join(fname_dir, "tiffs_visual") fname_gt_visual_dir = osp.join(fname_dir, "gts_visual") mkdir_if_missing(fname_tiff_visual_dir) mkdir_if_missing(fname_gt_visual_dir) mkdir_if_missing(fname_tiff_dir) mkdir_if_missing(fname_gt_dir) img = tifffile.imread(img_path) img = (img > 1) junc = tifffile.imread(junc_path) ins = label(img, connectivity=1) ins_inds = np.unique(ins)[1:] ins_visual = [] inds = np.arange(img.size) for i in ins_inds: ins_i = ins == i if np.sum(ins_i) < 8000: continue print(i, np.sum(ins_i)) ins_i = ins_i.astype(np.uint8) gt_i = np.zeros_like(ins_i) junc_mask = np.logical_and(ins_i, junc) if np.sum(junc_mask) == 0: continue inds_sel = inds[junc_mask.flatten()] mask = np.zeros_like(junc_mask) for ind in inds_sel: coord = np.unravel_index(ind, img.shape) mask += range_mask_generate(coord, img.shape) mask = np.logical_and(mask, ins_i) fname_i = "{}_{}.tif".format(fname, i) gt_i[mask] = 1 if flag_visual: tifffile.imsave(osp.join(fname_tiff_visual_dir, fname_i), colors[ins_i]) tifffile.imsave(osp.join(fname_gt_visual_dir, fname_i), colors[gt_i]) tifffile.imsave(osp.join(fname_tiff_dir, fname_i), ins_i) tifffile.imsave(osp.join(fname_gt_dir, fname_i), gt_i) ins_i = np.sum(ins_i, axis=0) > 0 ins_i = ins_i.astype(np.int) ins_visual.append(colors[ins_i]) if len(ins_visual) == 0: return fname_x = "{}_x.jpg".format(fname) ins_visual = np.hstack(ins_visual) scipy.misc.imsave(osp.join(fname_dir, fname_x), ins_visual)
parser = argparse.ArgumentParser(description="Split tiffs into patches") parser.add_argument('--source-dir', type=str, metavar='PATH') shift = [1, 1, 1] args = parser.parse_args() fdir = args.source_dir fpath_tiffs = glob(osp.join(fdir, "*/*.tif")) for fpath_tiff in fpath_tiffs: print(fpath_tiff) tiff = tifffile.imread(fpath_tiff) tiff_shift_dir = "{}_shift".format(osp.dirname(fpath_tiff)) mkdir_if_missing(tiff_shift_dir) fpath_tiff_shift = osp.join(tiff_shift_dir, osp.basename(fpath_tiff)) inds_sel = np.arange(tiff.size)[(tiff > 0).flatten()] tiff_shift = np.zeros_like(tiff) coords_sel = np.array(np.unravel_index(inds_sel, tiff.shape)) coords_sel_shift = coords_sel + np.array(shift)[:, None] xs_, ys_, zs_ = coords_sel_shift xs, ys, zs = coords_sel xs_mask = np.logical_and(xs_ > 0, xs_ < tiff.shape[0]) ys_mask = np.logical_and(ys_ > 0, ys_ < tiff.shape[0]) zs_mask = np.logical_and(zs_ > 0, zs_ < tiff.shape[0])
imgs_info_ins.sort() #imgs_info_tiffs = glob("/home/jjx/Biology/DirectField/data_300_modified_VOX/visual_results/*/*_pred.tif") imgs_info_gts = glob( "/home/jjx/Biology/DirectField/data_300_modified_junc2/gts/*") imgs_info_gts.sort() imgs_info_tiffs = glob( "/home/jjx/Biology/DirectField/data_300_modified/tiffs/*") imgs_info_tiffs.sort() imgs_info = list( zip(imgs_info_gts, imgs_info_ins, imgs_info_juncs, imgs_info_centers, imgs_info_tiffs))[:2] mkdir_if_missing(args.logs_dir) def range_mask_generate(coord, shape, thres=8): x, y, z = coord x_r = x + thres if x + thres <= shape[0] else shape[0] x_l = x - thres if x - thres >= 0 else 0 y_r = y + thres if y + thres <= shape[1] else shape[1] y_l = y - thres if y - thres >= 0 else 0 z_r = z + thres if z + thres <= shape[2] else shape[2] z_l = z - thres if z - thres >= 0 else 0 mask = np.zeros(shape, dtype=bool) mask[x_l:x_r, y_l:y_r, z_l:z_r] = 1