做图像分割或者图像检测时,需要标注图像,使用labelme进行标注的时候,做图像分割时需要生成mask图像,做图像检测时需要这些标注区域的轮廓点坐标。通常,标注直接生成其中一种文件,即只生成json文件或只生成Mask图像。故在此贴出mask图像与json文件相互转换的Python代码。
labelme的json转mask图
import cv2
import json
import numpy as np
import os
def func(file):
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:
contours = [[point] for point in shape["points"]]
if contours is not None and len(contours) > 0:
cv2.drawContours(png, [np.array(contours, np.int32)], -1,(255, 255, 255),-1)
return png
if __name__ == "__main__":
save_mask_path = r'' #保存mask图路径
json_path = r'' #读取json路径
if os.path.isdir(json_path):
for file in os.listdir(json_path):
cv2.imwrite(os.path.join(save_mask_path, os.path.splitext(file)[0] + ".png"),
func(os.path.join(json_path, file)))
mask图转json
import cv2
import os
import json
def func(file):
png = cv2.imread(file)
gray = cv2.cvtColor(png, cv2.COLOR_BGR2GRAY)
contours, _ = cv2.findContours(gray, 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:]:
temp.append([int(point[0][0]), int(point[0][1])])
dic["shapes"].append({"label": "cell", "points": temp, "group_id": None,
"shape_type": "polygon", "flags": {}})
return dic
if __name__ == "__main__":
mask_path = r' ' #mask图的路径
save_json_path = r'' #保存json路径:
for file in os.listdir(mask_path):
with open(os.path.join(save_json_path, os.path.splitext(file)[0] + ".json"), mode='w', encoding="utf-8") as f:
json.dump(func(os.path.join(mask_path, file)), f)