
- 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 - 擊中與錯過變換
擊中與錯過變換是一種二進位制形態學運算,用於檢測影像中的特定模式或形狀。
該運算將結構元素與輸入二值影像進行比較。結構元素由以特定模式排列的前景 (1) 和背景 (0) 畫素組成,這些畫素代表要檢測的所需形狀或模式。
擊中或錯過變換在結構元素和影像之間執行逐畫素邏輯與運算,然後檢查結果是否與預定義條件匹配。
該條件指定匹配模式中應該存在的特定前景和背景畫素排列。如果滿足該條件,則輸出畫素設定為 1,表示匹配;否則,設定為 0。
Mahotas 中的擊中與錯過變換
在 Mahotas 中,我們可以使用mahotas.hitmiss()函式對影像執行擊中與錯過變換。該函式使用結構元素'Bc'來確定輸入影像中是否存在特定模式。
Mahotas 中的結構元素可以取三個值:0、1 或 2。值為 1 表示結構元素的前景,而 0 表示背景。
值 2 用作“不關心”值,這意味著不應對該特定畫素執行匹配。
為了識別匹配,結構元素的值必須與輸入影像中相應的畫素值重疊。
如果重疊滿足結構元素指定的條件,則該畫素被視為匹配。
mahotas.hitmiss() 函式
mahotas.hitmiss() 以灰度影像作為輸入,並返回二值影像作為輸出。白色畫素表示結構元素與輸入影像匹配的區域,而黑色畫素表示不匹配的區域。
語法
以下是 mahotas 中 hitmiss() 函式的基本語法:
mahotas.hitmiss(input, Bc, out=np.zeros_like(input))
其中,
input - 輸入灰度影像。
Bc - 需要在輸入影像中匹配的模式。它可以是 0、1 或 2。
out(可選) - 定義在哪個陣列中儲存輸出影像(預設為與輸入相同大小)。
示例
以下示例演示了使用 mh.hitmiss() 函式對影像進行擊中與錯過變換。
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) # Applying thresholding threshold_image = mh.thresholding.bernsen(image, 5, 200) # Creating hit & miss template template = np.array([[1, 2, 1, 2, 1],[2, 1, 1, 1, 2],[2, 2, 1, 2, 2]]) # Applying hit & miss transformation hit_miss = mh.hitmiss(threshold_image, template) # 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 hit & miss transformed image axes[1].imshow(hit_miss, cmap='gray') axes[1].set_title('Hit & Miss Transformed Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
以下是上述程式碼的輸出:

透過檢測邊緣
我們還可以透過應用擊中與錯過變換來檢測影像的邊緣。邊緣表示影像中不同區域之間的邊界。
這些是相鄰畫素之間強度值差異較大的區域。
在 mahotas 中,要使用擊中與錯過變換檢測邊緣,我們首先建立一個結構元素。此結構元素將模板的邊緣與輸入影像進行匹配。
然後,我們對影像執行閾值化,然後將結構元素作為Bc引數傳遞給 hitmiss() 函式。
例如,以下結構元素可用於檢測輸入影像中的邊緣:
[[1, 2, 1] [2, 2, 2] [1, 2, 1]]
在此,1 位於結構元素的右上角、左上角、右下角和左下角位置。邊緣通常位於影像中的這些位置。
結構元素中的 1 與影像中強度值為 1 的畫素匹配,從而突出顯示邊緣作為前景。
示例
在此示例中,我們嘗試透過應用擊中與錯過變換來檢測影像的邊緣:
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) # Applying thresholding threshold_value = mh.thresholding.rc(image) threshold_image = image > threshold_value # Creating hit & miss template template = np.array([[1, 2, 1],[2, 2, 2],[1, 2, 1]]) # Applying hit & miss transformation hit_miss = mh.hitmiss(threshold_image, template) # 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 hit & miss transformed image axes[1].imshow(hit_miss, cmap='gray') axes[1].set_title('Hit & Miss Transformed Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
上述程式碼的輸出如下:

透過檢測對角線
我們也可以使用擊中與錯過變換來檢測影像的對角線。對角線由連線影像相對角的線性圖案指示。
這些是畫素強度沿對角線路徑變化的區域。
在 mahotas 中,我們首先對輸入影像執行閾值化。然後,我們將結構元素作為Bc引數傳遞給 hitmiss() 函式。此結構元素將模板的對角線與輸入影像的對角線進行匹配。
例如,以下結構元素可用於檢測輸入影像中的對角線:
[[0, 2, 0] [2, 0, 2] [0, 2, 0]]
在此,0 沿從左上角到右下角的對角線路徑執行,以及從右上角到左下角的對角線路徑執行。對角線通常位於影像中的這些位置。
結構元素中的 0 與影像中強度值為 0 的畫素匹配,從而突出顯示對角線作為背景。
示例
在這裡,我們嘗試使用擊中與錯過變換來檢測影像的對角線:
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) # Applying thresholding threshold_image = mh.thresholding.bernsen(image, 10, 10) # Creating hit & miss template template = np.array([[0, 2, 0],[2, 0, 2],[0, 2, 0]]) # Applying hit & miss transformation hit_miss = mh.hitmiss(threshold_image, template) # 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 hit & miss transformed image axes[1].imshow(hit_miss, cmap='gray') axes[1].set_title('Hit & Miss Transformed Image') axes[1].set_axis_off() # Adjusting spacing between subplots mtplt.tight_layout() # Showing the figures mtplt.show()
輸出
執行上述程式碼後,我們將獲得以下輸出:
