【问题标题】:How does matplotlib.path.contains_points interact with PolygonSelector?matplotlib.path.contains_points 如何与 PolygonSelector 交互?
【发布时间】:2019-05-25 09:34:19
【问题描述】:

我对 Python 还很陌生,遇到了一个问题……

我正在尝试在图像上绘制感兴趣区域 (ROI) 并计算该 ROI 中的平均像素值。

首先,我尝试了以下操作 - 首先我使用matplotlib.widgets.PolygonSelector 绘制一个多边形,然后使用path.contains_points to create 一个蒙版,然后我可以将其应用于原始图像。

class ROIPolygon(object):
    def __init__(self, ax, row, col):
        self.canvas = ax.figure.canvas
        self.poly = PolygonSelector(ax,
                                    self.onselect,
                                    lineprops = dict(color = 'g', alpha = 1),
                                    markerprops = dict(mec = 'g', mfc = 'g', alpha = 1))
        self.path = None

    def onselect(self, verts):
        path = Path(verts)
        self.canvas.draw_idle()
        self.path = path

def get_mask(drawn_roi, row, col):
    for i in np.arange(row):
        for j in np.arange(col):
             if drawn_roi.path.contains_points([(j,i)]) == [True]:
                 mask[i][j] = 1
    mask_bool = mask.astype(bool)
    mask_bool = ~mask_bool
    return mask_bool

但是,当我在仅包含 4 个彩色像素的小图像上进行测试时,我在绘制 ROI 之前得到了这个图像:

绘制 ROI 后的这个:

我希望掩码数组只返回 4 个 False 值。

[[ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True False False  True  True  True]
 [ True  True  True  True  True False False  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]]

但它返回了 12 个 False 值。同样,如果我要绘制更大的 ROI,我会返回更多的 False 值。

[[ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True False False False False  True  True]
 [ True  True  True  True False False False False  True  True]
 [ True  True  True  True False False False False  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True  True]]

所以我想知道我的方法是否有问题,或者我缺少PolygonSelector 和 path.contains_points 之间的特定交互?

谢谢!

【问题讨论】:

  • 如果您可以发布用于生成图像的数据,那就太好了。最后,当您创建 reproducible example 时,您有更多机会获得好的答案。谢谢!
  • 看起来代码和结果都没有问题。绿色多边形确实覆盖了 12 个点。也许您更愿意检查像素的角(或边缘),而不是它们的中心?这将需要一个新数组,在每个方向上多一个项目。
  • 哦,是的..对不起,我想我对 contains_points 感到困惑。我假设它只在值非零时返回一些东西。

标签: python matplotlib roi


【解决方案1】:

对于像我一样感到困惑的其他人(由于没有清楚地阅读文档),我通过如下更改 get_mask 函数解决了我的问题:

def get_mask(img_frame, drawn_roi, row, col):
    for i in np.arange(row):
        for j in np.arange(col):
             if np.logical_and(drawn_roi.path.contains_points([(j,i)]) == [True], img_frame[i][j] > 0):
                 mask[i][j] = 1
    mask_bool = mask.astype(bool)
    mask_bool = ~mask_bool
    return mask_bool

【讨论】:

    猜你喜欢
    • 2017-04-25
    • 2011-02-15
    • 2015-09-22
    • 2016-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-05
    相关资源
    最近更新 更多