cv2.fillPoly(): Convert the segmentation target labeled by labelme into a binarized segmentation image in png/jpg format

Table of contents

question

  • Convert the segmentation target labeled by labelme into a binarized segmentation image in png/jpg format
  • The file marked by labelme is a json file, which includes the coordinates of each point
      {
          
          
        "version": "5.0.1",
        "flags": {
          
          },
        "shapes": [
          {
          
          
            "label": "1",
            "points": [
              [
                256.90697674418607,
                175.8139534883721
              ],
              [
                251.32558139534888,
                195.34883720930233
              ],
              [
                243.4186046511628,
                219.06976744186048
              ],
              [
                229.93023255813955,
                248.8372093023256
              ],
              [
                204.34883720930233,
                313.48837209302326
              ],
              [
                128.06976744186048,
                474.8837209302326
              ],
              [
                145.27906976744185,
                476.27906976744185
              ],
              [
                219.2325581395349,
                310.69767441860466
              ],
              [
                264.34883720930236,
                266.51162790697674
              ],
              [
                270.8604651162791,
                266.51162790697674
              ],
              [
                280.6279069767442,
                249.30232558139537
              ],
              [
                277.37209302325584,
                241.86046511627907
              ],
              [
                274.58139534883725,
                215.8139534883721
              ],
              [
                266.2093023255814,
                232.09302325581396
              ],
              [
                270.8604651162791,
                241.86046511627907
              ],
              [
                257.8372093023256,
                260.4651162790698
              ],
              [
                229.46511627906978,
                291.6279069767442
              ],
              [
                255.04651162790697,
                250.2325581395349
              ],
              [
                262.95348837209303,
                238.13953488372093
              ],
              [
                277.8372093023256,
                204.6511627906977
              ],
              [
                278.30232558139534,
                191.16279069767444
              ],
              [
                288.0697674418605,
                176.27906976744185
              ],
              [
                280.6279069767442,
                171.62790697674419
              ],
              [
                268.5348837209302,
                188.8372093023256
              ],
              [
                268.06976744186045,
                207.4418604651163
              ],
              [
                249.46511627906978,
                244.18604651162792
              ],
              [
                216.4418604651163,
                300.93023255813955
              ],
              [
                239.2325581395349,
                244.6511627906977
              ],
              [
                258.30232558139534,
                198.13953488372093
              ],
              [
                256.90697674418607,
                191.62790697674419
              ],
              [
                263.8837209302326,
                174.4186046511628
              ]
            ],
            "group_id": null,
            "shape_type": "polygon",
            "flags": {
          
          }
          }
        ],
        "imagePath": "../pics/123.jpg",
        "imageData": "....",
        "imageHeight": 480,
        "imageWidth": 640
      }```
    
    
    

method

  • Use the function in opencv-python: cv2.fillPoly()

    def fillPoly(img: Mat, pts, color, lineType=..., shift=..., offset=...) -> typing.Any:
    

application

  • Use Python, and the opencv-python library

     import json  
     import numpy as np
     import cv2
     
     ## 获取分割点
     contours = []
     with open('a.json', 'r') as f_r:
         result = json.load(f_r)
         for shape in result['shapes']:
             ## 注意:数值的类型为 np.int32
             points = np.array(shape['points'], dtype=np.int32)
             ## 注意:分割点的格式
             points = points.reshape(len(points), 1, 2)
             contours.append(points)
             
     ## 转换为二值图像 
     img = np.zeros((480,640,3))
     for cnt in contours:
         fgmask = cv2.fillPoly(img, [cnt], color=(255, 255, 255))
     cv2.imwrite('a.jpg', fgmask) 
    
  • Converted binary image

Guess you like

Origin blog.csdn.net/qq_40541102/article/details/129820711