利用genjson和WSI切片生成训练集

利用genjson和WSI切片生成训练集与对应的mask

import cv2
import numpy as np
import json
import os
import glob
from PIL import Image
# 获取图片信息
# 1、利用WSI生成带有坐标信息的片,得到数据文件夹
img = np.zeros((1080, 1920), np.uint8)

# 获取多边形信息,从geojson中获取
def gen_coordinate(path_to_geojson:str)->list : # return :[[id,label,coordinate],[],...]
    with open(path_to_geojson,encoding="utf-8") as f:
        a = json.load(f,encoding="utf-8")
    coordinate=[]
    for i in range(len(a['features'])):
        current = []
        label = 0
        
        if a['features'][0]['properties']['classification']['name'] =='低级别':
            label = 1
            
        current.append(i)
        current.append(label)
        current.append(a['features'][i]['geometry']['coordinates'][0])

        coordinate.append(current)
    return coordinate

# 读取图片路径
def read_path(path,suffix:str)->list: # str: jpg  # path = 'F:\\Temp\\animal\\animal_photos\\'
    # 存放路径的列表
    current = []
    # os.listdir(path)函数列出指定路径path下的所有文件和文件夹
    # os.path.isdir()函数判断是否是一个文件夹
    # glob.glob()函数查找符合要求的文件
    path_to_image=[path+x for x in os.listdir(path)]
    # print(path_to_image)
    # print("path_to_image[0]:",path_to_image[0])
    # for image_path in path_to_image:
    #     for path in glob.glob(image_path+'/*.{}'.format(str)):
    #         # 如果不加后缀,path与src没有区别
    #         print("path:",path)
    #         current.append(path)
    return path_to_image

# 从路径名中获取图片坐标信息
def get_image_coordinate(strings:str)->list:# list:[left_x,left_y,right_x,right_y] # strings:/cluster/home/ljk/mmsegmentation/data/pathology/processed/grid/tile_0_level0_5414-10932-6438-11956.png
    x = strings.split('_')
    coor=x[-1].split('-')
    coor[-1]=coor[-1].split('.')[0]
    coor_int = [int(x) for x in coor]
    return coor_int
    
def gen_mask(path_to_image:str,path_to_json:str,path_to_save:str):
    mask_coordinate = gen_coordinate(path_to_json)
    list_image = read_path(path_to_image,"png")
    for image_path,i in zip(list_image,range(len(list_image))):
        print("image:",i)
        image = cv2.imread(image_path,cv2.IMREAD_COLOR)
        image_coodinate = get_image_coordinate(image_path)
        mask = np.zeros((1024, 1024), np.uint8)
        # [[id,label,coordinate],[],...]
        # "coordinates":[[[10576, 7510],[10488, 7514],[10403, 7569],[10345, 7631]]]
        currents = []
        for c_ in mask_coordinate:
            # print("mask_coordinate[2]:",np.array(c_[2]).shape) # mask_coordinate[2]: (15, 2)
            current_point = []
            if c_[1]==1:# label = 1 
                for re_c in c_[2]:
                    if isinstance(re_c[0],list):
                        re_c=re_c[0]     
                        current_point.append([re_c[0]-image_coodinate[0],re_c[1]-image_coodinate[1]])
                    else:    
                        current_point.append([re_c[0]-image_coodinate[0],re_c[1]-image_coodinate[1]])
                # print("current_point:",np.array(current_point).shape)
            if len(current_point)<=1:
                continue
            currents.append(np.array(current_point))
        # print("currents[0]:",currents[0])
        # print("currents.shape:",np.array(currents).shape)
        mask = cv2.fillPoly(mask, np.array(currents), (255))
        means, stddev = cv2.meanStdDev(mask)
        # if means > 0:
        with open(path_to_save+"/mask"+"/seg.txt", "a") as f:
            f.write("seg_{}".format(i) + "\n")
            Image.fromarray(mask).save(path_to_save+"/mask_visualization"+"/mask_{}.png".format(i), 'PNG')
        with open(path_to_save+"/image"+"/image.txt", "a") as f:
            f.write("image_{}".format(i) + "\n")    
            Image.fromarray(image).save(path_to_save+"/image_all"+"/{}.png".format(i), 'PNG')
    return 0



# img = np.zeros((1080, 1920), np.uint8)
# mask = np.zeros((1080, 1920), np.uint8)
# area1 = np.array([[-250, 200], [300, 100], [750, 800], [100, 2000],[250, 200]])
# area2 = np.array([[1000, 200], [1500, 200], [1500, 400], [2000, 400]])
 
# # 根据多边形生成掩码
# img = cv2.fillPoly(img, [area1, area2], (1))
# # 掩码color固定  # 低级别 0,0,0 黑色  其它级别 255,0,0   0,255,0  0,0,255
# mask[img == 1] = 1
# Image.fromarray(mask).save("./try.png", 'PNG')

# 保存图片
# cv2.imwrite('./1.jpg',img)


if __name__ == '__main__':
    # a = read_path("/cluster/home/ljk/mmsegmentation/data/pathology/processed/grid/","png")
 
    
    # get_image_coordinate("/cluster/home/ljk/mmsegmentation/data/pathology/processed/grid/tile_0_level0_5414-10932-6438-11956.png")
    gen_mask("/****/grid/","/****/2022-28873-5.geojson","/****/dataset")
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值