SBD数据集(VOC2021扩展数据集)mat格式转化为png,以及可视化

我是跟随这篇博客进行转换的

博客
图像语义分割网络实战(一)增强数据集PASCAL VOC2012 Aug的制作
https://blog.csdn.net/qq_39444453/article/details/107231357?utm_medium=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-4.test_version_3&depth_1-utm_source=distribute.pc_relevant_download.none-task-blog-2~default~BlogCommendFromBaidu~default-4.test_version_

博客2
SBD数据集从这里下载:https://www.jianshu.com/p/da4641f50000
简书: pascal VOC 2012 分割数据集及其增强版SBD介绍
SBD官网:http://home.bharathh.info/pubs/codes/SBD/download.html

博客3
可视化:灰度标签转化为彩色标签可视化
https://www.freesion.com/article/77741310718/

博客4
下载tools
tools我是从这里下载的(VOC增强数据集数据转换代码)https://download.csdn.net/download/qq_31347869/11850943?utm_medium=distribute.pc_relevant.none-task-download-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-download-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

一、根据博客2下载SBD数据集

二、博客1介绍SBD数据集情况如下

SBD

增强版的VOC跟原版没什么区别,只是增加了数据。介绍下载的文件夹中每个文件的作用。

  •     img: 增强版的原图,共11355张图片
  •     cls: 用于语义分割的label,共11355个.mat文件,每个.mat文件对应一张原图
  •     inst: 用于实例分割的label,也是11355个.mat文件
  •     tools: 3个用于数据转换的脚本,在后面数据转换时用到(这个文件我没有)
  •     train.txt: 训练集索引
  •     val.txt: 验证集索引

三、下载tools

tools我是从这里下载的(VOC增强数据集数据转换代码):https://download.csdn.net/download/qq_31347869/11850943?utm_medium=distribute.pc_relevant.none-task-download-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control&depth_1-utm_source=distribute.pc_relevant.none-task-download-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.control

四、开始转换mat2png

由于pascal voc 2012增强版的数据集的label是.mat格式的文件,需要将其转换为.png格式的图片。转化后的图片是8-bit的灰度图。

  1. 1)在benchmark_RELEASE目录下创建一个名为cls_png的目录用于存放转化后的png图片

  2. 2)在benchmark_RELEASE目录下执行mat2png.py脚本

  3. python tools/mat2png.py cls cls_png(cls是读入文件夹、cls_png是输出文件夹)

mat2png.py文件如下

from __future__ import print_function
import os
import sys
import glob
from PIL import Image as PILImage
import os
import cv2
from utils import mat2png_hariharan

def main():
  input_path, output_path = process_arguments(sys.argv) 

  if os.path.isdir(input_path) and os.path.isdir(output_path):
    mat_files = glob.glob(os.path.join(input_path, '*.mat'))
    convert_mat2png(mat_files, output_path)
  else:
    help('Input or output path does not exist!\n')

def process_arguments(argv):
  num_args = len(argv)

  input_path  = None
  output_path = None 

  if num_args == 3:
    input_path  = argv[1]
    output_path = argv[2]
  else:
    help()

  return input_path, output_path

def convert_mat2png(mat_files, output_path):
  if not mat_files:
    help('Input directory does not contain any Matlab files!\n')

  for mat in mat_files:
    numpy_img = mat2png_hariharan(mat)
    # pil_img = PILImage.fromarray(numpy_img)
    # pil_img.save(os.path.join(output_path, modify_image_name(mat, 'png')))
    cv2.imwrite(os.path.join(output_path, modify_image_name(mat, 'png')), numpy_img)

# Extract name of image from given path, replace its extension with specified one
# and return new name only, not path.
def modify_image_name(path, ext):
  return os.path.basename(path).split('.')[0] + '.' + ext

