Mahotas 简明教程

Mahotas - SURF Integral

SURF,代表加速稳健特征(Speeded−Up Robust Features),是一种用于检测特征的算法。SURF 积分是此算法中的一个关键概念。

SURF, which stands for Speeded−Up Robust Features, is an algorithm used to detect features. The SURF integral is a key concept within this algorithm.

为了理解 SURF 积分,让我们从图像的概念开始思考。图像由像素构成,这些像素是存储在该特定位置图像强度信息的微小点。

To understand the SURF integral, let’s start with the idea of an image. An image is composed of pixels, which are tiny dots that store information about the intensity of the image at that particular location.

现在,想象一下将图像划分为一个小的局部邻域。SURF 积分是一种有效计算每个局部邻域的总像素值的方法。

Now, imagine dividing the image into a small local neighborhood. The SURF integral is a way to efficiently calculate the total pixel value for each local neighborhood.

SURF Integral in Mahotas

在 Mahotas 中,我们可以使用 mahotas.features.surf.integral() 函数来计算图像的 SURF 积分。以下是有关该函数工作原理的基本方法的说明 −

In Mahotas, we can use the mahotas.features.surf.integral() function to compute the SURF integral of an image. Following is the basic approach of how the function works −

  1. Initialization − First, the function initializes the integral image by setting all the pixel values to zero. Integral images refer to images that store the sum of all pixels up to a certain point.

  2. Recursive Sum Calculation − The function then proceeds to calculate the sum of pixels for each point in the integral image. It does this recursively, meaning it calculates the sum for each point based on the previous sums.

由于积分图像存储了到特定点的所有像素的总和,因此它们可以极大地提高计算 SURF 描述符的速度。由于该函数使用递归,因此计算大图像的总和时速度可能会较慢。

As the integral images store the sum of all pixels up to a specific point, they can significantly increase the speed of computing SURF descriptors. Since the function uses recursion, it can be slow for computing the sum of large images.

The mahotas.features.surf.integral() function

mahotas.features.surf.integral() 函数将灰度图像作为输入,并返回一个积分图像作为输出。

The mahotas.features.surf.integral() function takes a grayscale image as input and returns an integral image as output.

返回的结果是一个新图像,通常采用 NumPy 数组的形式,其中每个像素值对应于到该像素位置的像素强度的总和。

The returned result is a new image, typically in the form of a NumPy array, where each pixel value corresponds to the sum of pixel intensities up to that pixel location.

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

Following is the basic syntax of the surf.integral() function in mahotas −

mahotas.features.surf.integral(f, in_place=False, dtype=<class
'numpy.float64'>)

其中,

Where,

  1. f − It is the input image.

  2. in_place (optional) − It a flag which determines whether to overwrite the input image (default is 'False').

  3. dtype (optional) − It specifies the data type of the output image (default is float64).

在以下示例中,我们使用 mh.features.surf.integral() 函数计算图像的 SURF 积分。

In the following example, we are calculating the SURF integral of an image using the mh.features.surf.integral() function.

import mahotas as mh
from mahotas.features import surf
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sea.bmp')
# Converting it to grayscale
image = mh.colors.rgb2gray(image)
# Getting the SURF integral
surf_integral = surf.integral(image)
# 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 integral
axes[1].imshow(surf_integral)
axes[1].set_title('SURF Integral')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

Following is the output of the above code −

surf integral

SURF Integral of a Random Image

我们还可以计算随机生成的两维图像的 SURF 积分。二维随机图像是指每个像素被分配一个随机强度值的图像。强度值范围可以从 0(黑色)到 255(白色)。

We can also compute SURF integral of a randomly generate two−dimensional image. A two−dimensional random image refers to an image where each pixel is assigned a random intensity value. The intensity value can range from 0 (black) to 255 (white).

在 mahotas 中,要创建一个二维随机图像,我们首先指定它的维度。然后,我们将这些维度与像素的强度范围一起传递给 np.random.randint() 函数。

In mahotas, to create a 2−D random image we first specify its dimensions. Then, we pass these dimensions along with the intensity range of the pixels to np.random.randint() function.

之后,我们可以使用 surf.integral() 函数计算图像的 SURF 积分。

After that we can compute the SURF integral of the image using the surf.integral() function.

Example

在下面提到的示例中,我们正在计算随机生成的二维图像的 SURF 积分。

In the example mentioned below, we are computing the SURF integral of a randomly generated 2−D image.

import mahotas as mh
from mahotas.features import surf
import numpy as np
import matplotlib.pyplot as mtplt
# Specifying dimensions of image
l, w = 1000, 1000
# Creating a random 2-D image
image = np.random.randint(0, 256, (l, w))
# Getting the SURF integral
surf_integral = surf.integral(image)
# 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 integral
axes[1].imshow(surf_integral)
axes[1].set_title('SURF Integral')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

Output of the above code is as follows −

surf integral random image

SURF Integral of a Threshold Image

除了随机二维图像外,我们还可以计算阈值图像的 SURF 积分。阈值图像是一个二值图像,其中像素被分类为前景或背景。

In addition to random 2−D images, we can also compute the SURF integral of a threshold image. A threshold image is a binary image where the pixels are classified into the foreground or the background.

前景像素为白色,由值 1 表示,而背景像素为黑色,由值 0 表示。

The foreground pixels are white and represented by the value 1, while the background pixels are black and represented by value 0.

在 mahotas 中,我们首先使用任何阈值算法对输入图像进行阈值处理。让我们假设 Bernsen thresholding algorithm 。这可以通过在灰度图像上使用 mh.thresholding.bernsen() 函数来实现。然后,我们可以使用 surf.integral() 函数计算阈值图像的 SURF 积分。

In mahotas, we first threshold the input image using any thresholding algorithm. Let us assume Bernsen thresholding algorithm. This can be done by using the mh.thresholding.bernsen() function on a grayscale image. Then, we can compute the SURF integral of threshold image using the surf.integral() function.

Example

在这里,我们正在计算阈值图像的 SURF 积分。

In here, we are calculating SURF integral of a threshold image.

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)
# Thresholding the image
image = mh.thresholding.bernsen(image, 5, 5)
# Getting the SURF integral
surf_integral = surf.integral(image)
# 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 integral
axes[1].imshow(surf_integral)
axes[1].set_title('SURF Integral')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

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

surf integral random image1