最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

介紹

在機(jī)器視覺(jué)領(lǐng)域的深度學(xué)習(xí)中,每個(gè)數(shù)據(jù)集都有一份標(biāo)注好的數(shù)據(jù)用于訓(xùn)練神經(jīng)網(wǎng)絡(luò)。

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

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

圖像格式主要又分為 .jpg 和 .png 兩種格式,其中l(wèi)abel數(shù)據(jù)一定不能使用 .jpg,因?yàn)樗驗(yàn)閴嚎s算算法的原因,會(huì)造成圖像失真,圖像各個(gè)像素的值可能會(huì)發(fā)生變化。分割任務(wù)的數(shù)據(jù)集的 label 圖像中每一個(gè)像素都代表了該像素點(diǎn)所屬的類(lèi)別,所以這樣的失真是無(wú)法接受的。為此只能使用 .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])

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

相關(guān)文章

  • 基于Python實(shí)現(xiàn)圖像文字識(shí)別OCR工具

    基于Python實(shí)現(xiàn)圖像文字識(shí)別OCR工具

    在工作、生活中常常會(huì)用到,比如票據(jù)、漫畫(huà)、掃描件、照片的文本提取。本文主要介紹了基于PyQt + PaddleOCR實(shí)現(xiàn)的一個(gè)桌面端的OCR工具,用于快速實(shí)現(xiàn)圖片中文本區(qū)域自動(dòng)檢測(cè)+文本自動(dòng)識(shí)別,需要的朋友可以參考一下
    2021-12-12
  • 使用Python實(shí)現(xiàn)一個(gè)優(yōu)雅的異步定時(shí)器

    使用Python實(shí)現(xiàn)一個(gè)優(yōu)雅的異步定時(shí)器

    在 Python 中實(shí)現(xiàn)定時(shí)器功能是一個(gè)常見(jiàn)需求,尤其是在需要周期性執(zhí)行任務(wù)的場(chǎng)景下,本文給大家介紹了基于 asyncio 和 threading 模塊,可擴(kuò)展的異步定時(shí)器實(shí)現(xiàn),需要的朋友可以參考下
    2025-04-04
  • Python基礎(chǔ)之進(jìn)程詳解

    Python基礎(chǔ)之進(jìn)程詳解

    今天帶大家學(xué)習(xí)Python基礎(chǔ)知識(shí),文中對(duì)python進(jìn)程作了詳細(xì)的介紹,對(duì)正在學(xué)習(xí)python基礎(chǔ)的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • Python Pygame實(shí)戰(zhàn)之水果忍者游戲的實(shí)現(xiàn)

    Python Pygame實(shí)戰(zhàn)之水果忍者游戲的實(shí)現(xiàn)

    大家還記得水果忍者這個(gè)游戲嗎?想當(dāng)年,這也是個(gè)風(fēng)靡全國(guó)的游戲,基本每個(gè)人都玩過(guò)。今天小編就用Python中的Pygame庫(kù)復(fù)刻這一經(jīng)典游戲,需要的可以參考一下
    2022-02-02
  • Python中的整除和取模實(shí)例

    Python中的整除和取模實(shí)例

    這篇文章主要介紹了Python中的整除和取模實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Python繪制七段數(shù)碼管字母

    Python繪制七段數(shù)碼管字母

    在現(xiàn)代電子顯示技術(shù)中,七段數(shù)碼管是一種廣泛應(yīng)用的顯示器件,常用于顯示數(shù)字、字母和一些特殊符號(hào),本文將詳細(xì)介紹如何使用Python繪制七段數(shù)碼管顯示字母的過(guò)程,需要的可以參考下
    2024-12-12
  • Django模板中變量的運(yùn)算實(shí)現(xiàn)

    Django模板中變量的運(yùn)算實(shí)現(xiàn)

    這篇文章主要介紹了Django模板中變量的運(yùn)算,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • Python函數(shù)式編程實(shí)例詳解

    Python函數(shù)式編程實(shí)例詳解

    這篇文章主要介紹了Python函數(shù)式編程,結(jié)合實(shí)例形式詳細(xì)分析了Python函數(shù)式編程高階函數(shù)、匿名函數(shù)、閉包及函數(shù)裝飾器等相關(guān)概念、原理與使用技巧,需要的朋友可以參考下
    2020-01-01
  • 利用django創(chuàng)建一個(gè)簡(jiǎn)易的博客網(wǎng)站的示例

    利用django創(chuàng)建一個(gè)簡(jiǎn)易的博客網(wǎng)站的示例

    這篇文章主要介紹了利用django創(chuàng)建一個(gè)簡(jiǎn)易的博客網(wǎng)站的示例,幫助大家更好的學(xué)習(xí)和使用django框架,感興趣的朋友可以了解下
    2020-09-09
  • 基于Python Pygame實(shí)現(xiàn)的畫(huà)餅圖游戲

    基于Python Pygame實(shí)現(xiàn)的畫(huà)餅圖游戲

    這篇文章主要介紹了基于Pygame實(shí)現(xiàn)一個(gè)畫(huà)餅圖游戲,可以根據(jù)鍵盤(pán)上輸入不同的數(shù)字,將圓分割成不同的幾個(gè)部分,每部分用不同的顏色來(lái)實(shí)現(xiàn)。需要的朋友可以參考一下
    2021-12-12

最新評(píng)論

黑水县| 安多县| 罗江县| 金川县| 桦川县| 湄潭县| 任丘市| 柘城县| 旌德县| 光山县| 墨竹工卡县| 无为县| 图们市| 临海市| 开封县| 同心县| 四子王旗| 平舆县| 内江市| 高淳县| 永嘉县| 南通市| 页游| 彩票| 微博| 巫山县| 阳山县| 唐河县| 扎赉特旗| 天津市| 吉首市| 内乡县| 教育| 遂昌县| 铜陵市| 合水县| 南召县| 集贤县| 邹平县| 曲麻莱县| 吴堡县|