python rgba_Python cv2.COLOR_RGBA2BGR属性代码示例

# 需要导入模块: import cv2 [as 别名]

# 或者: from cv2 import COLOR_RGBA2BGR [as 别名]

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值