深度学习——语义分割标注转换

核心思想:“将颜色转换成对应的标号”
形式一:Json格式的标注转换成调色板mask
形式二:RGB类型mask(24位三通道)转成调色板mask(8位单通道),调色板的格式为.png
形式三:对于二分类的语义分割,一般采用灰度图(8位单通道)

Json格式的标注转换成调色板mask

首先读取json文件,获取获取图片width和height ,使用构造三个东西:调色板mask,colormap(一维列表),类别列表(含背景),类别索引

import numpy as np
from PIL import Image,ImageDraw
import json 


#一张图片对应一个json文件的情况
def json2mask(json_path,colormap):
    #打开json 文件
    with open(json_path,"r",encoding="utf-8") as f:
        #获取字典
        data=json.load(f)
    #获取所有标注的多边形
    shapes=data["shapes"]

    height=data["imageHeight"]
    width=data["imageWidth"]

   
    #构造mask,大小和原图大小一致
    mask=Image.new("P",(width,height),0)
    mask.putpalette(colormap)

    #获取图像路径
    image_path=data["imagePath"]

    for shape in shapes:
        label_index=int(shape["label"])  #类别索引
        points=shape["points"]  #列表
        points=[ tuple(points[i]) for i in range(len(points))]
        #print(points)
        
        drawObject=ImageDraw.Draw(mask)
        drawObject.polygon(points,fill=label_index)  
    #调色板模式需要用.png存储,所以这里需要对图像的格式进行检查
    if image_path.endswith("jpg") is True:
        image_path=image_path.split(".")[0]+".png"

    mask.save(image_path)

    # mask=np.array(mask)
    # print(mask[1000:1060,10:100])

if __name__=="__main__":
    #classes长度为类别数加一   例如我有猫,狗,树三个种类,加上background后就是四类
    #构造调色板的颜色,颜色种类和classes的种类一致,每三位代表一种颜色例如 0,0,0 为黑色,128,0,0 为红色,0,128,0为绿色,0,0,128为蓝色
    colormap=[0,0,0,128,0,0,0,128,0,0,0,128]
    json_path="./segmentation.json"
    
    json2mask(json_path=json_path,colormap=colormap)

参考:https://blog.csdn.net/qq_37541097/article/details/113247318?spm=1001.2014.3001.5502

RGB类型mask(24位三通道)转成调色板mask(8位单通道)

B站李沐老师的语义分割视频

对于二分类的语义分割,一般采用灰度图(8位单通道)
import numpy as np
from PIL import Image,ImageDraw
import json 



def json2mask(json_path):
    #打开json 文件
    with open(json_path,"r",encoding="utf-8") as f:
        #获取字典
        data=json.load(f)
    #获取所有需要的信息
    images=data["images"]
    annotations=data["annotations"]
   
    for i  in range(len(images)):
        #图像的名称
        image_name=images[i]["file_name"].split("/")[-1]

        image_width=images[i]["width"]
        image_height=images[i]["height"]

        #多边形标注点
        points=annotations[i]["segmentation"][0]

        #[(x1,y1),(x2,y2),........]
        points=[ (points[i],points[i+1]) for i in range(0,len(points),2)]

        #构造mask,大小和原图大小一致,模式设置为RGB,颜色为黑色
        new_image_mask=Image.new("RGB",(image_width,image_height),"black")
        
        # 接下来进行填充
        drawObject=ImageDraw.Draw(new_image_mask)
        drawObject.polygon(points,fill="white") 
        new_image_mask.save(image_name)


if __name__=="__main__":
    json_path="./instances_default.json"
    json2mask(json_path=json_path)
    

https://zhuanlan.zhihu.com/p/22976342
https://www.cnpython.com/qa/70742
https://blog.csdn.net/Return_0_/article/details/97623422

语义分割的图像缩放

image缩放使用插值
mask缩放使用就近插值,即使用周围图像填充

语义分割的评价指标

在这里插入图片描述
参考链接:https://www.bilibili.com/video/BV1ev411P7dR/?spm_id_from=333.999.0.0&vd_source=4e158793050c12a2c18e590ddab400e7

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

千禧皓月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值