def help(msg=''):
  print(msg +
        'Usage: python mat2png.py INPUT_PATH OUTPUT_PATH\n'
        'INPUT_PATH denotes path containing Matlab files for conversion.\n'
        'OUTPUT_PATH denotes path where converted Png files ar going to be saved.'
        , file=sys.stderr)

  exit()

if __name__ == '__main__':
  main()

值得注意的是这里转化语义cls标签直接按照上述进行操作就行了,如果转化实力标签inst需要做两件事:

1 记得将mat2png调用的utils.mat2png_hariharan这里的key改成GTinst

2 这里会出现PIL的报错,可能是我自己的PIL库的版本问题,
不能将数组型array转化成PIL类型(方案一)
因此我们采用方案二,由于cv2可以直接保存array格式直接cv2.imwrite(path, img)即可

五、根据博客3灰度标签转化为彩色标签

仅需要改变成如下读入、输出路径即可,这是两个文件夹对应创建即可

path = './cls_png/*.png'                  
path_converted = './cls_png_color'

from __future__ import print_function
import os
import sys
import numpy as np
from skimage.io import imread, imsave
import glob


def pascal_palette():
    palette = {(0, 0, 0): 0,
               (128, 0, 0): 1,
               (0, 128, 0): 2,
               (128, 128, 0): 3,
               (0, 0, 128): 4,
               (128, 0, 128): 5,
               (0, 128, 128): 6,
               (128, 128, 128): 7,
               (64, 0, 0): 8,
               (192, 0, 0): 9,
               (64, 128, 0): 10,
               (192, 128, 0): 11,
               (64, 0, 128): 12,
               (192, 0, 128): 13,
               (64, 128, 128): 14,
               (192, 128, 128): 15,
               (0, 64, 0): 16,
               (128, 64, 0): 17,
               (0, 192, 0): 18,
               (128, 192, 0): 19,
               (0, 64, 128): 20}

    return palette


def convert_from_color_segmentation(seg):
    color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
    palette = pascal_palette()

    for c, i in palette.items():
        color_seg[ seg == i] = c
        
    color_seg = color_seg[..., ::-1]

    return color_seg


def main():
    ##
    ext = '.png'
    ##
    # path, txt_file, path_converted = process_arguments(sys.argv)
    path = './voc12_aug/benchmark_RELEASE/dataset/seg/*.png'					# 前面转换后的语义图片
    path_converted = './voc12_aug/benchmark_RELEASE/dataset/seg_visualize'				# 着色后的图片的保存位置

    # Create dir for converted labels
    if not os.path.isdir(path_converted):
        os.makedirs(path_converted)

    f = glob.glob(path)
    for img_name in f:
        img_base_name = os.path.basename(img_name)
        img = imread(img_name)

        if (len(img.shape) == 2):
            img = convert_from_color_segmentation(img)
            imsave(os.path.join(path_converted, img_base_name), img)
        else:
            print(img_name + " is not composed of three dimensions, therefore "
                                "shouldn't be processed by this script.\n"
                                "Exiting.", file=sys.stderr)
            exit()


def process_arguments(argv):
    if len(argv) != 4:
        help()

    path = argv[1]
    list_file = argv[2]
    new_path = argv[3]

    return path, list_file, new_path


def help():
    print('Usage: python convert_labels.py PATH LIST_FILE NEW_PATH\n'
          'PATH points to directory with segmentation image labels.\n'
          'LIST_FILE denotes text file containing names of images in PATH.\n'
          'Names do not include extension of images.\n'
          'NEW_PATH points to directory where converted labels will be stored.'
          , file=sys.stderr)
    exit()


if __name__ == '__main__':
    main()

最后展示下我自己目录:
绿色是我自己创建的文件夹
褐色是从博客4下载
紫色从博客3下载

---benchmark_RELEASE
------dataset
---------cls
---------cls_png
---------cls_png_color
---------img
---------inst
---------tools
------------convert_labels.py
------------grey2color.py
------------mat2png.py
------------utils.py


版权声明:本文为zjc910997316原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
THE END
< <上一篇
下一篇>>