Mahotas 简明教程

Mahotas - Polygon Utilities

多边形实用程序是一组旨在处理多边形的工具。多边形是指由一系列相连顶点定义的封闭形状。这些实用程序提供了执行与多边形相关的各种任务的功能,例如操作、面积计算、点多边形测试等。

在内部,Mahotas 中的多边形实用程序利用计算几何中的算法和技术来实现其功能。这些底层算法确保了多边形操作和分析的准确性和效率。

总体而言,Mahotas 中的多边形实用程序可以作为处理图像处理任务中的多边形的工具箱,提供基本的函数和算法,以有效地操作、分析和执行这些形状上的操作。

Polygon Utilities Functions in Mahotas

以下是在 mahotas 中提供的不同多边形实用程序函数−

S.No

Function & Description

1

convexhull() 此函数创建最小的形状,该形状包围一组点,同时保持其凸性(凸包)。

2

fill_convexhull() 此函数填充一系列点生成的凸壳的内部。

3

fill_polygon() 此函数填充由一系列点定义的多边形的内部。

4

line() 此函数在两个指定点之间绘制直线。

现在,让我们简要了解这四个函数并查看它们的示例。

The convexhull() function

convexhull() 函数在图像中生成一系列点的凸壳。凸壳是最小程度包含给定点的多边形。此函数通过确定多边形的顶点并确保所有内角小于或等于 180 度来生成凸壳。

Syntax

以下是 mahotas 中 convexhull() 函数的基本语法 −

mahotas.polygon.convexhull(bwimg)

其中,

  1. bwimg − 它是输入二值图像。

Example

在下面的示例中,我们使用 convexhull() 函数获取图像的轮廓。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image = np.zeros((10, 10), bool)
image[2, 5] = 1
image[3, 6] = 1
image[4, 9] = 1
# Computing convex hull
convex_hull = mh.polygon.convexhull(image)
# Printing the value of the hull
print(convex_hull)
# Displaying the convex hull
mtplt.imshow(convex_hull)
mtplt.title('Convex Hull')
# Showing the figures
mtplt.show()

以下是上面代码的输出: -

[[2 5]
 [3 6]
 [4 9]]

获得的图像如下所示:

polygon utilities

The fill_convexhull() function

fill_convexhull() 函数用一系列点在图像中生成的色彩填充凸壳的内部。此函数确保凸壳内的所有像素都分配了特定色彩。

Syntax

以下是 mahotas 中 fill_convexhull() 函数的基本语法 −

mahotas.polygon.fill_convexhull(bwimg)

其中,

  1. bwimg − 它是输入二值图像。

Example

在此处,我们使用 fill_convexhull() 函数填充轮廓的色彩。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image = np.zeros((10, 10), bool)
image[2, 5] = 1
image[3, 6] = 1
image[4, 9] = 1
# Filling convex hull
fill_convexhull = mh.polygon.fill_convexhull(image)
# Displaying the filled convex hull
mtplt.imshow(fill_convexhull)
mtplt.title('Filled Convex Hull')
# Showing the figures
mtplt.show()

执行上面的代码后,我们得到以下输出: -

polygon utilities1

The fill_polygon() function

类似地,fill_polygon() 函数用于填充图像中多边形的内部,使用特定色彩。它获取表示多边形形状的点,并在多边形边界内执行填充操作。

Syntax

以下是 mahotas 中 fill_polygon() 函数的基本语法 −

mahotas.polygon.fill_polygon([(y0, x0), (y1, x1), ..., ], canvas, color=1)

其中,

  1. [(y0, x0), (y1, x1), …​, ] − 这些是 (y, x) 坐标的列表,定义多边形的形状。

  2. canvas − 这是将填充多边形的图像。

  3. color (optional) − 它确定多边形内像素的色彩(默认值为 1)。

Example

在此示例中,我们使用 fill_polygon() 函数填充多边形的色彩。

import numpy as np
import matplotlib.pyplot as mtplt
import mahotas.polygon as mp
# Create a 100x100 image with all zeros
canvas = np.zeros((100, 100), dtype=np.uint8)
# Define the polygon as a list of (y, x) points
polygon = [(0, 0), (50, 0), (50, 50), (0, 50)]
# Fill the polygon in the canvas.
mp.fill_polygon(polygon, canvas, color=255)
# Display the image
plt.imshow(canvas, cmap='gray')
plt.show()

我们获得以下图像作为输出 −

polygon utilities2

The line() function

line() 函数用于在图像中两个指定点(开始和结束)之间绘制直线。这些点由其 x 和 y 坐标表示。x0 和 y0 是开始点的坐标,x1 和 y1 是结束点的坐标。

Syntax

以下是 mahotas 中 line() 函数的基本语法 −

mahotas.polygon.line((y0, x0), (y1, x1), canvas, color=1)

其中,

  1. (y0, x0) - 指定直线起始坐标的元组。

  2. (y1, x1) - 指定直线终点坐标的元组。

  3. canvas - 直线将填充在该图像中。

  4. color (optional) − 它确定多边形内像素的色彩(默认值为 1)。

Example

在以下示例中,我们使用 line() 函数绘制彩色直线。

import numpy as np
import matplotlib.pyplot as mtplt
import mahotas.polygon as mp
# Create a 100x100 image with all zeros
canvas = np.zeros((100, 100), dtype=np.uint8)
# Draw a line in the canvas
mp.line((0, 0), (99, 99), canvas, color=255)
# Display the image
mtplt.imshow(canvas)
mtplt.show()

以下是上面代码的输出: -

polygon utilities3