欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python實現(xiàn)RLE格式與PNG格式互轉(zhuǎn)

 更新時間:2022年08月18日 08:49:34   作者:Livingbody  
在機器視覺領域的深度學習中,很多數(shù)據(jù)集的標注文件使用RLE的格式。但是神經(jīng)網(wǎng)絡的輸入一定是一張圖片,為此必須把RLE格式的文件轉(zhuǎn)變?yōu)閳D像格式。本文將利用Python實現(xiàn)RLE格式與PNG格式互轉(zhuǎn),感興趣的可以了解一下

介紹

在機器視覺領域的深度學習中,每個數(shù)據(jù)集都有一份標注好的數(shù)據(jù)用于訓練神經(jīng)網(wǎng)絡。

為了節(jié)省空間,很多數(shù)據(jù)集的標注文件使用RLE的格式。

但是神經(jīng)網(wǎng)絡的輸入一定是一張圖片,為此必須把RLE格式的文件轉(zhuǎn)變?yōu)閳D像格式。

圖像格式主要又分為 .jpg 和 .png 兩種格式,其中l(wèi)abel數(shù)據(jù)一定不能使用 .jpg,因為它因為壓縮算算法的原因,會造成圖像失真,圖像各個像素的值可能會發(fā)生變化。分割任務的數(shù)據(jù)集的 label 圖像中每一個像素都代表了該像素點所屬的類別,所以這樣的失真是無法接受的。為此只能使用 .png 格式作為label,pascol voc 和 coco 數(shù)據(jù)集正是這樣做的。

1.PNG2RLE

PNG格式轉(zhuǎn)RLE格式

#!---- coding: utf- ---- import numpy as np

def rle_encode(binary_mask):
    '''
    binary_mask: numpy array, 1 - mask, 0 - background
    Returns run length as string formated
    '''
    pixels = binary_mask.flatten()
    pixels = np.concatenate([[0], pixels, [0]])
    runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
    runs[1::2] -= runs[::2]
    return ' '.join(str(x) for x in runs)

2.RLE2PNG

RLE格式轉(zhuǎn)PNG格式

#!--*-- coding: utf- --*--
import numpy as np

def rle_decode(mask_rle, shape):
    '''
    mask_rle: run-length as string formated (start length)
    shape: (height,width) of array to return
    Returns numpy array, 1 - mask, 0 - background
    '''
    s = mask_rle.split()
    starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
    starts -= 1
    ends = starts + lengths
    binary_mask = np.zeros(shape[0] * shape[1], dtype=np.uint8)
    for lo, hi in zip(starts, ends):
        binary_mask[lo:hi] = 1
    return binary_mask.reshape(shape)

3.示例

'''
RLE: Run-Length Encode
'''
from PIL import Image
import numpy as np 

def __main__():
    maskfile = '/path/to/test.png'
    mask = np.array(Image.open(maskfile))
    binary_mask = mask.copy()
    binary_mask[binary_mask <= 127] = 0
    binary_mask[binary_mask > 127] = 1

    # encode
    rle_mask = rle_encode(binary_mask)
    
    # decode
    binary_mask_decode = self.rle_decode(rle_mask, binary_mask.shape[:2])

4.完整代碼如下

'''
RLE: Run-Length Encode
'''
#!--*-- coding: utf- --*--
import numpy as np
from PIL import Image
import matplotlib.pyplot as plt


# M1:
class general_rle(object):
    '''
    ref.: https://www.kaggle.com/stainsby/fast-tested-rle
    '''
    def __init__(self):
        pass


    def rle_encode(self, binary_mask):
        pixels = binary_mask.flatten()
        # We avoid issues with '1' at the start or end (at the corners of
        # the original image) by setting those pixels to '0' explicitly.
        # We do not expect these to be non-zero for an accurate mask,
        # so this should not harm the score.
        pixels[0] = 0
        pixels[-1] = 0
        runs = np.where(pixels[1:] != pixels[:-1])[0] + 2
        runs[1::2] = runs[1::2] - runs[:-1:2]
        return runs


    def rle_to_string(self, runs):
        return ' '.join(str(x) for x in runs)


    def check(self):
        test_mask = np.asarray([[0, 0, 0, 0],
                                [0, 0, 1, 1],
                                [0, 0, 1, 1],
                                [0, 0, 0, 0]])
        assert rle_to_string(rle_encode(test_mask)) == '7 2 11 2'


