Convert between Mask image and json file

When doing machine learning to process images, it is necessary to label the image, one of which is to mark certain areas in the image, generate a Mask image or record the coordinates of the contour points of these areas. Usually, labeling directly generates one of the files, that is, only json files or only Mask images are generated. Therefore, the Python code for the mutual conversion between the Mask image and the json file is posted here.


mask_to_json

import cv2
import os
import json
import sys


def func(file:str) -> dict:
    png = cv2.imread(file)
    gray = cv2.cvtColor(png, cv2.COLOR_BGR2GRAY)
    _, binary = cv2.threshold(gray,10,255,cv2.THRESH_BINARY)
    contours, _ = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    dic = {"version": "5.0.1", "flags": {},"shapes":list(), "imagePath":os.path.basename(file),
            "imageHeight":png.shape[0], "imageWidth":png.shape[1]}
    for contour in contours:
        temp = list()
        for point in contour[2:]:
            if len(temp) > 1 and temp[-2][0] * temp[-2][1] * int(point[0][0]) * int(point[0][1]) != 0 and (int(point[0][0]) - temp[-2][0]) * (
                            temp[-1][1] - temp[-2][1]) == (int(point[0][1]) - temp[-2][1]) * (temp[-1][0] - temp[-1][0]):
                temp[-1][0] = int(point[0][0])
                temp[-1][1] = int(point[0][1])
            else:
                temp.append([int(point[0][0]), int(point[0][1])])
        dic["shapes"].append({"label": "result", "points":temp, "group_id": None,
                                "shape_type": "polygon", "flags": {}})

    return dic


if  __name__ == "__main__":

    if len(sys.argv) != 3:
        raise ValueError("mask文件或目录 输出路径")

    if os.path.isdir(sys.argv[1]):
        for file in os.listdir(sys.argv[1]):
            with open(os.path.join(sys.argv[2], os.path.splitext(file)[0]+".json"), mode='w', encoding="utf-8") as f:
                json.dump(func(os.path.join(sys.argv[1], file)), f)
    else:
        with open(os.path.join(sys.argv[2], os.path.splitext(os.path.basename(sys.argv[1]))[0]+".json"), mode='w', encoding="utf-8") as f:
            json.dump(func(sys.argv[1]), f)

json_to_mask

import cv2
import json
import numpy as np
import os
import sys


def func(file:str) -> np.ndarray:
    with open(file, mode='r', encoding="utf-8") as f:
        configs = json.load(f)
    shapes = configs["shapes"]

    png = np.zeros((configs["imageHeight"], configs["imageWidth"], 3), np.uint8)

    for shape in shapes:
        cv2.fillPoly(png, [np.array(shape["points"], np.int32)], (0,0,255))

    return png


if  __name__ == "__main__":

    if len(sys.argv) != 3:
        raise ValueError("json文件或目录 输出路径")

    if os.path.isdir(sys.argv[1]):
        for file in os.listdir(sys.argv[1]):
            cv2.imwrite(os.path.join(sys.argv[2], os.path.splitext(file)[0]+".png" ), func(os.path.join(sys.argv[1], file)))
    else:
        cv2.imwrite(os.path.join(sys.argv[2], os.path.splitext(os.path.basename(sys.argv[1]))[0]+".png"), func(sys.argv[1]))

How to use: Enter on the command line

python json_to_mask.py folder or json file output folder

python mask_to_json.py folder or mask image output folder

If you enter a folder, all json files or mask images under the folder will be converted into corresponding files and output to the specified path.

Guess you like

Origin blog.csdn.net/qq_34343690/article/details/128411332