【问题标题】:Irregular region masking in pythonpython中的不规则区域掩蔽
【发布时间】:2016-07-28 20:36:51
【问题描述】:

我想掩盖轮廓内的区域。我的一段代码如下:

cnt = np.array(list((y, x) for x in X for y in Y))
mask = np.zeros(np.shape(b_contour_map),np.uint8)
cv2.drawContours(mask,[cnt],0,1)

我根据点坐标的两个向量创建cnt,打印出来的样子是这样的:

[[252 251]
 [252 251]
 [252 251]
 ...,
 [249 251]
 [249 251]
 [252 251]]

而b_contour_map是包含结构轮廓点的图像。 当我显示掩码时,我得到了结构的边界框,但我只需要知道我的结构不规则轮廓内的点(由 cnt 定义)。有没有办法做到这一点?

【问题讨论】:

  • 您想获得与蓝色和红色相似的图像,除了红色区域是由您的轮廓而不是矩形形状吗?
  • 是的,完全正确。与我的同事一起,我们设法通过从轮廓中删除重复点来获得非常相似的结构,但它并没有完全填充。现在我们正在努力对轮廓点进行排序,因为我们怀疑这可能是导致不正确结果的问题。知道怎么做吗? “几乎”工作的解决方案是: cnt_duplicates = zip(Y, X) cnt = list(set(cnt_duplicates)) maskIm = Image.new('L', (b_contour_map.shape[1], b_contour_map.shape[0 ]), 0) ImageDraw.Draw(maskIm).polygon(cnt, outline=1, fill=None) mask = np.array(maskIm)
  • 由于您有一个由沿边缘的点定义的轮廓,您可以遍历该区域并测试每个点是否在轮廓中。

标签: python opencv


【解决方案1】:

事实证明,点的顺序是问题所在。我通过实施得到了正确的结果:

cnt = zip(Y, X)
cnt.sort(key=lambda x: (-x[0], x[[1]]))
maskIm = Image.new('L', (b_contour_map.shape[[1]], b_contour_map.shape[0]), 0)
ImageDraw.Draw(maskIm).polygon(cnt, outline=1, fill=1)
mask = np.array(maskIm) <p>

【讨论】:

    猜你喜欢
    • 2011-02-02
    • 1970-01-01
    • 2014-02-15
    • 1970-01-01
    • 1970-01-01
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多