jam*_*yce 2 python json opencv image-processing computer-vision
我正在尝试使用 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":""}}}
Run Code Online (Sandbox Code Playgroud)
或者有人知道有什么工具可以帮助创建这样的数据集吗?
谢谢
编辑
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)
Run Code Online (Sandbox Code Playgroud)
还使用了一种幼稚的方法将除了 255 之外的所有东西都变成黑色
img = cv2.imread('mask_pen.jpg')
img[img != 255] = 0
Run Code Online (Sandbox Code Playgroud)
给出这样的结果
这是在 Python/OpenCV 中进行填充多边形和抗锯齿的一种方法。
import cv2
import numpy as np
import skimage.exposure
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 = np.zeros_like(img)
filled = cv2.fillPoly(filled, pts = [area], color =(255,255,255))
blur = cv2.GaussianBlur(filled, (0,0), sigmaX=2, sigmaY=2, borderType = cv2.BORDER_DEFAULT)
result = skimage.exposure.rescale_intensity(blur, in_range=(127.5,255), out_range=(0,255)).astype(np.uint8)
cv2.imwrite("filled_polygon.jpg",filled)
cv2.imwrite("filled_polygon_antialiased.jpg",result)
cv2.imshow("filled", filled)
cv2.imshow("result", result)
cv2.waitKey(0)
Run Code Online (Sandbox Code Playgroud)
填充多边形:
抗锯齿填充多边形: