语义分割标注

1,先用pixleAnnotationTool标注

https://github.com/abreheret/PixelAnnotationTool/releases下载该工具

解压后打开:

PixelAnnotationTool.exe

 

将下面的保存为1.json。这是我们的配置文件,定义了每个类别的label名称,显示颜色,id等。

{
    "labels": {
        "Build": {
            "categorie": "Build",
            "color": [
                0,
				0,
				255
            ],
            "id": 1,
            "id_categorie": 1,
            "name": "Build"
        },
        "Tree": {
            "categorie": "Tree",
            "color": [
                0,
                255,
                0
            ],
            "id": 2,
            "id_categorie": 2,
            "name": "Tree"
        },
        "grass": {
            "categorie": "grass",
            "color": [
                0,
                255,
                255
            ],
            "id": 3,
            "id_categorie": 3,
            "name": "grass"
        },
        "Soil": {
            "categorie": "Soil",
            "color": [
                255,
                0,
                0
            ],
            "id": 4,
            "id_categorie": 4,
            "name": "Soil"
        },    
        "Road": {
            "categorie": "Road",
            "color": [
                255,
                255,
                0
            ],
            "id": 5,
            "id_categorie": 5,
            "name": "Road"
        },   
        "Water": {
            "categorie": "Water",
            "color": [
                255,
                0,
                255
            ],
            "id": 6,
            "id_categorie": 6,
            "name": "Water"
        },      
    }
}

 

导入上文的1.json文件并导入图片所在的文件夹(导入1.json后, PixelAnnotationTool 的左边栏应该包含1.json中定义了类别和颜色;图片所在文件夹需要是英文路径)

然后,开始标注,标注是交互式标注,自己可以根据标注结果继续调整

执行一次分水岭之后的结果,可以继续调整

大概调整好了以后,保存数据

,。

生成的3个文件分别为xxx_color_mask.png(通过红绿蓝显示的label),xxx_mask.png(自己标注的label),xx_watershed_mask.png(通过分水岭算法生成的label).注意,这些图片有白边,需要处理白边后使用。

 

这个工具标注时很难完全精细,所以需要进一步微调,。微调工具:js-segment-anotator-master。该工具我自己测试时发现我这边只能在Linux系统下正常使用(Linux打开后,测试一下能不能正常显示、标注和export),而且上文通过PixelAnnotation得到的图片无法直接使用js-segmenta-anotator-master标注,需要做一次转换。

https://github.com/kyamagu/js-segment-annotator,下载并解压

 

 

首先,用python处理xx_watershed_mask.png.

import numpy as np
from PIL import Image
annotation = np.array(Image.open('./cr1_watershed_mask.png'))#根据自己的实际路径更改路径名


temp = annotation[:,:,0]
temp[0]=temp[1]
temp[-1] = temp[-2]
temp[:,0]=temp[:,1]
temp[:,-1] = temp[:,-2]
temp= temp-1

Image.fromarray(np.stack([
    np.bitwise_and(temp, 255),
    np.bitwise_and(temp >> 8, 255),
    np.bitwise_and(temp >> 16, 255),
    ], axis=2).astype(np.uint8)).save('encoded.png')

 

将tiff格式文件改为jpg文件。

用常用的图片打开工具,另存为jpg即可。

现在得到两个文件xxx.jpg(原图转为jpg图像),xxx.png(原图的标签转为js-segment-anotator-master可以识别的图片).将文件拷贝到js-segmentation-annotator-master工具指定的文件夹中。

然后修改example.json

指定标签名和图像名:

{
  "labels": [
    "Build",
    "Tree",
    "Grass",
    "Soil",
    "Road",
    "Water"
  ],
  "imageURLs": [
    "data/images/020_RGB.jpg"
  ],
  "annotationURLs": [
    "data/annotations/020_RGB_watershed_mask_encoded.png"
  ]
}

然后打开index.html,即可使用多边形或者涂抹的方式标注数据

标注完成后,点击export保存。保存后的图片,需要用python将图片转化为label标签。

import numpy as np
from PIL import Image

# Decode
encoded = np.array(Image.open('./encoded2.png'))
annotation = np.bitwise_or(np.bitwise_or(
    encoded[:, :, 0].astype(np.uint32),
    encoded[:, :, 1].astype(np.uint32) << 8),
    encoded[:, :, 2].astype(np.uint32) << 16)
Image.fromarray(annotation).save('encoded3.png')

print(np.unique(annotation))

至此,数据标注完成,输出像素级的标签图像。

 

更加精细化的标注需要用photoshop进行像素级的涂抹。

参考:https://blog.csdn.net/qq_32425195/article/details/102469980

 

photoshop涂抹后的数据需要与当前数据合并,假设photoshop导出的数据为1.tif,将1.tif先转为二值图,然后在原来的标签图像中,在1.tif中值为1的地方,替换为想要的标签值。

from PIL import Image
import numpy as np

a = Image.open('1.tif')
#需要查看图片读取是否正确
Image.fromarray(np.array(a)).save('2.tif')

#先把彩色图像转为灰度图
Lim = a.convert('L')

#将灰度图转为二值图
threshold = 80
table=[]
for i in range(256):
    if i < threshold:
        table.append(0)
    else:
        table.append(1)

bim = Lim.point(table,'1')#生成二值图像并保存
bim.save('BinaGrass.png')

#导入两张要合并的图像,BinaGrass.png是二值图像,表示值为1的地方为草。encoded3.png是人工标注的label图
a = Image.open("BinaGrass.png")
a = np.array(a)
b= Image.open("encoded3.png")
b = np.array(b)

#将图像合并并保存
for i in range(np.shape(b)[0]):
    for j in range(np.shape(b)[1]):
        c[i,j]=b[i,j] if a[i,j]==True else 3    #3是grass的类别
Image.fromarray(c).save('encode4.png')


 

 

 

 

 

 

 

 

 

  • 5
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值