【问题标题】:How to build the binary mask for U-net?如何为 U-net 构建二进制掩码?
【发布时间】:2021-06-24 08:23:30
【问题描述】:

我正在使用 json 文件中的标签文件,但是..如何将这些文件转换为 U-net 模型的 png 中的掩码文件?

【问题讨论】:

    标签: annotations semantic-segmentation


    【解决方案1】:
    import os
    import cv2
    import json
    import glob
    import numpy as np
    
    
    #save all image files and json files separately to list
    image_list = sorted(glob.glob('img_json/*.png'))
    ann_list = sorted(glob.glob('img_json/*.json'))
    
    #create blank mask with image sizes
    def create_binary_masks(im, shape_dicts):
        
        blank = np.zeros(shape=(im.shape[0], im.shape[1]), dtype=np.float32)
        for shape in shape_dicts:
            points = np.array(shape['points'], dtype=np.int32)
            cv2.fillPoly(blank, [points], 255)
        return blank
    
    #get annotated points
    def get_poly(ann_path):
        
        with open(ann_path) as handle:
            data = json.load(handle)
        shape_dicts = data['shapes']
        
        return shape_dicts
    
    #iterate every image and its json file to create binary mask
    for im_fn, ann_fn in zip(image_list, ann_list):
        
        im = cv2.imread(im_fn, 0)
        shape_dicts = get_poly(ann_fn)
        im_binary = create_binary_masks(im, shape_dicts)
        
        #extract the name of image file
        filename = im_fn.split('.png')[-2].split('\\')[-1] + '.png'
        cv2.imwrite(filename, im_binary)
    

    这是我找到并编辑的代码,以匹配您的要求。只需使用您的图像和注释文件夹的规范运行此代码。通常所有带注释的图像及其 json 文件都保存在同一个文件夹中并具有相同的名称。要成功运行此代码,您必须执行上述操作。 在我运行 for 循环以遍历 image 和 ann_list 的最后一个代码块中,我保存了与其图像同名的二进制掩码。仔细检查您的路径以使用 split 提取文件名

    【讨论】:

      猜你喜欢
      • 2020-04-13
      • 2022-09-23
      • 2017-10-16
      • 2021-04-08
      • 2020-06-14
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多