将灰度图像转换为RGB和副维拉

时间:2019-08-03 00:12:55

标签: python numpy image-processing computer-vision cv2

我写了一段代码将地面真实灰度蒙版转换为RGB,反之亦然,但是RGB2Grayscales不能按预期工作?
标签示例和转换后的RGB
label_image
Color_image

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

当我使用 gray2color 时,一切正常。但是当我尝试通过 color2gray 转换RGB图像时,它会转换,但结果与原始的灰度图像不同。 (将19s更改为13,其他课程都可以)。我检查了多次代码,但不知道为什么结果不好。
为澄清
如您所见,在返回的灰度中没有19的值,所有值加起来为13s。

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


color2gray 功能也非常缓慢且耗时!

2 个答案:

答案 0 :(得分:0)

您的trainId值太低,这就是您的灰度图像非常暗的原因。在“标签”列表中增加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

答案 1 :(得分:0)

我自己找到了一个问题的答案

<Select className='react-select-container' placeholder="Choose country" onChange={handleChange} labelKey="name" valueKey="code" getOptionLabel={option => { return option.name; }} getOptionValue={option => { return option.code; }} options={options} /> 只需要两个数组

因此必须像这样嵌套使用

np.logical_and()

但是速度问题仍然存在,对如何矢量化分配有任何想法吗?