Mahotas - 影像平均值



影像的平均值指的是影像所有畫素的平均亮度。亮度是影像的一種屬性,決定了影像在人眼看來是明亮還是暗淡。

它由畫素強度值決定;較高的畫素強度值表示較亮的區域,而較低的畫素強度值表示較暗的區域。

影像的平均值廣泛用於影像分割,影像分割涉及將影像劃分為不同的區域。

它也可以用於影像閾值化,影像閾值化指的是將影像轉換為包含前景和背景畫素的二值影像。

Mahotas 中的影像平均值

Mahotas 沒有內建函式來查詢影像的平均值。但是,我們可以使用 mahotas 和 numpy 庫一起查詢影像的平均值。

我們可以使用 numpy 庫中的 mean() 函式來查詢影像的平均畫素強度值。

mean() 函式的工作原理是迭代遍歷每個畫素並將其強度值求和。遍歷完所有畫素後,它將總和除以畫素總數。

可以使用以下公式計算影像的平均畫素強度值:

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

例如,假設一個影像由 2 個畫素組成,每個畫素的強度值為 5。然後可以按如下方式計算平均值:

Mean = 10 / 2
Mean = 5

numpy.mean() 函式

numpy.mean() 函式以影像作為輸入,並將其所有畫素的平均亮度作為十進位制數返回。mean 函式適用於任何型別的輸入影像,例如 RGB、灰度或標記影像。

語法

以下是 numpy 中 mean() 函式的基本語法:

numpy.mean(image)

其中,

  • 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

每個通道的平均值

我們還可以找到 Mahotas 中 RGB 影像每個通道的平均值。RGB 影像指的是具有三個顏色通道的影像:紅色、綠色和藍色。

RGB 影像中的每個畫素都有三個強度值,每個顏色通道一個。

紅色的通道值為 0,綠色的通道值為 1,藍色的通道值為 2。這些值可用於將 RGB 影像分離成其各個顏色分量。

在 mahotas 中,要查詢 RGB 影像每個通道的平均畫素強度值,我們首先將 RGB 影像分離成單獨的通道。這是透過指定通道值來實現的。分離通道後,我們可以分別找到它們的平均值。

示例

在下面提到的示例中,我們正在查詢 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

灰度影像的平均值

我們還可以找到灰度影像的平均值。灰度影像指的是隻有一個顏色通道的影像。

灰度影像的每個畫素都由單個強度值表示。

灰度影像的強度值範圍從 0(黑色)到 255(白色)。0 到 255 之間的任何值都會產生灰色陰影。較低的值會產生較暗的陰影,而較高的值會產生較亮的陰影。

在 mahotas 中,我們首先使用 mh.colors.rgb2gray() 函式將輸入 RGB 影像轉換為灰度。然後,我們使用 mean() 函式找到其平均畫素強度值。

示例

在此示例中,我們正在查詢灰度影像的平均畫素強度值。

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
廣告