我有一些代码可以打印出坐标列表(存储在点中)

f=open('139cm_2000_frame27.json') 
data=json.load(f) 
shapes=data["shapes"] 
for i in shapes: 
    print(i['label'])   # prints the label first
    for c in i['points']:
        d=np.array(c)
        print(d)       # an array containing coordinates in the form (x,y) 

d、 坐标是n个10边多边形的点.坐标0-9是第一个多边形的坐标,坐标10-19是第二个多边形...

json文件中可以有任意数量的多边形,但每个多边形始终有10个坐标.

我需要找到一种方法,使用这些坐标在128x128数组中"绘制"或"重建"这些多边形.

我试过了

from skimage.draw import polygon
   img = np.zeros((128, 128), dtype=np.uint8)
   r = np.array([#the x coordinates of d])
   c = np.array([#the y coordinates of d])
   rr, cc = polygon(r, c)
   img[rr, cc] = 1 #unsure about the 1
   img

但我不知道如何1)获得10个坐标集,2)将xs读入r,将ys读入c

非常感谢!

输入json的一个示例:

{
  "version": "4.6.0",
  "flags": {},
  "shapes": [
    {
      "label": "blob",
      "points": [
        [
          61.42857142857143,
          20.285714285714285
        ],
        [
          59.10047478151446,
          18.879430437885873
        ],
        [
          58.04359793578868,
          16.37330203102605
        ],
        [
          58.661631924538575,
          13.724584936383643
        ],
        [
          60.71850877026435,
          11.94499905752918
        ],
        [
          63.42857142857143,
          11.714285714285715
        ],
        [
          65.75666807562841,
          13.120569562114127
        ],
        [
          66.81354492135418,
          15.62669796897395
        ],
        [
          66.19551093260428,
          18.275415063616357
        ],
        [
          64.13863408687851,
          20.05500094247082
        ]
      ],
      "group_id": null,
      "shape_type": "polygon",
      "flags": {}
    },
    {
      "label": "blob",
      "points": [
        [
          88.71428571428572,
          82.42857142857143
        ],
        [
          85.63470409582908,
          81.33512050565437
......

推荐答案

从软件工程的Angular 来看,建议将代码分解为简单的独立部分(即模块化).

First you will need a function for reading the input json and parsing it. I called it read_input in the code below. The format of the parsed data depends on the application.
I chose to return a list of pairs of ndarrays. Each element in the list represents one polygon. Each polygon contains 2 ndarrays: 1 for the x coordinates, and 1 for the y coordinates. I chose this representation because it is convenient for drawing the polygons (see below).

其次,你需要一个函数来绘制多边形(draw_polygons).它将包含对多边形列表的迭代,并为绘制1个多边形(draw_one_polygon)调用较低级别的函数,同样是出于模块化的原因.

请参见下面的代码:

import json
import numpy as np
from skimage.draw import polygon

def read_input(filename: str):
    polygons = []
    f = open(filename)
    data = json.load(f)
    shapes = data["shapes"]
    for i in shapes:
        cur_poly_points = i["points"]
        tmp = list(zip(*cur_poly_points))
        # NOTE: The following line assumes that the point coordinates are given as (x,y). 
        #       Change the order of the indices if needed.
        polygons.append((np.array(tmp[1]), np.array(tmp[0])))
    return polygons

def draw_one_polygon(img, one_poly):
    r = one_poly[0];
    c = one_poly[1];
    rr, cc = polygon(r, c)
    img[rr,cc] = 1

def draw_polygons(img, polygons):
    for poly in polygons:
        draw_one_polygon(img, poly)

filename = '139cm_2000_frame27.json'
polygons = read_input(filename)
img = np.zeros((128, 128), dtype=np.uint8)
draw_polygons(img, polygons)
print(img)

Note:在实际代码中,您应该验证坐标没有超过图像尺寸.

文档和示例:skimage.draw.polygon

如果您不熟悉这个符号:*cur_poly_points,请参见此处:How to unzip a list of tuples into individual lists?.

Python相关问答推荐

Pandas 第二小值有条件

如果条件为真,则Groupby.mean()

使用新的类型语法正确注释ParamSecdecorator (3.12)

将特定列信息移动到当前行下的新行

Python库:可选地支持numpy类型,而不依赖于numpy

将pandas Dataframe转换为3D numpy矩阵

如果条件不满足,我如何获得掩码的第一个索引并获得None?

Pandas:将多级列名改为一级

我对我应该做什么以及我如何做感到困惑'

在ubuntu上安装dlib时出错

从嵌套的yaml创建一个嵌套字符串,后面跟着点

Python 3试图访问在线程调用中实例化的类的对象

如何使用Azure Function将xlsb转换为xlsx?

Python日志(log)库如何有效地获取lineno和funcName?

Scipy差分进化:如何传递矩阵作为参数进行优化?

如何写一个polars birame到DuckDB

对于标准的原始类型注释,从键入`和`从www.example.com `?

根据过滤后的牛郎星图表中的数据计算新系列

Django查询集-排除True值

将多行数据循环到嵌套框架中的单行