将图像分割数据集中的mask文件夹下保存的目标区域重新转为json格式

一、前言

        有些时候直接拿网上现有的分割数据集来训练自己的网络会报错,但是自己用labelme等软件标注的不会报错。 如果重新会生成一个json文件,里面有目标区域的坐标信息,再来转换数据就不会报错。

        通过借助cv2.findContours()函数来获取轮廓的坐标。为了方便查看,先将这些坐标以及图片名、宽和高都保存在result.csv文件中,再读取csv文件转存为json文件。

 

二、转换

 

        image下的图片

         mask下的图片将图像分割数据集中的mask文件夹下保存的目标区域重新转为json格式_第1张图片

       生成的 json下的文件 

将图像分割数据集中的mask文件夹下保存的目标区域重新转为json格式_第2张图片

         

代码

        Get_polygon_coordinates_save_to_csv.py代码(注意修改mask和result.csv地址以及图片后缀名)

import cv2
import pandas as pd
import os


def get_coor(img):
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # 变为灰度图
    ret, binary = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)  ## 阈值分割得到二值化图片
    # cv2.namedWindow('binary', cv2.WINDOW_AUTOSIZE)
    # cv2.imshow('binary', binary)
    # cv2.waitKey(0)
    contours, heriachy = cv2.findContours(binary, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    print(len(contours))
    for i, contour in enumerate(contours):
        if len(contour) < 20: 
            continue
        num = len(contour[:, 0, 0])  # 个数
        if num > 500:
            hundred = num // 100  # 步长
            tem = contour[:, 0][::hundred]
            return tem, 1
        if 1 <= num <= 10:
            print('num<10')
            return contour[:, 0], 0
        else:
            return contour[:, 0], 1

name_l = []
w_list = []
h_list = []
contours_list = []

file_dir = r'D:\YQ\Thyroid_class_seg\DDTI\1_or_data\mask'
for name in os.listdir(file_dir):
    print(name)
    img_path = os.path.join(file_dir, name)
    img = cv2.imread(img_path)
    h, w = img.shape[:-1]
    contours, flag = get_coor(img)
    if flag == 0:
        continue
    name = name.replace('png', 'jpg')
    name_l.append(name)
    w_list.append(w)
    h_list.append(h)
    contours_list.append(contours)
res_pd = pd.DataFrame()
res_pd['imagePath'] = name_l
res_pd['imageWidth'] = w_list
res_pd['imageHeight'] = h_list
res_pd['points'] = contours_list
res_pd.to_csv(r'D:\YQ\Thyroid_class_seg\DDTI\1_or_data\result.csv', index= False)

        read_csv_Coordinates_save_to_json.py

        (注意修改相应的地址以及dict["shapes"].append中label对应的值的内容)

import json
import pandas as pd
import os
import shutil

def get_coor(sppoint):
    tem_list = []
    for i in range(len(sppoint)):
        sp = sppoint[i].replace('[', '')
        sp1 = sp.replace(']', '')
        sp2 = sp1.strip()  
        num = sp2.count(' ')
        strs = ''
        for n in range(num):
            strs = strs + ' '
            sp3 = sp2.replace(strs, ',')  # sppoint[i][-6:-5]
        x = int(sp3.split(',')[0])
        y = int(sp3.split(',')[-1])
        coor = [x, y]
        tem_list.append(coor)
    return tem_list

def process_text_to_json(name, h, w, coor):
    dict = {}
    dict["version"] = "5.0.1"
    dict["flags"] = {}
    dict["shapes"] = []
    dict["shapes"].append({"label":'nodule', "points":coor, "group_id":"null", "shape_type":"polygon", "flags":{}})
    dict["imagePath"] = name
    dict["imageData"] = "none"
    dict["imageHeight"] = h
    dict["imageWidth"] = w
    # location_data = {"location_data": location_data}
    return json.dumps(dict, indent=4)

coor_df = pd.read_csv(r'D:\YQ\Thyroid_class_seg\DDTI\1_or_data\result.csv')
image_dir = r'D:\YQ\Thyroid_class_seg\DDTI\1_or_data\image'

imagePath_l = []
imageWidth_l = []
imageHeight_l = []
coordinate = []

print(coor_df['points'])

for i, imagePath in enumerate(coor_df['imagePath']):
    imagePath_l.append(imagePath)

for i, imageWidth in enumerate(coor_df['imageWidth']):
    imageWidth_l.append(imageWidth)

for i, imageHeight in enumerate(coor_df['imageHeight']):
    imageHeight_l.append(imageHeight)

for i, point in enumerate(coor_df['points']):
    img_path = os.path.join(image_dir, imagePath_l[i])
    # shutil.copy(img_path, r'D:\YQ\Thyroid_class_seg\segmentation_single\json')

    print(imagePath_l[i])
    coor = []
    sppoint = point.split('\n ')
    coor = get_coor(sppoint)
    x = process_text_to_json(imagePath_l[i],imageHeight_l[i],imageWidth_l[i], coor)
    # print(x)
    # 保存本地json文件
    fileObject = open(r'D:\YQ\Thyroid_class_seg\DDTI\1_or_data\json\{}.json'.format(imagePath_l[i].split('.')[0]), 'w')
    fileObject.write(x)
    fileObject.close()

    # print(coor)
    # coordinate.append(coor)
    # if i == 1:
        # break

# print(len(imagePath_l))
# print(len(imageWidth_l))
# print(len(imageHeight_l))
# print(len(coordinate))




你可能感兴趣的:(json,python,经验分享,opencv)