Remote-sensing-image-semantic-segmentation icon indicating copy to clipboard operation
Remote-sensing-image-semantic-segmentation copied to clipboard

create_train_val_label.py 将label由rgb转为单个值很慢,借助numpy可快速转换

Open pdj369 opened this issue 1 year ago • 0 comments

实现: def get_label_from_palette(label_img, palette_file='Palette.json'): with open(palette_file, 'r') as fp: text = json.load(fp) palette_values = np.array(list(text.values())) palette_keys = np.array(list(text.keys())) # 将三维RGB图像展平为二维形状 flat_label_img = label_img.reshape((-1, 3))

    mask = np.all(np.equal(palette_values, flat_label_img[:, None]), axis=2)
    indices = np.where(mask)
    labels = palette_keys[indices[1]]
    # 将标签重新形状为与原始图像相同
    label = labels.reshape(label_img.shape[:2])

    return label.astype(np.uint8)

pdj369 avatar Oct 31 '23 07:10 pdj369