几何外壳计算
该部分主要由 GeometricHullCalculator 类提供,用于计算给定点集的凸包和凹包,同时也支持随机点生成和可视化操作。
主要功能:
计算并返回点集的最小边界矩形(MBR)、凸包和凹包。
支持使用 OpenCV 和 matplotlib 分别进行结果的可视化展示。
提供生成随机点集的辅助方法,方便示例测试和验证算法。
主要方法:
generate_random_points(num_points: int = 100, scale: int = 50, offset: int = 150): 生成随机点集,用于测试或示例展示。
get_convex_hull(self): 返回点集的凸包结果。
get_concave_hull(self): 返回点集的凹包结果。
visualize_opencv(self): 使用 OpenCV 显示计算结果。
visualize_matplotlib(self): 使用 matplotlib 绘制与展示计算结果。
示例:
>>> from pywayne.cv.geometric_hull_calculator import GeometricHullCalculator
>>> hull_calculator = GeometricHullCalculator(points=GeometricHullCalculator.generate_random_points())
>>> convex_hull = hull_calculator.get_convex_hull()
>>> concave_hull = hull_calculator.get_concave_hull()
>>> hull_calculator.visualize_matplotlib()
- class pywayne.cv.geometric_hull_calculator.GeometricHullCalculator(points, algorithm='concave-hull', use_filtered_pts=False)
Bases:
objectA class for calculating and visualizing geometric hulls of a set of points.
This class provides methods to compute minimum bounding rectangles, convex hulls, and concave hulls for a given set of 2D points. It also offers visualization options using both OpenCV and Matplotlib.
- points
The input set of 2D points.
- Type:
np.ndarray
- algorithm
The algorithm to use for concave hull calculation (‘concave-hull’ or ‘alphashape’).
- Type:
str
- use_filtered_pts
Whether to use filtered points for hull calculations.
- Type:
bool
- box
The minimum bounding rectangle of the points.
- Type:
np.ndarray
- center
The center point of the input points.
- Type:
np.ndarray
- filter_radius
The radius used for filtering points.
- Type:
float
- concave_hull_result
The resulting concave hull.
- Type:
np.ndarray or list
- convex_hull_points
The convex hull points.
- Type:
np.ndarray
- compute_concave_hull_area()
Compute the area of the concave hull.
- Returns:
The area of the concave hull.
- Return type:
float
- static generate_random_points(num_points=100, scale=50, offset=150)
Generate a set of random 2D points.
- Parameters:
num_points (int) – The number of points to generate.
scale (float) – The scale factor for the random distribution.
offset (float) – The offset to add to all points.
- Returns:
An array of random 2D points.
- Return type:
np.ndarray
- get_concave_hull()
Get the Concave Hull result.
- get_convex_hull()
Get the Convex Hull points.
- get_mbr()
Get the Minimum Bounding Rectangle.
- visualize_matplotlib()
Visualize the geometric shapes using Matplotlib.
- visualize_opencv()
Visualize the geometric shapes using OpenCV.