【发布时间】:2021-05-20 05:45:12
【问题描述】:
我正在尝试使用 json 文件中存在的坐标将带注释的图像转换为二进制掩码图像。
使用 VGG 标注对图像进行标注。
下面是实际图像、Json 数据和我想要的结果。
这是上图的坐标
{"ILSVRC2012_test_00000181.jpg28497":{"filename":"ILSVRC2012_test_00000181.jpg","size":28497,"regions":[{"shape_attributes":{"name":"polygon","all_points_x":[55,63,82,103,116,137,140,153,155,160,160,169,199,211,227,236,242,250,255,265,268,278,282,290,303,315,321,329,326,332,337,336,330,324,321,317,317,319,309,285,275,264,262,236,223,207,196,190,183,176,176,190,196,187,158,145,118,94,76,83,94,111,101,87,88,105,79,55],"all_points_y":[102,95,87,69,62,58,58,64,69,76,79,77,80,81,78,77,79,84,90,104,108,113,114,129,147,168,204,252,264,286,302,305,299,288,295,316,327,340,343,345,338,340,346,348,340,337,323,314,310,308,305,301,329,337,331,316,325,330,319,306,289,282,229,159,128,98,98,104]},"region_attributes":{"name":"not_defined","type":"unknown","image_quality":{"good":true,"frontal":true,"good_illumination":true},"animal":"pigeon"}}],"file_attributes":{"caption":"","public_domain":"no","image_url":""}}}
或者有没有人知道任何有助于创建这样的数据集的工具。
谢谢
编辑
import cv2
import numpy as np
img = cv2.imread("input.jpg")
area = np.array([
[55, 102], [63, 95], [82, 87], [103, 69],[116,62 ], [137,58 ],[140,58 ], [153, 64],
[155,69 ], [160, 76],[160,79 ], [169,77 ],[199, 80], [211,81 ],[227,78 ], [236, 77],
[242,79 ], [250, 84],[255, 90], [265, 104],[268, 108], [278, 113],[282, 114], [290, 129],
[303, 147], [315, 168],[321, 204], [329, 252],[326, 264], [332, 286],[337, 302], [336, 305],
[330, 299], [324, 288],[321,295 ], [317, 316],[317, 327], [319,340],[309, 343], [285, 345],
[275, 338], [264, 340],[262, 346], [236, 348],[223, 340], [207, 337],[196, 323], [190, 314],
[183, 310], [176, 308],[176, 305], [190, 301],[196, 329], [187, 337],[158, 331], [145, 316],
[118, 325], [94, 330],[76, 319], [83, 306],[94, 289], [111, 282],[101, 229], [87, 159],
[88,128 ], [105, 98],[79, 98],[55, 104]
] )
filled = cv2.fillPoly(img, pts = [area], color =(255,255,255))
cv2.imwrite("mask_pen.jpg",filled)
还用一种天真的方法把除了 255 以外的所有东西都变成黑色
img = cv2.imread('mask_pen.jpg')
img[img != 255] = 0
给出类似this的结果
【问题讨论】:
-
您只需从 JSON 数据中提取 x 和 y 坐标列表,然后使用 OpenCV cv2.fillPoly() 在黑色背景图像上绘制白色填充多边形。
-
然而,JSON 数据并没有直接给出所需的维度。必须提取图像并检查其尺寸,以便为黑色背景图像创建所需的尺寸。
-
Python 有 JSON 阅读器。例如:programiz.com/python-programming/json 和 w3schools.com/python/python_json.asp
-
谢谢,我也会查看链接和 cv2.fillPolly()。
-
drawContoura opencv 函数也可以工作
标签: python json opencv image-processing computer-vision