Mahotas 简明教程

Mahotas - Computing Linear Binary Patterns

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

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.

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

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

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.

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

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

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

其中,

  1. image − It is the input grayscale image.

  2. radius − 它指定用于比较像素强度的区域大小。

  3. points − 它确定在计算每个像素的 LBP 时应考虑的相邻像素数量。

  4. ignore_zeros (optional) − 它是指定是否忽略零值像素的标志(默认为 false)。

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

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

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

computing linear binary patterns

Ignoring the Zero Valued Pixels

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

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

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

Example

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

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

上述代码的输出如下:

zero valued pixels mahotas

LBP of a Specific Region

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

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

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

Example

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

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

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

zero valued pixels mahotas1