【语义分割数据预处理】

本文介绍了语义分割数据预处理的步骤,包括使用Labelme进行标注,将json转换为mask,进行像素序列化,以及进行1024尺寸的灰度和RGB图像裁剪。建议mask保存为png格式以避免信息损失。
摘要由CSDN通过智能技术生成

1 Labelme 标注数据

安装labelme

pip install lamebelme

使用labelme

activate labelme
labelme

在这里插入图片描述
得到json文件

2 json2mask

import os
import shutil

def json_to_mask(json_path, process_path, mask_path, img_path):
    # 1) 将每一个json文件转换为dataset文件夹存入process中,文件夹名与json文件名相同
    # 文件名为 X.json
    filelist = os.listdir(json_path)
    for file in filelist:
        filename = file.split('.')[0]   # X.json分离得到X字符
        new_path = filename
        cmd = 'labelme_json_to_dataset ' + json_path + '/' + file + ' -o ' + process_path + '/' + new_path      #单双引号应该都可以
        os.system(cmd)

    # 2) 将process中每个文件夹中的label.png文件找出,重命名并保存至mask文件夹
    for folder in os.listdir(process_path):
        processed_mask_path = process_path + '/' + folder   #每个json文件转换得到的文件夹
        for filename in os.listdir(processed_mask_path):
            if filename == 'label.png':
                new_name = 'label_' + folder + '.png' # 重命名mask文件
                shutil.copy(processed_mask_path + '/' + filename, mask_path + '/' + new_name)  # 将单个mask复制到另一个文件夹

            if filename == 'img.png':               # 由于标记用的图不一定是原图,可以跳过这步
            	new_name = 'img_' + folder + '.png' # 重命名原图
            	shutil.copy(processed_mask_path + '/' + filename, img_path + '/' + new_name) # 将单个原图复制到另一个文件夹


# 路径设置/命名
path = 'D:\zcy_dataset\wrj_dataset'  # 上级目录设置,默认为当前目录
json_path =  'D:\zcy_dataset\wrj_dataset\json'
process_path = path + '/process'
mask_path = path + '/mask'
img_path = path + '/img'

all_path = [process_path, mask_path, img_path]
for path in all_path:
    if not os.path.exists(path):
        os.makedirs(path)

json_to_mask(json_path, process_path, mask_path, img_path)

3 Pixel serialization

由于得到mask的像素不是从0依次排列的,因此需要mask像素点的转换

import cv2
import os
import numpy as np

COLORMAP1 = np.array([
    [0], [14], [38], [52], [75], [113]
])
def rgb2gray(mask):
    mask = np.expand_dims(mask, axis=-1)
    print(mask.shape)
    label_mask = np.zeros((mask.shape[0], mask.shape[1]), dtype=np.int8)
    # 标签处理
    for ii, label in enumerate(COLORMAP1):
        # 通道方向上计算元素的逻辑与 n*h*w->h*w
        locations = np.all(mask == label, axis=-1)
        label_mask[locations] = ii
    return label_mask.astype(np.uint8)

def gray2rgb(label_mask):
    mask = np.zeros((label_mask.shape[0], label_mask.shape[1], 3),dtype=np.int8)
    for i in range(6):
        # 这里也困了很久没相通,结果答案很简单,大家可以自行打印出来看一下中间的东西是什么
        mask[label_mask==i]=COLORMAP[i]
    return mask.astype(np.uint8)

root = 'D:/zcy_dataset/wrj_dataset/train/labels'
save_root = 'D:/zcy_dataset/wrj_dataset/train/label'
for file in os.listdir(root):
    path = root + '/' + file
    label = cv2.imread(path, 0)
    label = rgb2gray(label)
    save_path = save_root + '/' + file
    cv2.imwrite(save_path, label)
    print(np.unique(label))

得到了8000*6000的可训练的mask

4 Crop_gray_1024

import os
import cv2 as cv
import numpy as np
from tqdm import tqdm


ROOT = r'D:\zcy_dataset\wrj_dataset\test\labels'

for name in tqdm(os.listdir(ROOT)):
    img = cv.imread(os.path.join(ROOT, name), 0)
    H, W = img.shape
    side = 1024
    num_h = H // side + 1
    num_w = W // side + 1
    img = np.array(img)
    img_crop = np.zeros((side, side))
    image = []
    for h in range(0, num_h):
        for w in range(0, num_w):
            # 可以无填充的裁剪
            if w < num_w - 1 and h < num_h - 1:
                img_crop = img[h * side:(h + 1) * side, w * side:(w + 1) * side]
                image.append(img_crop)
            elif w == num_w - 1 and h < num_h - 1:
                img_crop = img[h * side:(h + 1) * side, W-side:W]
                image.append(img_crop)
            elif h == num_w - 1 and w < num_w - 1:
                img_crop = img[H - side:H, w * side:(w + 1) * side]
                image.append(img_crop)
            else:
                img_crop = img[H - side:H, W-side:W]
                image.append(img_crop)
            pass
        pass

    save_path = ROOT.replace("wrj_dataset", "wrj_1024")  # 保存路径
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    for i in range(0, len(image)):
        image_i = image[i]
        path_image_i = os.path.join(save_path, name[:-4] + '_' + str(i) + '.png')
        cv.imwrite(path_image_i, image_i)

5 Crop_rgb_1024

import os
import cv2 as cv
import numpy as np
from tqdm import tqdm


ROOT = r'D:\zcy_dataset\wrj_dataset\test\images'

for name in tqdm(os.listdir(ROOT)):
    img = cv.imread(os.path.join(ROOT, name))
    H, W, _ = img.shape
    side = 1024  # 裁剪大小200*200
    num_h = H // side + 1
    num_w = W // side + 1
    img = np.array(img)
    # img_gt = np.array(img_gt)
    img_crop = np.zeros((side, side, 3))
    image = []
    for h in range(0, num_h):
        for w in range(0, num_w):
            # 可以无填充的裁剪
            if w < num_w - 1 and h < num_h - 1:
                img_crop = img[h * side:(h + 1) * side, w * side:(w + 1) * side]
                image.append(img_crop)
            elif w == num_w - 1 and h < num_h - 1:
                img_crop = img[h * side:(h + 1) * side, W-side:W]
                image.append(img_crop)
            elif h == num_w - 1 and w < num_w - 1:
                img_crop = img[H - side:H, w * side:(w + 1) * side]
                image.append(img_crop)
            else:
                img_crop = img[H - side:H, W-side:W]
                image.append(img_crop)
            pass
        pass

    save_path = ROOT.replace("wrj_dataset", "wrj_1024")  # 保存路径
    if not os.path.exists(save_path):
        os.mkdir(save_path)

    for i in range(0, len(image)):
        image_i = image[i]
        path_image_i = os.path.join(save_path, name[:-4] + '_' + str(i) + '.jpg')
        cv.imwrite(path_image_i, image_i)

注意:mask最好保存为png格式,jpg会有损失

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

买代码v 18327244279

谢谢你这莫可爱还打赏我!!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值