Mahotas 简明教程

Mahotas - Computing Linear Binary Patterns

线性二值模式 (LBP) 用于分析图像中的图案。它将图像中中心像素的强度值与相邻像素进行比较,并将结果编码为二进制模式(0 或 1)。

Linear Binary Patterns (LBP) is used to analyze the patterns in an image. It compares the intensity value of a central pixel in the image with its neighboring pixels, and encodes the results into binary patterns (either 0 or 1).

想象一下您有一幅灰度图像,其中每个像素表示从黑色到白色不等的灰色阴影。LBP 将图像划分为小区域。

Imagine you have a grayscale image, where each pixel represents a shade of gray ranging from black to white. LBP divides the image into small regions.

对于每个区域,它都会观察中心像素并将其亮度与相邻像素进行比较。

For each region, it looks at the central pixel and compares its brightness with the neighboring pixels.

如果相邻像素比中心像素更亮或等于中心像素,则为其分配值 1;否则,为其分配值 0。此过程针对所有相邻像素重复,从而创建二进制模式。

If a neighboring pixel is brighter or equal to the central pixel, it’s assigned a value of 1; otherwise, it’s assigned a value of 0. This process is repeated for all the neighboring pixels, creating a binary pattern.

Computing Linear Binary Patterns in Mahotas

在 Mahotas 中,我们可以使用 features.lbp() 函数来计算图像中的线性二值模式。此函数将中心像素的亮度与其相邻像素进行比较,并根据比较分配二进制值(0 或 1)。

In Mahotas, we can use the features.lbp() function to compute linear binary patterns in an image. The function compares the brightness of the central pixel with its neighbors and assigns binary values (0 or 1) based on the comparisons.

这些二进制值随后组合在一起,生成二进制模式,该模式描述每个区域的纹理。通过对所有区域执行该操作,创建直方图以统计图像中每个模式的出现次数。

These binary values are then combined to create a binary pattern that describes the texture in each region. By doing this for all regions, a histogram is created to count the occurrence of each pattern in the image.

该直方图帮助我们了解图像中的纹理分布。

The histogram helps us to understand the distribution of textures in the image.

The mahotas.features.lbp() function

mahotas.features.lbp() 函数将灰度图像作为输入,并返回每个像素的二进制值。然后使用该二进制值创建线性二进制模式的直方图。

The mahotas.features.lbp() function takes a grayscale image as an input and returns binary value of each pixel. The binary value is then used to create a histogram of the linear binary patterns.

直方图的 x 轴表示计算所得的 LBP 值,而 y 轴表示 LBP 值的频率。

The x−axis of the histogram represents the computed LBP value while the y−axis represents the frequency of the LBP value.

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

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

mahotas.features.lbp(image, radius, points, ignore_zeros=False)

其中,

Where,

  1. image − It is the input grayscale image.

  2. radius − It specifies the size of the region considered for comparing pixel intensities.

  3. points − It determines the number of neighboring pixels that should be considered when computing LBP for each pixel.

  4. ignore_zeros (optional) − It is a flag which specifies whether to ignore zero valued pixels (default is false).

在以下示例中,我们使用 mh.features.lbp() 函数计算线性二进制模式。

In the following example, we are computing linear binary patterns using the mh.features.lbp() function.

import mahotas as mh
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)
# Computing linear binary patterns
lbp = mh.features.lbp(image, 5, 5)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the linear binary patterns
axes[1].hist(lbp)
axes[1].set_title('Linear Binary Patterns')
axes[1].set_xlabel('LBP Value')
axes[1].set_ylabel('Frequency')
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

Following is the output of the above code −

computing linear binary patterns

Ignoring the Zero Valued Pixels

在计算线性二进制模式时,我们可以忽略值为空的像素。值为 0 的像素表示强度值为 0 的像素。

We can ignore the zero valued pixels when computing linear binary patterns. Zero valued pixels are referred to the pixels having an intensity value of 0.

它们通常表示图像的背景,但可能也表示噪声。在灰度图像中,值为 0 的像素由颜色“黑色”表示。

They usually represent the background of an image but may also represent noise. In grayscale images, zero valued pixels are represented by the color 'black'.

在 mahotas 中,我们可以将 ignore_zeros 参数设置为布尔值“True”,以在 mh.features.lbp() 函数中排除值为 0 的像素。

In mahotas, we can set the ignore_zeros parameter to the boolean value 'True' to exclude zero valued pixels in the mh.features.lbp() function.

Example

以下示例显示了通过忽略值为 0 的像素来计算线性二进制模式。

The following example shows computation of linear binary patterns by ignoring the zero valued pixels.

import mahotas as mh
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)
# Computing linear binary patterns
lbp = mh.features.lbp(image, 20, 10, ignore_zeros=True)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the linear binary patterns
axes[1].hist(lbp)
axes[1].set_title('Linear Binary Patterns')
axes[1].set_xlabel('LBP Value')
axes[1].set_ylabel('Frequency')
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

Output of the above code is as follows −

zero valued pixels mahotas

LBP of a Specific Region

我们还可以计算图像中特定区域的线性二进制模式。特定区域是指具有任何尺寸的图像部分。它可以通过裁剪原始图像获得。

We can also compute the linear binary patterns of a specific region in the image. Specific regions refer to a portion of an image having any dimension. It can be obtained by cropping the original image.

在 mahotas 中,要计算特定区域的线性二进制模式,我们首先需要从图像中找到感兴趣的区域。为此,我们分别为 x 和 y 坐标指定起始和结束像素值。然后,我们可以使用 lbp() 函数计算该区域的 LBP。

In mahotas, to compute linear binary patterns of a specific region, we first need to find the region of interest from the image. To do this, we specify the starting and ending pixel values for the x and y coordinates respectively. Then we can compute the LBP of this region using the lbp() function.

例如,如果我们将值指定为 [300:800],那么该区域将从 300 个像素开始,垂直方向(y 轴)上达到 800 个像素。

For example, if we specify the values as [300:800], then the region will start from 300 pixels and go up to 800 pixels in the vertical direction (y−axis).

Example

在这里,我们正在计算指定灰度图像的特定部分的 LBP。

Here, we are computing the LBP of a specific portion of the specified grayscale image.

import mahotas as mh
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)
# Specifying a region of interest
image = image[300:800]
# Computing linear binary patterns
lbp = mh.features.lbp(image, 20, 10)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 2)
# Displaying the original image
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].set_axis_off()
# Displaying the linear binary patterns
axes[1].hist(lbp)
axes[1].set_title('Linear Binary Patterns')
axes[1].set_xlabel('LBP Value')
axes[1].set_ylabel('Frequency')
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

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

zero valued pixels mahotas1