分割掩码的反转像素坐标
Inverted pixel coordinates of segmentation mask
以下是分割蒙版的图像(显示为黄色)。覆盖在该蒙版上的是完全相同的分割蒙版(显示为蓝色)的 pixels/coordinates。
我的问题是:为什么这些 pixels/coordinates 是倒置的、透明的并且在对角线上分裂?为什么不将它们绘制为完整的 "fill",例如面具本身?
我的目标是让这些坐标以 "normal" (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()
在您的示例中,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)
我希望现在看起来有点像预期或接近预期。
以下是分割蒙版的图像(显示为黄色)。覆盖在该蒙版上的是完全相同的分割蒙版(显示为蓝色)的 pixels/coordinates。
我的问题是:为什么这些 pixels/coordinates 是倒置的、透明的并且在对角线上分裂?为什么不将它们绘制为完整的 "fill",例如面具本身?
我的目标是让这些坐标以 "normal" (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()
在您的示例中,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)
我希望现在看起来有点像预期或接近预期。