# M2:
class binary_mask_rle(object):
    '''
    ref.: https://www.kaggle.com/paulorzp/run-length-encode-and-decode
    '''
    def __init__(self):
        pass

    def rle_encode(self, binary_mask):
        '''
        binary_mask: numpy array, 1 - mask, 0 - background
        Returns run length as string formated
        '''
        pixels = binary_mask.flatten()
        pixels = np.concatenate([[0], pixels, [0]])
        runs = np.where(pixels[1:] != pixels[:-1])[0] + 1
        runs[1::2] -= runs[::2]
        return ' '.join(str(x) for x in runs)


    def rle_decode(self, mask_rle, shape):
        '''
        mask_rle: run-length as string formated (start length)
        shape: (height,width) of array to return
        Returns numpy array, 1 - mask, 0 - background
        '''
        s = mask_rle.split()
        starts, lengths = [np.asarray(x, dtype=int) for x in (s[0:][::2], s[1:][::2])]
        starts -= 1
        ends = starts + lengths
        binary_mask = np.zeros(shape[0] * shape[1], dtype=np.uint8)
        for lo, hi in zip(starts, ends):
            binary_mask[lo:hi] = 1
        return binary_mask.reshape(shape)

    def check(self):
        maskfile = '/path/to/test.png'
        mask = np.array(Image.open(maskfile))
        binary_mask = mask.copy()
        binary_mask[binary_mask <= 127] = 0
        binary_mask[binary_mask > 127] = 1

        # encode
        rle_mask = self.rle_encode(binary_mask)

        # decode
        binary_mask2 = self.rle_decode(rle_mask, binary_mask.shape[:2])

到此這篇關于Python實現(xiàn)RLE格式與PNG格式互轉(zhuǎn)的文章就介紹到這了,更多相關Python RLE轉(zhuǎn)PNG內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python實現(xiàn)自動發(fā)送測試報告郵件的示例代碼

    Python實現(xiàn)自動發(fā)送測試報告郵件的示例代碼

    SMTP也就是簡單郵件傳輸協(xié)議,是一種提供可靠且有效電子郵件傳輸?shù)膮f(xié)議,python的smtplib模塊就提供了一種很方便的途徑發(fā)送電子郵件,它對smtp協(xié)議進行了簡單的封裝,下面就來和大家簡單聊聊吧
    2023-07-07
  • Python Selenium截圖功能實現(xiàn)代碼

    Python Selenium截圖功能實現(xiàn)代碼

    這篇文章主要介紹了Python Selenium截圖功能實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-04-04
  • Python全棧之強制轉(zhuǎn)換

    Python全棧之強制轉(zhuǎn)換

    這篇文章主要為大家介紹了Python強制轉(zhuǎn)換,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • python創(chuàng)建堆的方法實例講解

    python創(chuàng)建堆的方法實例講解

    在本篇文章里小編給大家整理的是一篇關于python創(chuàng)建堆的方法實例講解內(nèi)容,有興趣的朋友們可以學習下。
    2021-03-03
  • Tkinter組件Checkbutton的具體使用

    Tkinter組件Checkbutton的具體使用

    Checkbutton組件用于實現(xiàn)確定是否選擇的按鈕,本文主要介紹了Tkinter組件Checkbutton的具體使用,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01
  • python pyecharts 實現(xiàn)一個文件繪制多張圖

    python pyecharts 實現(xiàn)一個文件繪制多張圖

    這篇文章主要介紹了python pyecharts 實現(xiàn)一個文件繪制多張圖,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-05-05
  • Python爬蟲基礎之requestes模塊

    Python爬蟲基礎之requestes模塊

    這篇文章主要介紹了Python爬蟲基礎之requestes模塊,文中有非常詳細的代碼示例,對正在學習python爬蟲的小伙伴們有非常好的幫助,需要的朋友可以參考下
    2021-04-04
  • 利用Python腳本實現(xiàn)傳遞參數(shù)的三種方式分享

    利用Python腳本實現(xiàn)傳遞參數(shù)的三種方式分享

    使用python腳本傳遞參數(shù)在實際工作過程中還是比較常用。這篇文章為大家總結(jié)了三個常用的方式,感興趣的小伙伴可以跟隨小編一起學習一下
    2022-12-12
  • 對Python 字典元素進行刪除的方法

    對Python 字典元素進行刪除的方法

    這篇文章主要介紹了對Python 字典元素進行刪除的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2020-07-07
  • python 運算符 供重載參考

    python 運算符 供重載參考

    二元運算符及其對應的特殊方法
    2009-06-06

最新評論