• 通过labelme的json文件实现对图片的批量裁剪


    通过labelme的json文件实现对图片的批量裁剪

    工具安装

    首先我们需要在conda的终端下载labelme,然后打开labelme对图片进行抠图,将一张图需要抠的指定元素打好标签。具体操作很简单需要进一步了解的可以点击这里

    抠图程序编写

    首先我们将每张图对应的json文件命名与原图一样,以便于后续操作,并将全部json文件统一放到统一的文件夹下面,我这里的文件夹是picturejson
    在这里插入图片描述
    json文件的具体样式为:

    {
      "version": "5.0.1",
      "flags": {},
      "shapes": [
        {
          "label": "yang",
          "points": [
            [
              34.09803921568627,
              200.0
            ],
            [
              79.19607843137254,
              162.7450980392157
            ],
            [
              200.76470588235293,
              166.66666666666666
            ],
            [
              418.4117647058823,
              162.7450980392157
            ],
            [
              447.82352941176475,
              245.09803921568627
            ],
            [
              426.2549019607843,
              327.45098039215685
            ],
            [
              439.98039215686276,
              411.7647058823529
            ],
            [
              706.6470588235294,
              325.4901960784314
            ],
            [
              718.4117647058823,
              1788.235294117647
            ],
            [
              32.13725490196077,
              1796.078431372549
            ]
          ],
          "group_id": null,
          "shape_type": "polygon",
          "flags": {}
        },
        {
          "label": "yang",
          "points": [
            [
              1043.9019607843138,
              168.62745098039215
            ],
            [
              1392.921568627451,
              164.70588235294116
            ],
            [
              1432.1372549019607,
              254.90196078431373
            ],
            [
              1406.6470588235293,
              350.98039215686276
            ],
            [
              1438.0196078431372,
              1780.392156862745
            ],
            [
              841.9411764705882,
              1813.7254901960785
            ],
            [
              743.9019607843137,
              1156.862745098039
            ],
            [
              745.8627450980392,
              890.1960784313725
            ],
            [
              857.6274509803922,
              774.5098039215686
            ],
            [
              994.8823529411764,
              760.7843137254902
            ],
            [
              983.1176470588234,
              200.0
            ]
          ],
          "group_id": null,
          "shape_type": "polygon",
          "flags": {}
        },
        {
          "label": "yang",
          "points": [
            [
              2416.450980392157,
              164.70588235294116
            ],
            [
              2851.7450980392155,
              168.62745098039215
            ],
            [
              2857.627450980392,
              1782.3529411764705
            ],
            [
              2189.0,
              1792.156862745098
            ],
            [
              2153.705882352941,
              905.8823529411765
            ],
            [
              2120.372549019608,
              190.19607843137254
            ]
          ],
          "group_id": null,
          "shape_type": "polygon",
          "flags": {}
        }
      ],
      "imagePath": "00001P.jpg",
      "imageData": null,
      "imageHeight": 1901,
      "imageWidth": 2874
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142

    注意

    我们在抠图的时候不要把图片的imagedata给保存,这个对勾给取消,不然数据乱码,json文件会识别不出来。
    在这里插入图片描述

    然后我们还需要对应的图片的数据集以便于我们可以通过json文件找到指定的图片并对指定的图片进行操作,这里要保证图片可以找到
    在这里插入图片描述

    具体程序编码

    
    
    import os
    import cv2
    import json
    import numpy as np
    
    
    jsonPath = "./picturejson/"
    jpgDirPath = "./dataset/"
    
    
    jsonName = os.listdir(jsonPath)
    
    for json1 in jsonName:
    
        img = cv2.imread(jpgDirPath + json1.split(".")[0] + ".jpg")
    
        path = jsonPath +json1
        print(path)
    
        j = 0
       
        with open(path, 'r', encoding='utf-8') as f:
            load_dict = json.load(f)
            dic_data = load_dict["shapes"]
    
            for i in dic_data:
                pts = np.array(i["points"])
    
                
                pts = pts.astype(np.int64)
    
               
                rect = cv2.boundingRect(pts)
                x, y, w, h = rect
                croped = img[y:y + h, x:x + w].copy()
               
                pts = pts - pts.min(axis=0)
                mask = np.zeros(croped.shape[:2], np.uint8)
                cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
    
               
                dst = cv2.bitwise_and(croped, croped, mask=mask)
                
                bg = np.ones_like(croped, np.uint8) * 255
                cv2.bitwise_not(bg, bg, mask=mask)
                dst2 = bg + dst
    
    
    
                Save_File = "./cutout/"
                Name_File = i['label']
    
                category = load_dict['imagePath']
    
                if "P" in category.split('.')[0] or "p" in category.split('.')[0]:
                    save_path = os.path.join(Save_File,Name_File+'/psave', load_dict['imagePath'].split(".")[0]+"_"+str(j) + '.jpg')
                else:
                    save_path = os.path.join(Save_File, Name_File + '/',load_dict['imagePath'].split(".")[0] + "_" + str(j) + '.jpg')
    
    
                if os.path.exists(Save_File + Name_File):
                    cv2.imwrite(save_path, dst2)
                else:
                    os.makedirs(Save_File + Name_File + "/psave")
                j += 1
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68

    结果

    程序会在当前文件夹下进行创建一个cutout文件夹并且同时会在cutout文件夹下对数据的类别进行分类存储。
    在这里插入图片描述

  • 相关阅读:
    学习笔记 | Excel 2016 零基础教程
    初学者设计PCB,如何检查光绘文件的断头线
    hutool两个list取差集subtractToList
    目标检测常见数据增强算法汇总讲解(Mixup,Cutout,CutMix,Mosaic)
    一个悄然崛起的AI开源项目!
    Java 代理模式
    【情态动词练习题】Can / Could you
    springboot使用的设计模式
    Flutter:Android/iOS集成Flutter模块
    解决electron + react单页应用调用localhost服务失败
  • 原文地址:https://blog.csdn.net/weixin_45522528/article/details/126577715