【语义分割】label2color2label_灰度标签彩色化、彩色标签灰度化

项目代码 https://gitee.com/zengxy2020/csdn_label2color2label在这里插入图片描述

0.应用背景

  1. 语义分割标签文件,一般为灰度,转换为彩色方便查看预训练模型在不同模型上推理结果。
  2. GANs的很多模型,数据集需要彩色的语义便签、或灰度的语义便签训练,二者之间的转换是比较重要的。(例如 pix2pix |pix2pixhd

1. label2color

  • 读取 .png格式的语义分割图(.jpg会改变像素值)
  • 设置numpy格式的灰度与rgb彩色映射表(colormap)
  • numpy快速转换

1.1 随机映射颜色

简略代码:完整见附录

src_label = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
n_label = 20 # the  number of label class

#random_color_map
random_state = np.random.RandomState(seed=1234)
color_custom_map = np.random.randint(low=0, high=255, size=(n_label, 3), dtype=np.uint8)

#核心转换,将所有灰度值一次性传入,由索引得到对应的rgb值
color_img = colormap[src_label]

save_path=os.path.join('./out_color', name)
cv2.imwrite(save_path, color_img)
随机映射颜色效果(每次启动都不同)

在这里插入图片描述

1.2 语义分割常用映射

 cmap = np.zeros((n_label, 3), dtype=np.uint8)
 for i in range(n_label):
     id = i
     r, g, b = 0, 0, 0
     for j in range(8):
         r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))
         g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))
         b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))
         id = id >> 3

     if not rgb1_bgr0:
         cmap[i, 0] = b
         cmap[i, 1] = g
         cmap[i, 2] = r
     else:
         cmap[i, 0] = r
         cmap[i, 1] = g
         cmap[i, 2] = b

2. 彩色label转换为gray

简略代码:完整见附录

# 512x1024  time=0.05s
def color2gray(image_rgb, rgb_mapping):
    gray_img = np.zeros(shape=(image_rgb.shape[0], image_rgb.shape[1]), dtype=np.uint8)
    _st_rgb2gray = time.time()
    for map_idx, rgb in enumerate(rgb_mapping):
        idx = np.where((image_rgb[..., 0] == rgb[0]) & (image_rgb[..., 1] == rgb[1]) & (image_rgb[..., 2] == rgb[2]))
        gray_img[idx] = map_idx
    print("rgb 2 gray time used: {}".format(time.time() - _st_rgb2gray))
    return gray_img

3. 运行附录程序

  • label 转化到 彩色 再转换为 label 根据结果灰度值没发生跳变(语义类别不变),证明转换成功可逆
    在这里插入图片描述

3.1 逐像素查看灰度验证(非必要

4. 附录

4.1 main.py

# -*- coding: utf-8 -*-
# @Time : 2022/3/28 18:16 -2022/03/30 12:10
# @Author : XyZeng

import os
import cv2
import numpy as np

import rgb2gray2rgb
import label2color

def label_color_label(img_path, color_map, debug=1):
    src_label = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)

    # resize
    # src_label = cv2.resize(img, (256, 256))
    '''

    label2color
    '''
    color_img = rgb2gray2rgb.gray2color(src_label, color_map)
    #save
    dir, name = os.path.split(img_path)
    save_path=os.path.join('./out_color', name)
    cv2.imwrite(save_path, color_img)

    '''
    color2label
    '''
    gray_img = rgb2gray2rgb.color2gray(color_img, color_map)

    '''
        原始lable图像的灰度值
        lable2color2label图像的灰度值,
    
    '''
    pix_value = np.unique(src_label)
    print('src_label_value', pix_value)
    pix_value = np.unique(gray_img)
    print("label2color2label_value", pix_value)

    if debug:
        cv2.imshow('label2color', color_img)
        cv2.imshow('color2label', gray_img)
        cv2.waitKey(0)


if __name__ == '__main__':

    img_dir = './label/'

    n_label = 20 # the  number of label class

    color_map1 = label2color.label_colormap(n_label=n_label, rgb1_bgr0=0)

    '''
    random gene color map
    '''
    random_state = np.random.RandomState(seed=1234)
    color_custom_map = np.random.randint(low=0, high=255, size=(n_label, 3), dtype=np.uint8)

    for img in sorted(os.listdir(img_dir)):

        if not img.endswith((".png", ".jpg")):
            continue

        img_path = os.path.join(img_dir, img)
        label_color_label(img_path, color_map1, debug=1)


# Ctrl + Alt + L     pycharm代码整理

4.2 rgb2gray2rgb.py

# -*- coding: utf-8 -*-
# @Time : 2022/3/30 14:57
# @Author : XyZeng
import time

import numpy as np
import cv2

def gray2color(gray_img, colormap):
    '''

    :param gray_img:  (h,w)  max_value=num_class
    :param colormap:  (num_class,3) # colormap的索引对应了灰度值
    :return: color_img: (h,w,3) colormap的索引与label中灰度值一一顺序对应;如colormap[0]的颜色对应gray_image==0
    '''
    color_img = colormap[gray_img]

    return color_img


# 512x1024  time=0.05s
def color2gray(image_rgb, rgb_mapping):
    gray_img = np.zeros(shape=(image_rgb.shape[0], image_rgb.shape[1]), dtype=np.uint8)
    _st_rgb2gray = time.time()
    for map_idx, rgb in enumerate(rgb_mapping):
        idx = np.where((image_rgb[..., 0] == rgb[0]) & (image_rgb[..., 1] == rgb[1]) & (image_rgb[..., 2] == rgb[2]))
        gray_img[idx] = map_idx
    print("rgb 2 gray time used: {}".format(time.time() - _st_rgb2gray))
    return gray_img

4.3 label2color.py

'''
   https://github.com/hhj1897/face_parsing/blob/master/ibug/face_parsing/utils.py
'''

import numpy as np

random_state = np.random.RandomState(seed=1234)


def label_colormap(n_label=11,rgb1_bgr0=1):
    """Label colormap.
    Parameters
    ----------
    n_labels: int
        Number of labels (default: 11).
    value: float or int
        Value scale or value of label color in HSV space.
    Returns
    -------
    cmap: numpy.ndarray, (N, 3), numpy.uint8
        Label id to colormap.
    """
    if n_label == 11:  # helen, ibugmask
        cmap = np.array(
            [
                (0, 0, 0),
                (255, 255, 0),
                (139, 76, 57),
                (139, 54, 38),
                (0, 205, 0),
                (0, 138, 0),
                (154, 50, 205),
                (72, 118, 255),
                (255, 165, 0),
                (0, 0, 139),
                (255, 0, 0),
            ],
            dtype=np.uint8,
        )
    elif n_label == 19:  # celebamask-hq
        cmap = np.array(
            [
                (0, 0, 0),
                (204, 0, 0),
                (76, 153, 0),
                (204, 204, 0),
                (51, 51, 255),
                (204, 0, 204),
                (0, 255, 255),
                (255, 204, 204),
                (102, 51, 0),
                (255, 0, 0),
                (102, 204, 0),
                (255, 255, 0),
                (0, 0, 153),
                (0, 0, 204),
                (255, 51, 153),
                (0, 204, 204),
                (0, 51, 0),
                (255, 153, 51),
                (0, 204, 0),
            ],
            dtype=np.uint8,
        )
    else:

        def bitget(byteval, idx):
            return (byteval & (1 << idx)) != 0

        cmap = np.zeros((n_label, 3), dtype=np.uint8)
        gray_map=np.zeros((n_label))
        for i in range(n_label):
            id = i
            r, g, b = 0, 0, 0
            for j in range(8):
                r = np.bitwise_or(r, (bitget(id, 0) << 7 - j))
                g = np.bitwise_or(g, (bitget(id, 1) << 7 - j))
                b = np.bitwise_or(b, (bitget(id, 2) << 7 - j))
                id = id >> 3

            if not rgb1_bgr0:
                cmap[i, 0] = b
                cmap[i, 1] = g
                cmap[i, 2] = r
            else:
                cmap[i, 0] = r
                cmap[i, 1] = g
                cmap[i, 2] = b

            # gray_map[i]= b*0.114 + 0.57*g + 0.299*r

    return cmap

4.4 灰度原图

在这里插入图片描述

  • 10
    点赞
  • 76
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
灰度图像彩色通常有两种方法:基于统计学的方法和基于深度学习的方法。我这里给出一种基于统计学的方法: 1. 读入灰度图像; 2. 将灰度图像转换为 LAB 颜色空间; 3. 在 AB 通道上进行 k-means 聚类,将像素点聚成 k 类; 4. 计算每一类的 AB 通道的平均值; 5. 生成一个和原图像大小相同的空白彩色图像; 6. 将每一个像素点的灰度值对应的类别的平均值填入空白彩色图像的 AB 通道中; 7. 将彩色图像转换回 RGB 颜色空间。 以下是 MATLAB 代码实现: ```matlab % 读入灰度图像 gray_img = imread('gray_image.png'); % 将灰度图像转换为 LAB 颜色空间 lab_img = rgb2lab(gray_img); % 在 AB 通道上进行 k-means 聚类,将像素点聚成 k 类 k = 3; ab_img = lab_img(:,:,2:3); ab_img = im2single(ab_img); pixel_labels = kmeans(ab_img(:,:), k, 'MaxIter', 100); % 计算每一类的 AB 通道的平均值 ab_mean = zeros(k,2); for i = 1:k ab_mean(i,:) = mean(ab_img(pixel_labels==i,:)); end % 生成一个和原图像大小相同的空白彩色图像 color_img = zeros(size(gray_img,1), size(gray_img,2), 3, 'uint8'); % 将每一个像素点的灰度值对应的类别的平均值填入空白彩色图像的 AB 通道中 for i = 1:size(gray_img,1) for j = 1:size(gray_img,2) pixel_label = pixel_labels((i-1)*size(gray_img,2)+j); color_img(i,j,2:3) = ab_mean(pixel_label,:); end end % 将彩色图像转换回 RGB 颜色空间 color_img = lab2rgb(color_img); % 显示彩色图像 imshow(color_img); ``` 注意,这种方法的效果并不是很好,生成的彩色图像可能会失去很多细节。如果需要更好的效果,可以尝试基于深度学习的方法,例如使用 GAN(生成对抗网络)进行彩色
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

曾小蛙

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值