Mahotas 简明教程

Mahotas - Mean Value of Image

图像的平均值是指图像中所有像素的平均亮度。亮度是图像的一个属性,它决定了图像对人眼来说看起来有多亮或多暗。

它由像素强度值决定;较高的像素强度值表示较亮的区域,而较低的像素强度值表示较暗的区域。

图像的平均值广泛用于图像分割,它涉及将图像分割成不同的区域。

它还可以用于图像阈值处理,即把图像转换为由前景和背景像素组成的二进制图像。

Mean Value of Image in Mahotas

Mahotas 没有内置找到图像平均值的功能。不过,我们可以同时使用 mahotas 和 numpy 库找到图像的平均值。

我们可以使用 numpy 库中的 mean() 函数找到图像的平均像素强度值。

mean() 函数通过逐个遍历每个像素并对其强度值求和来工作。遍历所有像素之后,它将总和除以像素总数。

可以使用以下公式计算图像的平均像素强度值:

Mean = Sum of all pixel values / Total number of pixels

例如,假设图像由两像素构成,每个像素强度值均为 5。则平均值可按如下方式计算:

Mean = 10 / 2
Mean = 5

The numpy.mean() function

numpy.mean() 函数将图像作为输入,返回其所有像素的平均亮度(以十进制数形式)。mean 函数适用于任何类型的输入图像,如 RGB、灰度或标记图像。

以下是 numpy 中 mean() 函数的基本语法−

numpy.mean(image)

其中,

  1. image - 它是输入图像。

在以下示例中,我们使用 np.mean() 函数查找图像的平均像素强度值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Finding the mean value
mean_value = np.mean(image)
# Printing the mean value
print('Mean value of the image is =', mean_value)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the original image
axes.imshow(image)
axes.set_title('Original Image')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

Mean value of the image is = 105.32921300415184

获得的图像如下所示:

mean value image

Mean Value of each Channel

我们还可以在 Mahotas 中找出 RGB 图像的每个通道的平均值。RGB 图像是指具有三色通道的图像,即红色、绿色和蓝色。

RGB 图像中的每个像素具有三个强度值,每个颜色通道一个。

红色通道值为 0,绿色通道值为 1,蓝色通道值为 2。这些值可用于将 RGB 图像分离为其各个颜色分量。

在 Mahotas 中,为了找到 RGB 图像的每个通道的平均像素强度值,我们首先将 RGB 图像分离为各个通道。这可以通过指定通道值来实现。分离各通道后,我们可以分别找到它们的平均值。

Example

在下面提到的示例中,我们查找 RGB 图像的每个通道的平均像素强度值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('nature.jpeg')
# Getting the red channel.
red_channel = image[:, :, 0]
# Getting the green channel.
green_channel = image[:, :, 1]
# Getting the blue channel.
blue_channel = image[:, :, 2]
# Finding the mean value of each channel
mean_red = np.mean(red_channel)
mean_green = np.mean(green_channel)
mean_blue = np.mean(blue_channel)
# Printing the mean value of each channel
print('Mean value of the Red channel is =', mean_red)
print('Mean value of the Green channel is =', mean_green)
print('Mean value of the Blue channel is =', mean_blue)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the original image
axes.imshow(image)
axes.set_title('Original Image')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

上述代码的输出如下:

Mean value of the Red channel is = 135.4501688464837
Mean value of the Green channel is = 139.46532482847343
Mean value of the Blue channel is = 109.7802007397084

产生的图像如下:

mean value channel

Mean Value of Grayscale Image

我们也可以找到灰度图像的平均值。灰度图像是指仅具有单一颜色通道的图像。

灰度图像的每个像素由单个强度值表示。

灰度图像的强度值范围为 0(黑色)到 255(白色)。0 和 255 之间的任何值都将产生一种灰度。较低的值将产生较暗的阴影,而较高的值将产生较亮的阴影。

在 Mahotas 中,我们首先使用 mh.colors.rgb2gray() 函数将输入的 RGB 图像转换为灰度。然后,我们使用 mean() 函数找到它的平均像素强度值。

Example

在此示例中,我们正在查找灰度图像的平均像素强度值。

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
grayscale_image = mh.colors.rgb2gray(image)
# Finding the mean value of the grayscale image
mean_value = np.mean(grayscale_image)
# Printing the mean value of the image
print('Mean value of the grayscale image is =', mean_value)
# Creating a figure and axes for subplots
fig, axes = mtplt.subplots(1, 1)
# Displaying the grayscale image
axes.imshow(grayscale_image, cmap='gray')
axes.set_title('Grayscale Image')
axes.set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

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

Mean value of the grayscale image is = 113.21928107579335

以下为获得的图像:

mean value channel1