Mahotas 简明教程
Mahotas - Mean Value of Image
图像的平均值是指图像中所有像素的平均亮度。亮度是图像的一个属性,它决定了图像对人眼来说看起来有多亮或多暗。
Mean value of an image refers to the average brightness of all the pixels of an image. Brightness is a property of an image that determines how light or dark an image appears to the human eye.
它由像素强度值决定;较高的像素强度值表示较亮的区域,而较低的像素强度值表示较暗的区域。
It is determined by the pixel intensity value; higher pixel intensity values represent brighter areas, while lower pixel intensity values represent darker areas.
图像的平均值广泛用于图像分割,它涉及将图像分割成不同的区域。
The mean value of an image is widely used in image segmentation, which involves dividing an image into distinct regions.
它还可以用于图像阈值处理,即把图像转换为由前景和背景像素组成的二进制图像。
It can also be used in image thresholding which refers to converting an image into binary image consisting of foreground and background pixels.
Mean Value of Image in Mahotas
Mahotas 没有内置找到图像平均值的功能。不过,我们可以同时使用 mahotas 和 numpy 库找到图像的平均值。
Mahotas does not have a built-in function to find the mean of value of an image. However, we can find the mean value of an image by using mahotas and numpy library together.
我们可以使用 numpy 库中的 mean() 函数找到图像的平均像素强度值。
We can use the mean() function in the numpy library to find the mean pixel intensity value of an image.
mean() 函数通过逐个遍历每个像素并对其强度值求和来工作。遍历所有像素之后,它将总和除以像素总数。
The mean() function works by iteratively going over each pixel and summing its intensity value. Once all the pixels have been traversed, it divides the sum by the total number of pixels.
可以使用以下公式计算图像的平均像素强度值:
The mean pixel intensity value of an image can be calculated using the following formula −
Mean = Sum of all pixel values / Total number of pixels
例如,假设图像由两像素构成,每个像素强度值均为 5。则平均值可按如下方式计算:
For example, let’s assume that an image is composed of 2 pixels each with an intensity value of 5. Then the mean can be calculated as follows −
Mean = 10 / 2
Mean = 5
The numpy.mean() function
numpy.mean() 函数将图像作为输入,返回其所有像素的平均亮度(以十进制数形式)。mean 函数适用于任何类型的输入图像,如 RGB、灰度或标记图像。
The numpy.mean() function takes an image as input and returns the average brightness of all its pixels as a decimal number. The mean function works on any type of input image such as RGB, grayscale or labeled.
以下是 numpy 中 mean() 函数的基本语法−
Following is the basic syntax of the mean() function in numpy −
numpy.mean(image)
其中,
Where,
-
image − It is the input image.
在以下示例中,我们使用 np.mean() 函数查找图像的平均像素强度值。
In the following example, we are finding the average pixel intensity value of an image using the np.mean() function.
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()
以下是上面代码的输出: -
Following is the output of the above code −
Mean value of the image is = 105.32921300415184
获得的图像如下所示:
The image obtained is as shown below −
Mean Value of each Channel
我们还可以在 Mahotas 中找出 RGB 图像的每个通道的平均值。RGB 图像是指具有三色通道的图像,即红色、绿色和蓝色。
We can also find the mean value of each channel of an RGB image in Mahotas. RGB images to refer to images having three−color channels − Red, Green, and Blue.
RGB 图像中的每个像素具有三个强度值,每个颜色通道一个。
Each pixel in an RGB image has three intensity values, one for each color channel.
红色通道值为 0,绿色通道值为 1,蓝色通道值为 2。这些值可用于将 RGB 图像分离为其各个颜色分量。
The channel value of red is 0, green is 1 and blue is 2. These values can be used to separate an RGB image into its individual color components.
在 Mahotas 中,为了找到 RGB 图像的每个通道的平均像素强度值,我们首先将 RGB 图像分离为各个通道。这可以通过指定通道值来实现。分离各通道后,我们可以分别找到它们的平均值。
In mahotas, to find the mean pixel intensity value of each channel of an RGB image, we first separate the RGB image into separate channels. This is achieved by specifying the channel value. Once the channels are separated, we can find their mean value individually.
Example
在下面提到的示例中,我们查找 RGB 图像的每个通道的平均像素强度值。
In the example mentioned below, we are finding the mean pixel intensity value of each channel of an RGB image.
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()
上述代码的输出如下:
Output of the above code is as follows −
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
产生的图像如下:
The image produced is as follows −
Mean Value of Grayscale Image
我们也可以找到灰度图像的平均值。灰度图像是指仅具有单一颜色通道的图像。
We can find the mean value of a grayscale image as well. Grayscale images refer to the image having only a single−color channel.
灰度图像的每个像素由单个强度值表示。
Each pixel of a grayscale image is represented by a single intensity value.
灰度图像的强度值范围为 0(黑色)到 255(白色)。0 和 255 之间的任何值都将产生一种灰度。较低的值将产生较暗的阴影,而较高的值将产生较亮的阴影。
The intensity value of a grayscale image can range from 0 (black) to 255 (white). Any value between 0 and 255 will produce a shade of gray. Lower values will produce darker shades while higher values will produce lighter shades.
在 Mahotas 中,我们首先使用 mh.colors.rgb2gray() 函数将输入的 RGB 图像转换为灰度。然后,我们使用 mean() 函数找到它的平均像素强度值。
In mahotas, we first convert an input RGB image to grayscale using the mh.colors.rgb2gray() function. Then, we find its mean pixel intensity value using the mean() function.
Example
在此示例中,我们正在查找灰度图像的平均像素强度值。
In this example, we are finding the mean pixel intensity value of a 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
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
Mean value of the grayscale image is = 113.21928107579335
以下为获得的图像:
Following is the image obtained −