如何標(biāo)注mask用于圖像分割模型訓(xùn)練(VIA標(biāo)注semantic segmentation數(shù)據(jù)集的mask)

近幾年深度學(xué)習(xí)發(fā)展非常迅猛,深度學(xué)習(xí)用于圖像識(shí)別、分割等方面效果非常好,像mask rcnn這類網(wǎng)絡(luò)已經(jīng)可以做到對(duì)象分割了(instance segmentation)。再不跟進(jìn)就落伍了??!

下圖直觀的區(qū)分了這四種不同處理任務(wù)的效果。Instance segmentation的任務(wù)不單把cube這個(gè)物體找到了,還要分割出不同cube對(duì)象。


在這里插入圖片描述

網(wǎng)上看別人的研究成果都覺得效果很好,實(shí)踐起來到底怎么樣了?

最近嘗試下github上的Image Segmentation Keras

訓(xùn)練前,首先要準(zhǔn)備數(shù)據(jù)集,需要標(biāo)注大量mask圖片,找了幾款標(biāo)注工具,只能導(dǎo)出json文件,下面分享下如何把json文件轉(zhuǎn)化成mask 圖片。

首先,使用VIA標(biāo)注工具,標(biāo)注物體輪廓,導(dǎo)出json文件。json文件里包括了圖片中物體的輪廓坐標(biāo)信息。

標(biāo)注工具比較多,比如有名的像labelme、VIA等,而VIA是網(wǎng)頁版的,用起來比較簡(jiǎn)單方便,而且流暢,無需安裝。

在這里插入圖片描述

導(dǎo)出的json文件長(zhǎng)這樣兒的:

{
  "_via_settings": {...},
  "_via_img_metadata": {
    "1.png19539": {
      "filename": "1.png",
      "size": 19539,
      "regions": [
        {
          "shape_attributes": {
            "name": "polyline",
            "all_points_x": [
              138,
              149,
              265,
              347,
              364,
              367,
              362,
              352,
              257,
              222,
              162,
              151,
              136
            ],
            "all_points_y": [
              246,
              226,
              198,
              208,
              218,
              258,
              468,
              489,
              552,
              560,
              542,
              524,
              248
            ]
          },
          "region_attributes": {}
        }
      ],
      "file_attributes": {}
    },
    "2.png34896": {...},
    "3.png65485": {...}
  }
}    

運(yùn)行下面python代碼,加載剛才的json文件,解析出輪廓坐標(biāo),通過opencv的pointPolygonTest方法,區(qū)分物體內(nèi)還是物體外的像素點(diǎn),附上不同顏色數(shù)值,如該例中,背景像素為0,物體像素為1。保存成圖片。

import os
import json
import numpy as np
import skimage.draw
import cv2

IMAGE_FOLDER = "./train/"
MASK_FOLOER = "./mask/"
PATH_ANNOTATION_JSON = 'box.json'

# 加載VIA導(dǎo)出的json文件
annotations = json.load(open(PATH_ANNOTATION_JSON, 'r'))
imgs = annotations["_via_img_metadata"]

for imgId in imgs:
    filename = imgs[imgId]['filename']
    regions = imgs[imgId]['regions']
    if len(regions) <= 0:
        continue

    # 取出第一個(gè)標(biāo)注的類別,本例只標(biāo)注了一個(gè)物件
    polygons = regions[0]['shape_attributes']

    # 圖片路徑
    image_path = os.path.join(IMAGE_FOLDER, filename)
    # 讀出圖片,目的是獲取到寬高信息
    image = cv2.imread(image_path)  # image = skimage.io.imread(image_path)
    height, width = image.shape[:2]

    # 創(chuàng)建空的mask
    maskImage = np.zeros((height,width), dtype=np.uint8)
    countOfPoints = len(polygons['all_points_x'])
    points = [None] * countOfPoints
    for i in range(countOfPoints):
        x = int(polygons['all_points_x'][i])
        y = int(polygons['all_points_y'][i])
        points[i] = (x, y)

    contours = np.array(points)

    # 遍歷圖片所有坐標(biāo)
    for i in range(width):
        for j in range(height):
            if cv2.pointPolygonTest(contours, (i, j), False) > 0:
                maskImage[j,i] = 1

    savePath = MASK_FOLOER + filename
    # 保存mask
    cv2.imwrite(savePath, maskImage)

生成圖片保存在mask的文件夾里,每張mask png圖片跟原圖名字一樣,方便后面做訓(xùn)練。

正常來說,導(dǎo)出的mask圖片用肉眼看是黑色的,為了看到mask效果,把背景像素設(shè)置成0,物體像素設(shè)置成255,這樣就能看到效果了。下圖是行李箱mask的直觀效果。


在這里插入圖片描述
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容