Mahotas - 中值濾波器



中值濾波器是另一種常用的影像降噪技術。它的工作原理是計算相鄰畫素的中值(中間值),並用該中值替換原始畫素值。

為了理解中值濾波器,讓我們考慮同樣的黑白影像場景,其中小的黑點代表噪聲。影像中的每個畫素都有一個二值——白色(代表感興趣的物件)或黑色(代表背景)。

對於每個畫素,中值濾波器都會獲取視窗內相鄰畫素的畫素值。然後,它根據畫素的強度值將它們按升序排列。

之後,它選擇中間值(即中值),並用該中值替換原始畫素值。

Mahotas中的中值濾波器

要在Mahotas中應用中值濾波器,可以使用`median_filter()`函式。

Mahotas中的中值濾波器函式使用結構元素來檢查鄰域中的畫素。

結構元素透過計算其鄰域內的中值來替換每個畫素的值。

結構元素的大小決定了中值濾波器應用的平滑程度。

較大的鄰域將產生更強的平滑效果,同時減少影像的精細細節。另一方面,較小的鄰域將產生較少的平滑效果,但會保留更多細節。

`mahotas.median_filter()`函式

`median_filter()`函式使用指定的鄰域大小對輸入影像應用中值濾波器。它用在其鄰居中計算的中值替換每個畫素值。過濾後的影像儲存在輸出陣列中。

語法

以下是Mahotas中中值濾波器函式的基本語法:

mahotas.median_filter(img, Bc={square}, mode='reflect', cval=0.0,
out={np.empty(f.shape, f.dtype})

其中,

  • img - 輸入影像。

  • Bc - 定義鄰域的結構元素。預設情況下,它是一個邊長為3的正方形。

  • mode (可選) - 指定函式如何處理影像的邊界。它可以取不同的值,例如'reflect','constant','nearest','mirror'或'wrap'。預設設定為'reflect'。

  • cval (可選) - 當mode='constant'時使用的值。預設值為0.0。

  • out (可選) - 指定儲存過濾後圖像的輸出陣列。它必須與輸入影像具有相同的形狀和資料型別。

示例

以下是使用`median_filter()`函式過濾影像的基本示例:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image=mh.imread('tree.tiff', as_grey = True)
structuring_element = mh.disk(12)
filtered_image = mh.median_filter(image, structuring_element)
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the median filtered image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Median Filtered')
axes[1].axis('off')
mtplt.show()
輸出

執行上述程式碼後,我們將得到以下輸出:

Median Filter

使用反射模式的中值濾波器

當我們將中值濾波器應用於影像時,我們需要考慮每個畫素周圍的相鄰畫素來計算中值。

但是,在影像的邊緣,有些畫素在一側或多側沒有鄰居。

為了解決這個問題,我們使用'reflect'模式。反射模式沿影像邊緣建立映象效果。它允許我們透過映象方式複製畫素來虛擬地擴充套件影像。

這樣,我們即使在邊緣也能為中值濾波器提供相鄰畫素。

透過反射影像值,中值濾波器現在可以將這些映象畫素視為真實的鄰居。

它使用這些虛擬鄰居計算中值,從而在影像邊緣產生更精確的平滑過程。

示例

在這裡,我們嘗試使用反射模式計算中值濾波器:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image=mh.imread('nature.jpeg', as_grey = True)
structuring_element = mh.morph.dilate(mh.disk(12), Bc=mh.disk(12))
filtered_image = mh.median_filter(image, structuring_element, mode='reflect')
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the median filtered image
axes[1].imshow(filtered_image, cmap='gray')
axes[1].set_title('Median Filtered')
axes[1].axis('off')
mtplt.show()

輸出

上述程式碼的輸出如下:

Median Filter Reflect Mode

透過將結果儲存在輸出陣列中

我們也可以使用Mahotas將中值濾波器的結果儲存在輸出陣列中。為此,我們首先需要使用NumPy庫建立一個空陣列。

此陣列使用與輸入影像相同的形狀和資料型別進行初始化,以儲存生成的濾波影像。

最後,我們將生成的濾波影像儲存在輸出陣列中,將其作為引數傳遞給`median_filter()`函式。

示例

現在,我們嘗試將中值濾波器應用於灰度影像並將結果儲存在特定的輸出陣列中:

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
image=mh.imread('pic.jpg', as_grey = True)
# Create an output array for the filtered image
output = np.empty(image.shape)
# store the result in the output array
mh.median_filter(image, Bc=mh.disk(12), out=output)
# Displaying the original image
fig, axes = mtplt.subplots(1, 2, figsize=(9, 4))
axes[0].imshow(image, cmap='gray')
axes[0].set_title('Original Image')
axes[0].axis('off')
# Displaying the median filtered image
axes[1].imshow(output, cmap='gray')
axes[1].set_title('Median Filtered')
axes[1].axis('off')
mtplt.show()

輸出

以下是上述程式碼的輸出:

Storing Result Output Array Mohatas
廣告