Mahotas 简明教程

Mahotas - SURF Dense Points

SURF(加速稳健特征)是一种用于检测和描述图像中感兴趣点的算法。这些点被称为“密集点”或“关键点”,因为它们密集地存在于图像中,不同于仅出现在特定区域的稀疏点。

SURF 算法在不同尺度分析整个图像,并识别强度大幅变化的区域。

这些区域被视为潜在的关键点。它们是有趣的区域,包含独特且显着的模式。

SURF Dense Points in Mahotas

在 Mahotas 中,我们使用 mahotas.features.surf.dense() 函数来计算 SURF 密集点处的描述符。描述符本质上是特征向量,描述图像中像素的局部特征,例如它们的强度梯度和方向。

为了生成这些描述符,该函数在图像上创建了一个点网格,每个点由特定的距离分隔。在网格中的每个点,确定一个“兴趣点”。

这些兴趣点是捕获图像详细信息的位置。一旦识别出兴趣点,就计算出密集 SURF 描述符。

The mahotas.features.surf.dense() function

mahotas.features.surf.dense() 函数将灰度图像作为输入,并返回包含描述符的数组。

这个数组通常具有一个结构:每行对应不同的兴趣点,列表示该点的描述符特征值。

以下是 mahotas 中 surf.dense() 函数的基本语法:

mahotas.features.surf.dense(f, spacing, scale={np.sqrt(spacing)},
is_integral=False, include_interest_point=False)

其中,

  1. f − 它是输入的灰度图像。

  2. spacing - 它确定相邻关键点之间的距离。

  3. scale (optional) - 它指定计算描述符时使用的间距(默认为间距的平方根)。

  4. is_integral (optional) - 它是一个标记,表示输入图像是否是整数(默认为“False”)。

  5. include_interest_point (optional) - 它也是一个标记,表示是否返回具有 SURF 点的兴趣点(默认为“False”)。

在以下示例中,我们使用 mh.features.surf.dense() 函数计算图像的 SURF 稠密点。

import mahotas as mh
from mahotas.features import surf
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Getting the SURF dense points
surf_dense = surf.dense(image, 120)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the surf dense points
axes[1].imshow(surf_dense)
axes[1].set_title('SURF Dense Point')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

surf dense points

By Adjusting the Scale

我们可以调整比例尺以在不同的空间计算 SURF 稠密点的描述符。比例尺决定了在兴趣点周围检查的区域大小。

较小的比例尺适用于捕捉局部细节,而较大的比例尺适用于捕捉全局细节。

在 mahotas 中,surf.dense() 函数的 scale 参数决定了在计算 SURF 稠密点的描述符时使用的比例尺。

我们可以将任何值传递给该参数,以检查比例尺对 SURF 稠密点的影响。

Example

在下面提到的示例中,我们正在调整比例尺以计算 SURF 稠密点的描述符:

import mahotas as mh
from mahotas.features import surf
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Getting the SURF dense points
surf_dense = surf.dense(image, 100, np.sqrt(25))
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the surf dense points
axes[1].imshow(surf_dense)
axes[1].set_title('SURF Dense Point')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

scale adjusting

By Including Interest Points

我们在计算 SURF 稠密点的描述符时还可以包含图像的兴趣点。兴趣点是像素强度值显着变化的区域。

在 mahotas 中,要在计算 SURF 稠密点的描述符时包含图像的兴趣点,我们可以将 include_interest_point 参数设置为布尔值“True”。

Example

在这里,我们在计算图像的 SURF 稠密点的描述符时包含兴趣点。

import mahotas as mh
from mahotas.features import surf
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('tree.tiff')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Getting the SURF dense points
surf_dense = surf.dense(image, 100, include_interest_point=True)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image)
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the surf dense points
axes[1].imshow(surf_dense)
axes[1].set_title('SURF Dense Point')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

include interest points