python - 来自两条分割曲线的二进制掩码
问题描述
两条分割曲线作为一维 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')
解决方案
从两条曲线的点创建一个多边形,然后用它来填充一个白色区域。如果我们将曲线视为一组 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 中排水多边形的更多信息
推荐阅读
- javascript - 使用 jQuery 来操作一个组合的 HTML 页面?
- javascript - For Loop Javascript ScrollTo.Document 项目在重构后不起作用
- javascript - 添加区域后上传编辑器图像
- swift - 在 SwiftUI (iPadOS) 中获取移动指针的坐标
- html - 阻止伪元素扩展窗口的高度
- python - 我需要用多线程的 API 调用结果填充一个列表
- jquery - 将 Jquery 中的提示变量传递给 Flask
- python - dtype 为 str 的 numpy 数组不能与自身求和?
- jquery - jquery 对象在第二个`.forEach()` 中返回未定义
- node.js - 无法实现返回值的承诺 - NodeJS