简体   繁体   中英

Binary mask from two segmentation curves

Two segmentation curves are stored in r2(bottom line in image) and r1(upper line in image) as 1-d numpy arrays. I'm trying to create an binary mask; black everywhere except the region inside those two curves: white. So far, I have tried the following code which works for lines but not for curves based on another stackoverflow answer:

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和seg1

Create a polygon from the points of both curves, and then use that to fill a white area. If we treat the curves as one set of X values, and then two different sets of Y values, we should do the following:

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

See more about draing polygons in matplotlib here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM