Python 相当于 Matlab 的 createMask() 函数

问题描述 投票:0回答:1

我想创建一个相当于 Matlab 代码的 Python 代码,为我创建一个过滤掩码。在 Matlab 版本中,我有一个变量“位置”,用于维护我从中创建蒙版的多边形的 xy 值。这是Matlab版本: ...
xlim([0 50]) ylim([0 1000]) h=gca; 位置 = [0 0; ... 2.5 0; ... 10.01 75; ... 10.01 125; ... 0 25]; pol= imppoly(h, 位置); 掩码= createMask(pol);

我在 Python 中没有找到等效的“createMask”函数,我希望蒙版能够模仿 Matlab 的函数。

谢谢

python matlab filtering masking
1个回答
0
投票

您可以使用

Path
matplotlib
类。 它有一个名为
contains_points
的方法来给你一个面具。

这是一个最小的工作示例:

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.patches import Polygon
from matplotlib.path import Path

# Define your polygon's vertices
vertices = np.array([[0, 0], [2.5, 0], [10.01, 75], [10.01, 125], [0, 25])

# Create the polygon
polygon = Polygon(vertices, closed=True, fill=None, edgecolor='none', facecolor='none')

# Generate the masking filter
x, y = np.meshgrid(np.arange(0, 50), np.arange(0, 1000))
points = np.column_stack((x.ravel(), y.ravel()))
path = Path(vertices)
mask = path.contains_points(points).reshape(x.shape)

# plot...
© www.soinside.com 2019 - 2024. All rights reserved.