首页 > 其他分享 >如何在 Pascal Voc 语义分割任务中为标签图建立灰度图索引

如何在 Pascal Voc 语义分割任务中为标签图建立灰度图索引

时间:2023-02-15 10:33:05浏览次数:58  
标签:index img Voc Pascal 灰度 new np import pixel

上图是voc语义分割的图片,下图是来自陈洪翰大佬文章中的索引表。

直接放代码:

import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image

# 建立索引
pixel_index = np.zeros((4,4,4))
pixel_index[0,0,0]=0
pixel_index[2,0,0]=1
pixel_index[0,2,0]=2
pixel_index[2,2,0]=3
pixel_index[0,0,2]=4
pixel_index[2,0,2]=5
pixel_index[0,2,2]=6
pixel_index[2,2,2]=7
pixel_index[1,0,0]=8
pixel_index[3,0,0]=9
pixel_index[1,2,0]=10
pixel_index[3,2,0]=11
pixel_index[1,0,2]=12
pixel_index[3,0,2]=13
pixel_index[1,2,2]=14
pixel_index[3,2,2]=15
pixel_index[0,1,0]=16
pixel_index[2,1,0]=17
pixel_index[0,3,0]=18
pixel_index[2,3,0]=19
pixel_index[0,1,2]=20
pixel_index[3,3,3]=21
pixel_index.astype(np.int32)

img_list = os.listdir('./VOC2012/SegmentationClass')
for img_name in img_list:
    img = cv2.imread('./VOC2012/SegmentationClass/'+img_name)
    # img = Image.open('./VOC2012/SegmentationClass/'+img_name)
    img = np.array(img)
    img = img/64  
    img = img[:,:,::-1]
    img = img.astype(np.int32)
    new_img = np.zeros(img.shape[:2])
    for x in range(img.shape[0]):
        for y in range(0,img.shape[1]):
            r,g,b = img[x][y]
            new_img[x][y] = pixel_index[r,g,b]
    new_img = new_img.astype(np.uint8)
    cv2.imwrite('/home/aistudio/work/datas/VOCdevkit/VOC2012/Segmen/'+img_name,new_img)

标签:index,img,Voc,Pascal,灰度,new,np,import,pixel
From: https://www.cnblogs.com/wensi-eric/p/17121889.html

相关文章