python将灰度图保存为8bit彩色图

问题:

在制作语义分割图像数据集过程中,输出gt可能是表示类别的单通道灰度图,为了可视化,需要使用颜色映射转化为彩色图,灰度转3通道彩图参考使用numpy、颜色映射表 将灰度图变彩图,灰度转3通道彩色可参考  使用numpy、颜色映射表 将灰度图变彩图

python可实现将颜色映射表嵌入单通道图片,将灰度图转单通道(8bit)彩色图,

目前存在以下问题:

  • 转换速度略慢
  • cv2无法正常读取
  • 尽量保存为png格式,其他格式占用空间较大

 

代码:

# -*- coding: utf-8 -*- 
# @Author  : zhaoHL
# @Contact : huilin16@qq.com
# @File    : test_code.py 
# @Time    : 2021/4/12 20:56 
# @Desc    :
from __future__ import print_function
from __future__ import division
import os, cv2
from skimage import io
import numpy as np
import pandas as pd
from PIL import Image
from tqdm import tqdm
import seaborn as sns
import matplotlib.pyplot as plt
import tifffile as tif
# import torch, paddle

from collections import namedtuple
Cls = namedtuple('cls', ['name', 'id', 'color'])
Clss = [
    Cls('bg', 0, (0, 0, 0)),
    Cls('cls1', 1, (255, 128, 128)),
    Cls('cls2', 2, (255, 255, 128)),
    Cls('cls3', 3, (128, 255, 128)),
    Cls('cls4', 4, (128, 255, 255)),
    Cls('cls5', 5, (0, 128, 255)),
    Cls('cls6', 6, (255, 0, 0)),
    Cls('cls7', 7, (0, 128, 192)),
    Cls('cls8', 8, (255, 0, 255)),
    Cls('cls9', 9, (255, 128, 64)),
    Cls('cls10', 10, (128, 64, 0)),
    Cls('cls11', 11, (128, 128, 0)),
]

# region 灰度转彩色
def get_color8bit(grays_path, colors_path):
    '''
    灰度图转8bit彩色图
    :param grays_path:  灰度图文件路径
    :param colors_path: 彩色图文件路径
    :return:
    '''
    if not os.path.exists(colors_path):
        os.makedirs(colors_path)
    file_names = os.listdir(grays_path)
    bin_colormap = get_putpalette(Clss)
    with tqdm(file_names) as pbar:
        for file_name in pbar:
            gray_path = os.path.join(grays_path, file_name)
            color_path = os.path.join(colors_path, file_name.replace('.tif','.png'))
            gt = Image.open(gray_path)
            gt.putpalette(bin_colormap)
            gt.save(color_path)
            pbar.set_description('get color')


def get_putpalette(Clss, color_other=[0, 0, 0]):
    '''
    灰度图转8bit彩色图
    :param Clss:颜色映射表
    :param color_other:其余颜色设置
    :return:
    '''
    putpalette = []
    for cls in Clss:
        putpalette += list(cls.color)
    putpalette += color_other * (255 - len(Clss))
    return putpalette


# endregion


if __name__ == '__main__':
    pass
    grays_path = r'E:\1_dataset\NB_S11_mini100\data\gt'
    colors_path = r'E:\1_dataset\NB_S11_mini100\data\Visual\gt_color2'
    color_file = r'E:\1_dataset\NB_S11_mini100\data\Visual\gt_color2\22.png'
    get_color8bit(grays_path,colors_path)

    # 可正常读取
    img = Image.open(color_file)
    img = np.array(img)
    print(img.shape, np.unique(img))
    img = io.imread(color_file)
    print(img.shape, np.unique(img))
    # img = tif.imread(color_file) # 智能读取tif格式文件
    # print(img.shape, np.unique(img))
    # 不能正常读取
    img = cv2.imread(color_file, cv2.IMREAD_COLOR)
    print(img.shape, np.unique(img))
    img = cv2.imread(color_file, cv2.IMREAD_GRAYSCALE)
    print(img.shape, np.unique(img))

 

  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值