【问题标题】:Inverted pixel coordinates of segmentation mask分割掩码的倒置像素坐标
【发布时间】:2020-01-14 22:17:11
【问题描述】:

以下是分割掩码的图像(显示为黄色)。覆盖在此蒙版上的是同一分割蒙版的像素/坐标(显示为蓝色)。

我的问题是:为什么这些像素/坐标在对角线上被反转、透明和分割?为什么不将它们绘制为完整的“填充”,例如遮罩本身?

我的目标是让这些坐标以“正常”(x,y) 线性顺序出现。代码:

from matplotlib import patches
import numpy as np

# create mask
mask = np.zeros((350, 525), dtype=np.uint8)

# populate region of mask
mask[2:222,42:521] = 1

# get coordinates of populated region
y, x = np.where(mask == 1)
pts = np.column_stack([x, y])

# define figure, axes, title
fig = plt.figure()
ax = fig.add_axes([0,0,1,1])
ax.set_title('Segmentation mask pixel coordinates')

# show mask
plt.imshow(mask, interpolation='none')

# add mask points
poly = patches.Polygon(pts)
ax.add_patch(poly)
plt.show()

【问题讨论】:

标签: python-3.x numpy opencv matplotlib image-segmentation


【解决方案1】:

在您的示例中,len(pts) 给出了105380,因为pts 包含基于行顺序的掩码的所有点。所以poly 有一个蛇形形状,长度=105380,宽度=1。蛇从左上角开始,到右下角结束——这就是你有对角线的原因。

要更正情节,您可以进行以下修改:

# borders
(x1, y1), (x2, y2) = pts.min(axis=0), pts.max(axis=0)
# corners
pts_for_poly = list(zip((x1, x2, x2, x1), (y1, y1, y2, y2)))
# rectangle polygon
poly = patches.Polygon(pts_for_poly)

我希望它现在看起来有点像预期或接近那样。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-14
    • 2022-12-30
    • 2023-04-10
    • 2017-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2017-12-24
    相关资源
    最近更新 更多