Convert RGB image to index image

前端 未结 6 2336
一个人的身影
一个人的身影 2020-12-11 22:16

I want to convert a 3 channel RGB image to a index image with Python. It\'s used for handling the labels of training a deep net for semantic segmentation. By index image I m

6条回答
  •  无人及你
    2020-12-11 22:42

    Here's a small utility function to convert images (np.array) to per-pixel labels (indices), which can also be a one-hot encoding:

    def rgb2label(img, color_codes = None, one_hot_encode=False):
        if color_codes is None:
            color_codes = {val:i for i,val in enumerate(set( tuple(v) for m2d in img for v in m2d ))}
        n_labels = len(color_codes)
        result = np.ndarray(shape=img.shape[:2], dtype=int)
        result[:,:] = -1
        for rgb, idx in color_codes.items():
            result[(img==rgb).all(2)] = idx
    
        if one_hot_encode:
            one_hot_labels = np.zeros((img.shape[0],img.shape[1],n_labels))
            # one-hot encoding
            for c in range(n_labels):
                one_hot_labels[: , : , c ] = (result == c ).astype(int)
            result = one_hot_labels
    
        return result, color_codes
    
    
    img = cv2.imread("input_rgb_for_labels.png")
    img_labels, color_codes = rgb2label(img)
    print(color_codes) # e.g. to see what the codebook is
    
    img1 = cv2.imread("another_rgb_for_labels.png")
    img1_labels, _ = rgb2label(img1, color_codes) # use the same codebook
    

    It calculates (and returns) the color codebook if None is supplied.

提交回复
热议问题