简体   繁体   English

将灰度图像转换为 RGB,反之亦然

[英]Converting Grayscale Image to RGB and Vice veras

I wrote a code to convert ground truth grayscale mask to RGB, and Vice Versa, but RGB2Grayscales does not work as expected?我写了一个代码将地面实况灰度蒙版转换为 RGB,反之亦然,但 RGB2Grayscales 没有按预期工作?
Example of label, and converted RGB标签示例和转换后的 RGB
标签图像
颜色_图像

from __future__ import print_function, absolute_import, division
from collections import namedtuple
import numpy as np
import cv2

Label = namedtuple('Label',
                   ['name', 'id', 'trainId', 'category', 'categoryId', 'hasInstances', 'ignoreInEval', 'color', ])

labels = [
    #       name                     id    trainId   category            catId     hasInstances   ignoreInEval   color
    Label('unlabeled', 0, 19, 'void', 0, False, True, (0, 0, 0)),
    Label('ego vehicle', 1, 19, 'void', 0, False, True, (0, 0, 0)),
    Label('rectification border', 2, 19, 'void', 0, False, True, (0, 0, 0)),
    Label('out of roi', 3, 19, 'void', 0, False, True, (0, 0, 0)),
    Label('static', 4, 19, 'void', 0, False, True, (0, 0, 0)),
    Label('dynamic', 5, 19, 'void', 0, False, True, (111, 74, 0)),
    Label('ground', 6, 19, 'void', 0, False, True, (81, 0, 81)),
    Label('road', 7, 0, 'flat', 1, False, False, (128, 64, 128)),
    Label('sidewalk', 8, 1, 'flat', 1, False, False, (244, 35, 232)),
    Label('parking', 9, 19, 'flat', 1, False, True, (250, 170, 160)),
    Label('rail track', 10, 19, 'flat', 1, False, True, (230, 150, 140)),
    Label('building', 11, 2, 'construction', 2, False, False, (70, 70, 70)),
    Label('wall', 12, 3, 'construction', 2, False, False, (102, 102, 156)),
    Label('fence', 13, 4, 'construction', 2, False, False, (190, 153, 153)),
    Label('guard rail', 14, 19, 'construction', 2, False, True, (180, 165, 180)),
    Label('bridge', 15, 19, 'construction', 2, False, True, (150, 100, 100)),
    Label('tunnel', 16, 19, 'construction', 2, False, True, (150, 120, 90)),
    Label('pole', 17, 5, 'object', 3, False, False, (153, 153, 153)),
    Label('polegroup', 18, 19, 'object', 3, False, True, (153, 153, 153)),
    Label('traffic light', 19, 6, 'object', 3, False, False, (250, 170, 30)),
    Label('traffic sign', 20, 7, 'object', 3, False, False, (220, 220, 0)),
    Label('vegetation', 21, 8, 'nature', 4, False, False, (107, 142, 35)),
    Label('terrain', 22, 9, 'nature', 4, False, False, (152, 251, 152)),
    Label('sky', 23, 10, 'sky', 5, False, False, (70, 130, 180)),
    Label('person', 24, 11, 'human', 6, True, False, (220, 20, 60)),
    Label('rider', 25, 12, 'human', 6, True, False, (255, 0, 0)),
    Label('car', 26, 13, 'vehicle', 7, True, False, (0, 0, 142)),
    Label('truck', 27, 14, 'vehicle', 7, True, False, (0, 0, 70)),
    Label('bus', 28, 15, 'vehicle', 7, True, False, (0, 60, 100)),
    Label('caravan', 29, 19, 'vehicle', 7, True, True, (0, 0, 90)),
    Label('trailer', 30, 19, 'vehicle', 7, True, True, (0, 0, 110)),
    Label('train', 31, 16, 'vehicle', 7, True, False, (0, 80, 100)),
    Label('motorcycle', 32, 17, 'vehicle', 7, True, False, (0, 0, 230)),
    Label('bicycle', 33, 18, 'vehicle', 7, True, False, (119, 11, 32)),
    Label('license plate', -1, -1, 'vehicle', 7, False, True, (0, 0, 142)),
]


def trainIdToColor(trainId: int):
    for l in labels:
        if l.trainId == trainId:
            color = l.color
            break
    return color


def colortoTrainId(rgbColor):
    trainId = 0
    for l in labels:
        if l.color == rgbColor:
            trainId = l.trainId
            break
    return trainId


def gray2color(grayImage:np.ndarray,num_class:int):
    rgbImage=np.zeros((grayImage.shape[0],grayImage.shape[1],3),dtype='uint8')
    for cls in range(num_class):
        row,col=np.where(grayImage==cls)
        if (len(row)==0):
            continue
        color=trainIdToColor(cls)
        rgbImage[row,col]=color
    return rgbImage


def color2gray(colorImage:np.ndarray, bgr_color_space:bool):
    if bgr_color_space:
        colorImage = cv2.cvtColor(colorImage, cv2.COLOR_BGR2RGB)


    unique_color=np.unique(colorImage.reshape(-1, colorImage.shape[2]), axis=0)
    gray=np.zeros((colorImage.shape[0],colorImage.shape[1]),dtype=np.float32)
    for uc in unique_color:

        where_cond1= np.logical_and(colorImage[:,:,0]==uc[0],
                                    colorImage[:,:,1]==uc[1],
                                    colorImage[:,:,2]==uc[2])
        row,col=np.where(where_cond1)
        gray[row,col]=colortoTrainId(tuple(uc))

    return gray

when I use gray2color Everythings works fine.当我使用gray2color 时一切正常。 but when I try to convert an RGB image via color2gray , it converts but the result is not the same as original gray image.但是当我尝试通过color2gray转换 RGB 图像时,它会转换但结果与原始灰度图像不同。 (changes 19s to 13, other classes are fine) . (把19s改成13,其他班没问题) I checked to code multiple times but I don't know why I got bad results.我多次检查代码,但我不知道为什么我得到了不好的结果。
for clarification为了澄清
as you can see in returned grayscale there's no 19 value,all added up to 13s.正如您在返回的灰度中看到的那样,没有 19 值,全部加起来为 13 秒。

original grayscale:
unique: 0   1   2   4   5   7   8   10  11  13  19
count:  624649  168701  819940  2802    24885   12192   42082   37098   6791    115270  242742

returned grayscale:
unique: 0   1   2   4   5   7   8   10  11  13
count:  624649  168701  819940  2802    24885   12192   42082   37098   6791    358012


also color2gray function is very slow and time-consuming!还有color2gray函数很慢很费时间!

your trainId value is too low that's why your grayscale image is very dark.您的 trainId 值太低,这就是您的灰度图像非常暗的原因。 Increase the value of trainId in the Labels list and make your gray image datatype to be uint8增加 Labels 列表中 trainId 的值,并使您的灰度图像数据类型为 uint8

def color2gray(colorImage:np.ndarray, bgr_color_space:bool):
    if bgr_color_space:
        colorImage = cv2.cvtColor(colorImage, cv2.COLOR_BGR2RGB)


    gray=np.zeros((colorImage.shape[0],colorImage.shape[1]),dtype=np.uint8)

    for row in range(colorImage.shape[0]):
        for col in range(colorImage.shape[1]):
            # either multiply the result of colortoTrainId(tuple(uc)) with some number
            # or change the value of trainId in the Labels list
            gray[row,col]=colortoTrainId(tuple(colorImage[row,col]))*10

    return gray

I found the answer of 1 question myself我自己找到了 1 个问题的答案

np.logical_and() just take two arrays np.logical_and()只需要两个数组

so it must be used nested like this所以它必须像这样嵌套使用

where_cond1=np.logical_and(np.logical_and(colorImage[:,:,0]==uc[0],colorImage[:,:,1]==uc[1]), colorImage[:,:,2]==uc[2])

but the speed problem remains, any idea how to vectorize assignment?但速度问题仍然存在,知道如何矢量化分配吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM