
- 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 到 Sepia 轉換
- 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 - Haralic 特徵
- 標記區域的權重
- Mahotas - Zernike 特徵
- Mahotas - Zernike 矩
- Mahotas - 排序濾波器
- Mahotas - 2D 拉普拉斯濾波器
- Mahotas - 多數濾波器
- Mahotas - 均值濾波器
- Mahotas - 中值濾波器
- Mahotas - Otsu 方法
- Mahotas - 高斯濾波
- Mahotas - 擊中與不擊中變換
- Mahotas - 標記最大陣列
- Mahotas - 影像的平均值
- Mahotas - SURF 密集點
- Mahotas - SURF 積分
- Mahotas - Haar 變換
- 突出影像最大值
- 計算線性二值模式
- 獲取標籤的邊界
- 反轉 Haar 變換
- Riddler-Calvard 方法
- 標記區域的大小
- Mahotas - 模板匹配
- 加速魯棒特徵
- 移除邊界標記
- Mahotas - Daubechies 小波
- Mahotas - Sobel 邊緣檢測
Mahotas - 設定閾值
設定閾值是指為影像定義一個閾值,以執行影像閾值化。影像閾值化是將灰度影像轉換為二值影像的過程,其中畫素被分為兩類:前景或背景。
強度值高於閾值的畫素被分配到前景,而低於閾值的畫素被分配到背景類。
閾值範圍從 0 到 255,其中值為 0 產生僅包含前景(白色)的影像,值為 255 產生僅包含背景(黑色)的影像。
在 Mahotas 中設定閾值
在 Mahotas 中,我們可以使用 **numpy.mean()** 函式設定影像的閾值。
此函式將灰度影像作為輸入,並計算其畫素的平均強度值。
然後將平均值設定為閾值。任何強度超過閾值的畫素都被分類為前景,而強度低於閾值的畫素則被分類為背景。
**注意** - Mahotas 沒有提供直接設定閾值的方法,但是可以透過將 mahotas 與 numpy 結合使用來實現。
語法
以下是 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') # Converting it to grayscale image = mh.colors.rgb2gray(image) # Calculating mean intensity value mean_value = np.mean(image) # Creating threshold image threshold_image = image > mean_value # 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('Threshold Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
以下是上述程式碼的輸出:

設定反向閾值
我們還可以為影像設定反向閾值。在反向閾值中,強度大於閾值的畫素被分類為背景,而強度小於閾值的畫素被分類為前景。
在 mahotas 中,反向閾值化可以分兩步完成。第一步是使用 numpy.mean() 函式計算影像的閾值。
第二步是使用小於運算子 (<) 而不是正常閾值化中使用的大於運算子 (>),將影像的畫素強度與平均閾值進行比較。
示例
以下示例顯示瞭如何從灰度影像建立反向閾值影像。
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) # Calculating mean intensity value mean_value = np.mean(image) # Creating inverse threshold image threshold_image = image < mean_value # 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('Threshold Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
上述程式碼的輸出如下:

設定自定義閾值
設定閾值的另一種方法是為影像設定自定義閾值。它是一個完全基於影像決定的隨機數。
自定義閾值是一個任意數字,不是使用任何數學公式計算的。這就是為什麼不應將自定義值用作閾值的原因。
另一個原因是,自定義值可能會產生噪聲明顯更大的影像。
在 mahotas 中,我們可以分配一個任意數字來設定自定義閾值。然後,我們可以使用此值並將影像的畫素與其進行比較以生成閾值影像。
**注意** - 將閾值設定為 0 或 255 將導致最終影像僅包含前景畫素或背景畫素。
示例
在這裡,我們設定了一個任意數字作為閾值。
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) # Setting threshold value threshold_value = 200 # Creating threshold image threshold_image = image > threshold_value # 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('Threshold Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
執行上述程式碼後,我們得到以下輸出:
