如何在python中用虚线标记给定区域的分割掩码

问题描述 投票:0回答:2

我想通过编码为一些图像制作分割蒙版。有一个工具可以生成大约有边界点坐标的xml文件。可以通过获取标签值来获得这些坐标。图像大小为800x600像素。

例如,从该工具中提取以下几点。如何填充用虚线标记的轮廓内的区域,例如:白色?谢谢。

points = [(737,255),(736,265),(735,270),(739,273),(746,264),(755,268),(765,272),(767,276) ,(769,278),(776,275),(776,264),(767,260),(753,257),(738,256),(738,256),(479,247),( 455,244),(446,245),(440,243),(430,244),(423,241),(419,238),(412,232),(406,225),(403, 202),(404,185),(410,170),(414,166),(424,160),(439,157),(456,166),(469,177),(472,197) ,(480,199),(492,214),(494,207),(488,201),(492,193),(497,194),(502,196),(510,196),( 518,197),(523,202),(521,209)]

import Image
import numpy as np

a = Image.new('RGB',(800,600),0)
for p in points:
  a.putpixel(p,(255,255,255))
a.show()
a.save('./Original_mask.png')

Original_mask:enter image description here

Expected_mask:enter image description here

python xml opencv image-segmentation
2个回答
0
投票

基本上,如果你想填充受散乱点限制的区域,你可以使用cv2.drawContours

import cv2
import numpy as np

points = [
    (737, 255), (736, 265), (735, 270), (739, 273), (746, 264),
    (755, 268), (765, 272), (767, 276), (769, 278), (776, 275), (776, 264),
    (767, 260), (753, 257), (738, 256), (738, 256), (479, 247), (455, 244),
    (446, 245), (440, 243), (430, 244), (423, 241), (419, 238), (412, 232),
    (406, 225), (403, 202), (404, 185), (410, 170), (414, 166), (424, 160),
    (439, 157), (456, 166), (469, 177), (472, 197), (480, 199), (492, 214),
    (494, 207), (488, 201), (492, 193), (497, 194), (502, 196), (510, 196),
    (518, 197), (523, 202), (521, 209)
    ]

canvas = np.zeros((600, 800,3), np.uint8)
pts = np.array([[x,y] for(x,y) in points])
cv2.drawContours(canvas, [pts],-1, (0,255,0), -1)
cv2.polylines(canvas, [pts], isClosed=True, color=(255,0,0), thickness=2)
cv2.imshow("canvas", canvas);cv2.waitKey();cv2.destroyAllWindows()

enter image description here


我认为分数的顺序可能是错误的。


0
投票

在xml文件中有一个名为'object'的标签,所以直接读取点的属性我应该首先找到所有对象,然后绘制每个对象定义的轮廓。

© www.soinside.com 2019 - 2024. All rights reserved.