
- 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 - 裁剪影像
裁剪影像指的是從影像中選擇和提取特定的感興趣區域,並丟棄其餘部分。它允許我們專注於影像中特定區域或物件,同時移除不相關或不需要的部分。
一般來說,要裁剪影像,您需要定義要保留區域的座標或尺寸。
在 Mahotas 中裁剪影像
要使用 Mahotas 裁剪影像,我們可以使用 NumPy 陣列切片操作來選擇影像的所需區域。我們需要定義所需 ROI 的座標或尺寸。這可以透過指定要裁剪區域的起點、寬度和高度來完成。
透過提取和隔離 ROI,我們可以僅分析、操作或顯示影像的相關部分。
示例
在以下示例中,我們將影像裁剪到所需的大小 -
import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread('sun.png') cropping= image[50:1250,40:340] # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title('Original Image') axes[0].axis('off') # Display the cropped image axes[1].imshow(cropping, cmap='gray') axes[1].set_title('Cropped Image') axes[1].axis('off') # Adjust the layout and display the plot plt.tight_layout() plt.show()
輸出
以下是上述程式碼的輸出 -

裁剪正方形區域
要在 mahotas 中裁剪正方形區域,我們需要確定起始和結束行以及列。以下是如何計算這些值的方法 -
步驟 1 - 找到影像的最小尺寸。
步驟 2 - 透過從總行數中減去最小尺寸並將結果除以 2 來計算起始行。
步驟 3 - 透過將起始行新增到最小尺寸來計算結束行。
步驟 4 - 使用類似的方法計算起始列。
步驟 5 - 透過將起始列新增到最小尺寸來計算結束列。
使用計算出的起始和結束行以及列,我們可以從影像中裁剪正方形區域。我們透過使用適當的行和列範圍對影像陣列進行索引來實現這一點。
示例
在這裡,我們嘗試在正方形區域中裁剪影像 -
import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread('tree.tiff') # Get the minimum dimension size = min(image.shape[:2]) # Calculating the center of the image center = tuple(map(lambda x: x // 2, image.shape[:2])) # Cropping a square region around the center crop = image[center[0] - size // 2:center[0] + size // 2, center[1] - size // 2:center[1] + size // 2] # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(10, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title('Original Image') axes[0].axis('off') # Display the cropped image axes[1].imshow(crop, cmap='gray') axes[1].set_title('Cropped Image') axes[1].axis('off') # Adjust the layout and display the plot plt.tight_layout() plt.show()
輸出
執行上述程式碼後,我們將獲得以下輸出 -

裁剪圓形區域
要在 mahotas 中將影像裁剪為圓形區域,我們需要確定圓心的座標和半徑。
我們可以透過將中心計算為影像尺寸的中點並將半徑設定為最小尺寸的一半來實現。
接下來,我們建立一個與影像尺寸相同的布林掩碼,其中 True 值表示圓形區域內的畫素。
我們透過計算每個畫素到中心的距離並在指定半徑內設定 True 來實現。
現在我們有了圓形掩碼,我們可以透過將圓形區域外的值設定為零將其應用於影像。最後,我們得到裁剪後的影像。
示例
現在,我們嘗試在圓形區域中裁剪影像 -
import mahotas as mh import numpy as np import matplotlib.pyplot as plt image = mh.imread('sun.png') # Calculating the center of the image center = tuple(map(lambda x: x // 2, image.shape[:2])) # Calculating the radius as half the minimum dimension radius = min(image.shape[:2]) // 2 # Creating a boolean mask of zeros mask = np.zeros(image.shape[:2], dtype=bool) # Creating meshgrid indices y, x = np.ogrid[:image.shape[0], :image.shape[1]] # Setting mask values within the circular region to True mask[(x - center[0])**2 + (y - center[1])**2 <= radius**2] = True crop = image.copy() # Setting values outside the circular region to zero crop[~mask] = 0 # Create a figure with subplots fig, axes = plt.subplots(1, 2, figsize=(7, 5)) # Display the original image axes[0].imshow(image) axes[0].set_title('Original Image') axes[0].axis('off') # Display the cropped image axes[1].imshow(crop, cmap='gray') axes[1].set_title('Cropped Image') axes[1].axis('off') # Adjust the layout and display the plot plt.tight_layout() plt.show()
輸出
獲得的輸出如下所示 -
