Mahotas - Riddler-Calvard 方法



Riddler-Calvard 方法是一種用於將影像分割為前景和背景區域的技術。它對影像畫素進行分組,以在計算閾值時最小化組內方差。

組內方差衡量的是畫素值在一個組內的分散程度。組內方差低表示畫素值彼此接近,而組內方差高表示畫素值分散。

Mahotas 中的 Riddler-Calvard 方法

在 Mahotas 中,我們使用 `thresholding.rc()` 函式使用 Riddler-Calvard 技術計算影像的閾值。該函式的工作方式如下:

  • 它計算兩個叢集(前景和背景)的均值和方差。均值是所有畫素的平均值,方差是畫素分散程度的度量。

  • 接下來,它選擇一個最小化組內方差的閾值。

  • 然後,它將每個畫素分配給方差較小的叢集。

步驟 2 和 3 連續重複,直到計算出閾值。然後使用此值將影像分割為前景和背景。

`mahotas.thresholding.rc()` 函式

`mahotas.thresholding.rc()` 函式以灰度影像作為輸入,並返回使用 Riddler-Calvard 技術計算的閾值。

然後將灰度影像的畫素與閾值進行比較,以建立二值影像。

語法

以下是 Mahotas 中 `rc()` 函式的基本語法:

mahotas.thresholding.rc(img, ignore_zeros=False)

其中:

  • `img` - 輸入灰度影像。

  • `ignore_zeros (可選)` - 一個標誌,指定是否忽略零值畫素(預設為 false)。

示例

在下面的示例中,我們使用 `mh.thresholding.rc()` 函式查詢閾值。

import mahotas as mh
import numpy as np
import matplotlib.pyplot as mtplt
# Loading the image
image = mh.imread('sun.png')
# Converting it to grayscale
image = mh.colors.rgb2gray(image).astype(np.uint8)
# Calculating threshold value using Riddler-Calvard method
rc_threshold = mh.thresholding.rc(image)
# Creating image from the threshold value
final_image = image > rc_threshold
# 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 threshold image
axes[1].imshow(final_image, cmap='gray')
axes[1].set_title('Riddler-Calvard Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()
輸出

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

Riddler-Calvard

忽略零值畫素

我們還可以透過忽略零值畫素來找到 Riddler-Calvard 閾值。零值畫素是指強度值為 0 的畫素。

它們通常代表影像的背景畫素,但在某些影像中,它們也可能代表噪聲。

在灰度影像中,零值畫素由顏色“黑色”表示。

為了在 Mahotas 中計算閾值時排除零值畫素,我們可以將 `ignore_zeros` 引數設定為布林值“True”。

示例

在下面提到的示例中,我們在使用 Riddler-Calvard 方法計算閾值時忽略值為零的畫素。

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).astype(np.uint8)
# Calculating threshold value using Riddler-Calvard method
rc_threshold = mh.thresholding.rc(image, ignore_zeros=True)
# Creating image from the threshold value
final_image = image > rc_threshold
# 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 threshold image
axes[1].imshow(final_image, cmap='gray')
axes[1].set_title('Riddler-Calvard Threshold Image')
axes[1].set_axis_off()
# Adjusting spacing between subplots
mtplt.tight_layout()
# Showing the figures
mtplt.show()

輸出

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

Ignore Zero Valued Pixels
廣告
© . All rights reserved.