来自两条分割曲线的二值蒙版

时间:2019-03-08 13:03:38

标签: python

两个分割曲线以1-d numpy数组形式存储在r2(图像中的底线)和r1(图像中的上线)中。我正在尝试创建一个二进制掩码。除两条曲线内的区域外,其他所有地方均为黑色:白色。到目前为止,我已经尝试了以下代码,该代码适用于直线,但不适用于基于另一个stackoverflow答案的曲线:

def line_func(col, s, e):
    return (s + (e - s) * col / im.shape[1]).astype(np.int)

r1, r2 = [20, 25], [30, 35]
rows, cols = np.indices(im.shape)
m1 = np.logical_and(rows > line_func(cols, *r1),
                    rows < line_func(cols, *r2))
im+= 255 * (m1)
plt.imshow(im, cmap='gray')

Seg2 and seg1

1 个答案:

答案 0 :(得分:0)

从两条曲线的点创建一个多边形,然后使用该多边形填充白色区域。如果将曲线视为一组X值,然后视为两组不同的Y值,则应执行以下操作:

from matplotlib.patches import Polygon

X = ...
Y1, Y2 = ...
points = list(zip(X, Y1)) + list(reversed(zip(X, Y2)))
polygon = Polygon(points)

# Now fill the polygon with one color, and everything else with a different color

查看有关matplotlib here中的排水多边形的更多信息