
- Mahotas 教程
- Mahotas - 首頁
- Mahotas - 簡介
- Mahotas - 計算機視覺
- Mahotas - 歷史
- Mahotas - 特性
- Mahotas - 安裝
- Mahotas 影像處理
- Mahotas - 影像處理
- Mahotas - 載入影像
- Mahotas - 以灰度載入影像
- Mahotas - 顯示影像
- Mahotas - 顯示影像形狀
- Mahotas - 儲存影像
- Mahotas - 影像質心
- Mahotas - 影像卷積
- Mahotas - 建立RGB影像
- Mahotas - 影像尤拉數
- Mahotas - 影像中零的比例
- Mahotas - 獲取影像矩
- Mahotas - 影像區域性最大值
- Mahotas - 影像橢圓軸
- Mahotas - 影像拉伸RGB
- Mahotas 顏色空間轉換
- Mahotas - 顏色空間轉換
- Mahotas - RGB轉灰度轉換
- Mahotas - RGB轉LAB轉換
- Mahotas - RGB轉棕褐色
- Mahotas - RGB轉XYZ轉換
- Mahotas - XYZ轉LAB轉換
- Mahotas - XYZ轉RGB轉換
- Mahotas - 增加伽馬校正
- Mahotas - 拉伸伽馬校正
- Mahotas 標籤影像函式
- Mahotas - 標籤影像函式
- Mahotas - 影像標記
- Mahotas - 過濾區域
- Mahotas - 邊界畫素
- Mahotas - 形態學運算
- Mahotas - 形態學運算元
- Mahotas - 查詢影像均值
- Mahotas - 裁剪影像
- Mahotas - 影像離心率
- Mahotas - 影像疊加
- Mahotas - 影像圓度
- Mahotas - 調整影像大小
- Mahotas - 影像直方圖
- Mahotas - 膨脹影像
- Mahotas - 腐蝕影像
- Mahotas - 分水嶺演算法
- Mahotas - 影像開運算
- Mahotas - 影像閉運算
- Mahotas - 填充影像空洞
- Mahotas - 條件膨脹影像
- Mahotas - 條件腐蝕影像
- Mahotas - 影像條件分水嶺演算法
- Mahotas - 影像區域性最小值
- Mahotas - 影像區域最大值
- Mahotas - 影像區域最小值
- Mahotas - 高階概念
- Mahotas - 影像閾值化
- Mahotas - 設定閾值
- Mahotas - 軟閾值
- Mahotas - Bernsen區域性閾值化
- Mahotas - 小波變換
- 製作影像小波中心
- Mahotas - 距離變換
- Mahotas - 多邊形工具
- Mahotas - 區域性二值模式
- 閾值鄰域統計
- Mahotas - Haralick特徵
- 標記區域的權重
- Mahotas - Zernike特徵
- Mahotas - Zernike矩
- Mahotas - 排序濾波器
- Mahotas - 2D拉普拉斯濾波器
- Mahotas - 多數濾波器
- Mahotas - 均值濾波器
- Mahotas - 中值濾波器
- Mahotas - Otsu方法
- Mahotas - 高斯濾波
- Mahotas - Hit & Miss變換
- Mahotas - 標記最大值陣列
- Mahotas - 影像均值
- Mahotas - SURF密集點
- Mahotas - SURF積分圖
- Mahotas - Haar變換
- 突出影像最大值
- 計算線性二值模式
- 獲取標籤邊界
- 反轉Haar變換
- Riddler-Calvard方法
- 標記區域的大小
- Mahotas - 模板匹配
- 加速魯棒特徵
- 去除帶邊框的標記
- Mahotas - Daubechies小波
- Mahotas - Sobel邊緣檢測
Mahotas - 軟閾值
軟閾值是指降低影像的噪聲(降噪)以提高其質量。
它根據畫素與閾值的接近程度,為畫素分配一個連續的值範圍。這導致前景和背景區域之間逐漸過渡。
在軟閾值中,閾值決定了降噪和影像儲存之間的平衡。較高的閾值會導致更強的降噪,但會導致資訊丟失。
相反,較低的閾值保留更多資訊,但會導致不需要的噪聲。
Mahotas中的軟閾值
在Mahotas中,我們可以使用thresholding.soft_threshold()函式對影像應用軟閾值。它根據相鄰畫素動態調整閾值,以增強具有非均勻噪聲水平的影像。
透過使用動態調整,該函式按比例降低那些強度超過閾值的畫素的強度,並將它們分配給前景。
另一方面,如果畫素的強度低於閾值,則將其分配給背景。
mahotas.thresholding.soft_threshold() 函式
mahotas.thresholding.soft_threshold() 函式接收灰度影像作為輸入,並返回已應用軟閾值的影像。它的工作原理是將畫素強度與提供的閾值進行比較。
語法
以下是Mahotas中soft_threshold()函式的基本語法:
mahotas.thresholding.soft_threshold(f, tval)
其中,
f - 輸入灰度影像。
tval - 閾值。
示例
在下面的示例中,我們使用mh.thresholding.soft_threshold()函式對灰度影像應用軟閾值。
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 image = mh.colors.rgb2gray(image) # Setting threshold value tval = 150 # Applying soft threshold on the image threshold_image = mh.thresholding.soft_threshold(image, tval) # 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(threshold_image, cmap='gray') axes[1].set_title('Soft Threshold Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
以下是上述程式碼的輸出:

使用均值進行軟閾值
我們可以使用影像上畫素強度的均值來應用軟閾值。均值是指影像的平均強度。
它是透過將所有畫素的強度值相加,然後除以畫素總數來計算的。
在Mahotas中,我們可以使用numpy.mean()函式找到影像所有畫素的平均畫素強度。然後,可以將均值傳遞給mahotas.thresholding.soft_threshold()函式的tval引數以生成軟閾值影像。
這種應用軟閾值的方法在降噪和影像質量之間保持了良好的平衡,因為閾值既不太高也不太低。
示例
下面的示例顯示了當閾值為畫素強度均值時,對灰度影像應用軟閾值。
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 image = mh.colors.rgb2gray(image) # Setting mean threshold value tval = np.mean(image) # Applying soft threshold on the image threshold_image = mh.thresholding.soft_threshold(image, tval) # 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(threshold_image, cmap='gray') axes[1].set_title('Soft Threshold Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
上述程式碼的輸出如下:

使用百分位數進行軟閾值
除了均值,我們還可以使用影像畫素強度的百分位數來應用軟閾值。百分位數是指低於該值的資料所佔的百分比;在影像處理中,它指的是影像中畫素強度的分佈。
例如,讓我們將閾值百分位數設定為85。這意味著只有強度大於影像中其他畫素的85%的畫素才會被分類為前景,而其餘畫素會被分類為背景。
在Mahotas中,我們可以使用numpy.percentile()函式根據畫素強度的百分位數設定閾值。然後,此值用於soft_thresholding()函式對影像應用軟閾值。
示例
在這個例子中,我們展示了當使用百分位數查詢閾值時如何應用軟閾值。
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 image = mh.colors.rgb2gray(image) # Setting percentile threshold value tval = np.percentile(image, 85) # Applying soft threshold on the image threshold_image = mh.thresholding.soft_threshold(image, tval) # 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(threshold_image, cmap='gray') axes[1].set_title('Soft Threshold Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
執行上述程式碼後,我們將得到以下輸出:
