【问题标题】:how to convert rle format of label-studio to black and white image masks如何将label-studio的rle格式转换为黑白图像蒙版
【发布时间】:2022-12-24 00:24:39
【问题描述】:

给定值数组如下:完整数组here

[{"format": "rle", "rle": [0, 18, 192, 0, 57, 27, 255, 255, 255, 0, 259, 96, 17, 192, 97, 255, 248, 239, 227, 16, 2....., 255, 142, 3, 130, 21, 128, 0], "brushlabels": ["crack"], "original_width": 640, "original_height": 480}]

我怎样才能转换成 image.png?

【问题讨论】:

    标签: python opencv image-processing


    【解决方案1】:

    您可以使用 np.reshape() 方法将数组重塑为图像的原始尺寸:

    import cv2
    import numpy as np
    
    data = [{"format": "rle", "rle": [255, 0, 0, 0, 0, 0, 0, 0, 255, 0, 0, 255, 0, 0, 0, 255, 0, 0], "brushlabels": ["crack"], "original_width": 2, "original_height": 3}]
    
    # Create image from data
    img = np.uint8(data[0]["rle"]).reshape(data[0]["original_height"], data[0]["original_width"], 3)
    
    # The image is completed, but it's tiny small in this example, so resize it to be able to visualize it
    img = cv2.resize(img, (200, 300), interpolation=cv2.INTER_NEAREST)
    
    # Show result
    cv2.imshow("Result", img)
    cv2.waitKey(0)
    

    输出:

    【讨论】:

    【解决方案2】:

    基于https://github.com/heartexlabs/label-studio-converter/blob/master/label_studio_converter/brush.py#L70

    from typing import List
    import numpy as np
    
    
    class InputStream:
        def __init__(self, data):
            self.data = data
            self.i = 0
    
        def read(self, size):
            out = self.data[self.i:self.i + size]
            self.i += size
            return int(out, 2)
    
    
    def access_bit(data, num):
        """ from bytes array to bits by num position"""
        base = int(num // 8)
        shift = 7 - int(num % 8)
        return (data[base] & (1 << shift)) >> shift
    
    
    def bytes2bit(data):
        """ get bit string from bytes data"""
        return ''.join([str(access_bit(data, i)) for i in range(len(data) * 8)])
    
    
    def rle_to_mask(rle: List[int], height: int, width: int) -> np.array:
        """
        Converts rle to image mask
        Args:
            rle: your long rle
            height: original_height
            width: original_width
    
        Returns: np.array
        """
    
        rle_input = InputStream(bytes2bit(rle))
    
        num = rle_input.read(32)
        word_size = rle_input.read(5) + 1
        rle_sizes = [rle_input.read(4) + 1 for _ in range(4)]
        # print('RLE params:', num, 'values,', word_size, 'word_size,', rle_sizes, 'rle_sizes')
    
        i = 0
        out = np.zeros(num, dtype=np.uint8)
        while i < num:
            x = rle_input.read(1)
            j = i + 1 + rle_input.read(rle_sizes[rle_input.read(2)])
            if x:
                val = rle_input.read(word_size)
                out[i:j] = val
                i = j
            else:
                while i < j:
                    val = rle_input.read(word_size)
                    out[i] = val
                    i += 1
    
        image = np.reshape(out, [height, width, 4])[:, :, 3]
        return image
    

    用法:

    image = rle_to_mask(
        result['value']['rle'], 
        result['original_height'], 
        result['original_width']
    )
    print(image.shape)  # (original_height, original_width)
    
    from PIL import Image
    Image.fromarray(image).show()
    

    【讨论】:

      猜你喜欢
      • 2020-05-13
      • 2013-01-28
      • 2011-11-29
      • 1970-01-01
      • 2019-07-15
      • 1970-01-01
      • 2010-11-11
      • 2023-03-29
      • 1970-01-01
      相关资源
      最近更新 更多