voc分割數據集 pil調色板


voc分割數據集有兩種,文件夾名字分別是SegmentationClass,SegmentationClassAug,其中SegmentationClass文件夾圖片樣式如下:

SegmentationClassAug文件夾圖片樣式如下:

今天來說下SegmentationClass文件夾帶彩色圖的,讀一個deeplab的pytorch代碼的時候,我就在找是怎么把顏色對應到標簽圖的,找了半天沒有,但是發現最后的處理得到的標簽圖確實是0-21的像素,可是哪里處理了的了.

    def _make_img_gt_point_pair(self, index):
        _img = Image.open(self.images[index]).convert('RGB')
        _target = Image.open(self.categories[index])

        return _img, _target

只有一處是通過PIL讀取圖片,然后我驚奇的發現僅僅是通過這個讀就已經轉成了0-21的標簽圖!!!???why?

    def _make_img_gt_point_pair(self, index):
        _img = Image.open(self.images[index]).convert('RGB')
        _target = Image.open(self.categories[index])

        _img.show()
        _target.show()

        import numpy
        img = numpy.array(_img)
        targe = numpy.array(_target)

用pilshow出來,還是彩色圖

但是我用numpy看targe已經是單通道了...然后我再用opencv顯示,

   def _make_img_gt_point_pair(self, index):
        _img = Image.open(self.images[index]).convert('RGB')
        _target = Image.open(self.categories[index])

        _img.show()
        _target.show()


        import numpy
        img = numpy.array(_img)
        targe = numpy.array(_target)

        import cv2
        cv2.imshow("opencv-show-pil",targe)
        cv2.waitKey()

        mm = cv2.imread(self.categories[index],-1)
        cv2.imshow("opencv-read", mm)
        cv2.waitKey()


這里可以看到,直接讀取路徑確實是彩色圖,但是經過pil打開之后,就變成了標簽圖!神奇吧!

    def _make_img_gt_point_pair(self, index):
        _img = Image.open(self.images[index]).convert('RGB')
        _target = Image.open(self.categories[index])

        import numpy
        img = numpy.array(_img)
        targe = numpy.array(_target)

        import cv2
        cv2.imshow("opencv-show-pil",targe)
        cv2.waitKey()

        mm = cv2.imread(self.categories[index],-1)
        cv2.imshow("opencv-read", mm)
        cv2.waitKey()

        _img.show()
        _target.show()

        print(_img.format, _img.size, _img.mode)
        print(_target.format, _target.size, _target.mode)

打印pil的一些屬性,

None (438, 500) RGB
PNG (438, 500) P

可以發現標簽圖的mode是p. 百度了一下

modes 	描述
1 	1位像素,黑和白,存成8位的像素
L 	8位像素,黑白
P 	8位像素,使用調色板映射到任何其他模式
RGB 	3× 8位像素,真彩
RGBA 	4×8位像素,真彩+透明通道
CMYK 	4×8位像素,顏色隔離
YCbCr 	3×8位像素,彩色視頻格式
I 	32位整型像素
F 	32位浮點型像素

P 8位像素,使用調色板映射到任何其他模式
啥意思,沒看懂. 然后有段解釋:
https://www.zhihu.com/question/263994588
在PIL中,圖像有很多種模式,如'L'模式,'P'模式,還有常見的'RGB'模式。模式'P'為8位彩色圖像,它的每個像素用8個bit表示,其對應的彩色值是按照調色板索引值查詢出來的。
pascal voc的標簽圖像的模式正是'P'模式。
但是它怎么知道我哪個顏色對應哪個類別的呢.
應該是pil這個調色板是內置的,

print(_target.getpalette())

palette = np.array(_target.getpalette(), dtype=np.uint8).reshape((256, 3))
print(palette)

print(_target.getpalette())打印出很長的一串
[0, 0, 0, 128, 0, 0, 0, 128, 0, 128, 128, 0, 0, 0, 128, 128, 0, 128, 0, 128, 128, 128, 128, 128, 64, 0, 0, 192, 0, 0, 64, 128, 0, 192, 128, 0, 64, 0, 128, 192, 0, 128, 64, 128, 128, 192, 128, 128, 0, 64, 0, 128, 64, 0, 0, 192, 0, 128, 192, 0, 0,......]
轉為numpy,
palette = np.array(_target.getpalette(), dtype=np.uint8).reshape((256, 3))
print(palette)
打印如下:
[[ 0 0 0]
[128 0 0]
[ 0 128 0]
[128 128 0]
[ 0 0 128]
[128 0 128]
[ 0 128 128]
[128 128 128]
[ 64 0 0]
[192 0 0]
[ 64 128 0]
[192 128 0]
[ 64 0 128]
[192 0 128]
[ 64 128 128]
[192 128 128]
[ 0 64 0]
[128 64 0]
[ 0 192 0]
[128 192 0]
[ 0 64 128]
[128 64 128]
[ 0 192 128]
[128 192 128]
[ 64 64 0]
[192 64 0]
[ 64 192 0]
[192 192 0]
[ 64 64 128]
...

原來如此!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM