Mahotas 简明教程

Mahotas - Polygon Utilities

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

Polygon utilities are a collection of tools designed to handle polygons. A polygon refers to a closed shape defined by a sequence of connected vertices. These utilities offer functionalities to perform various tasks related to polygons, such as manipulation, area calculation, point−in−polygon tests, and more.

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

Internally, the polygon utilities in Mahotas utilize algorithms and techniques from computational geometry to achieve their functionalities. These underlying algorithms ensure accurate and efficient polygon manipulation and analysis.

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

Overall, the polygon utilities in Mahotas serve as a toolbox for working with polygons in image processing tasks, offering essential functionalities and algorithms to manipulate, analyze, and perform operations on these shapes efficiently.

Polygon Utilities Functions in Mahotas

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

Following are the different polygon utility functions available in mahotas −

S.No

Function & Description

1

convexhull() This function creates the smallest shape that encloses a set of points while maintaining its convexity (convex hull).

2

fill_convexhull() This function fills the interior of the convex hull generated by a set of points.

3

fill_polygon() This function fills the interior of a polygon defined by a set of points.

4

line() This function draws a straight line between two specified points.

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

Now, let’s briefly understand these four functions and see their examples.

The convexhull() function

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

The convexhull() function generates the convex hull of a set of points in an image. Convex hull is the smallest polygon that encloses the given points. The function generates the convex hull by identifying the vertices of the polygon and ensuring that all internal angles are less than or equal to 180 degrees.

Syntax

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

Following is the basic syntax of the convexhull() function in mahotas −

mahotas.polygon.convexhull(bwimg)

其中,

where,

  1. bwimg − It is the input binary image.

Example

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

In the example below, we are getting the hull of an image using the convexhull() function.

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()

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

Following is the output of the above code −

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

获得的图像如下所示:

The image obtained is as follows −

polygon utilities

The fill_convexhull() function

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

The fill_convexhull() function fills the interior of the convex hull with a color generated by a set of points in an image. The function ensures that all the pixels within the convex hull are assigned a specific color.

Syntax

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

Following is the basic syntax of the fill_convexhull() function in mahotas −

mahotas.polygon.fill_convexhull(bwimg)

其中,

where,

  1. bwimg − It is the input binary image.

Example

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

Here, we are filling the hull with color using the fill_convexhull() function.

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()

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

After executing the above code, we get the following output −

polygon utilities1

The fill_polygon() function

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

Similarly, the fill_polygon() function is used to fill the interior of a polygon with a specific color in an image. It takes the points representing the polygon shape and performs a filling operation within the boundary of the polygon.

Syntax

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

Following is the basic syntax of the fill_polygon() function in mahotas −

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

其中,

where,

  1. [(y0, x0), (y1, x1), …​, ] − These are list of (y, x) coordinates that define the shape of the polygon.

  2. canvas − It is the image in which the polygon will be filled.

  3. color (optional) − It determines the colors of the pixels inside the polygon (default is 1).

Example

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

In this example, we are filling a polygon with color using the fill_polygon() function.

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()

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

We get the following image as output −

polygon utilities2

The line() function

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

The line() function is used to draw a straight line between two specified points (the start and the end) in an image. The points are represented by their x and y coordinates. x0 and y0 are the coordinates of starting point, and x1 and y1 are coordinates of ending point.

Syntax

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

Following is the basic syntax of the line() function in mahotas −

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

其中,

where,

  1. (y0, x0) − It is a tuple which specifies the starting coordinates of the line.

  2. (y1, x1) − It is a tuple which specifies the ending coordinates of the line.

  3. canvas − It is the image in which the line will be filled.

  4. color (optional) − It determines the colors of the pixels inside the polygon (default is 1).

Example

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

In the following example, we are drawing a colored line using the line() function.

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()

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

Following is the output of the above code −

polygon utilities3