patch_generation.py 文件源码

python
阅读 23 收藏 0 点赞 0 评论 0

项目:camelyon17 作者: hyosari 项目源码 文件源码
def patch_generation(tif_dir,mask_dir,save_fdir,save_pdir,save_cdir,slide_level,patch_size,patch_num,tumor_win=False):
    """
     save patch image and extraction point with csv, jpg image on the directory

     tif_dir : the tif directory
     mask_dir : the mask directory 
     save_fdir : patch saved directory
     save_pdir : point jpg directory 
     save_cdir : Y,X csv directory 
     slide : slide_level that mask image was applied with 
     patch_size : patch size square 
     patch_num : the number of patches in a whole slide 
     tumor_win : tumor window patch sampling true


        tif file and mask file sholud be one to one mached and same ordered 
    """

    list_tif_name = filelist_in_directory(tif_dir)
    list_mask_name = filelist_in_directory(mask_dir) 

    for slide_idx in range(len(list_tif_name)): 
        pwd_tif = join(tif_dir,list_tif_name[slide_idx])
        pwd_msk = join(mask_dir,list_mask_name[slide_idx]) 
        filename = splitext(list_tif_name[slide_idx])[0]

        # open slide, csv, BGR_image, mask
        slide = OpenSlide(pwd_tif) 
        f = open(save_cdir+filename+".csv",'wt')
        c_writer = csv.writer(f)
        c_writer.writerow(('Y','X'))
        rgba_pil = slide.read_region((0,0),slide_level,slide.level_dimensions[slide_level])
        bgr_im = cv2.cvtColor(np.array(rgba_pil),cv2.COLOR_RGBA2BGR)
        mask = cv2.imread(pwd_msk,cv2.IMREAD_GRAYSCALE)

        #if mask tunes 255 change to 1 
        if np.max(mask) == 255:
            mask = mask == 255
            mask = mask.astype(int)

        # sampling patches
        if tumor_win:
            patch_list,patch_point = tumor_patch_sampling_using_centerwin(slide,slide_level,mask,patch_size,patch_num)
        else:
            patch_list,patch_point = patch_sampling_using_integral(slide,slide_level,mask,patch_size,patch_num)
        p_l_size = patch_size/ slide.level_downsamples[slide_level] 
        p_l_size = int(p_l_size)

        #image wirte and save patches 
        for f_th in range(len(patch_list)): 
            cv2.imwrite(save_fdir+filename+"_patch_"+str(f_th)+"_"+str(patch_point[f_th][1])+"_"+str(patch_point[f_th][0])+"_"+str(patch_size)+".jpg",patch_list[f_th]) 

            c_writer.writerow((patch_point[f_th][1],patch_point[f_th][0]))
            cv2.rectangle(bgr_im,(patch_point[f_th][1],patch_point[f_th][0]),(patch_point[f_th][1]+p_l_size,patch_point[f_th][0]+p_l_size),(255,0,0),1)

        cv2.imwrite(save_pdir+filename+"_selection_point.jpg",bgr_im)
        print "complete patch extraction about "+ list_tif_name[slide_idx]
        f.close()

    return 0
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号