Mahotas 简明教程

Mahotas - Cropping an Image

图像裁剪是指从图像中选择和提取一个特定的感兴趣区域,然后丢弃其余部分。它使我们可以专注于图像中的特定区域或对象,同时删除无关或不需要的部分。

要裁剪一般图像,您需要定义要保留区域的坐标或维度。

Cropping an Image in Mahotas

要使用 Mahotas 裁剪图像,我们可以使用 NumPy 数组切片操作来选择图像的所需区域。我们需要定义所需 ROI 的坐标或维度。这可以通过指定要裁剪区域的起始点、宽度和高度来完成。

通过提取和隔离 ROI,我们只能分析、操作或显示图像的相关部分。

Example

在以下示例中,我们将图像裁剪为所需的大小 −

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()

以下是上述代码的输出 −

cropping image

Cropping a Square Region

要在 mahotas 中裁剪一个正方形区域,我们需要确定起始行和结束行以及起始列和结束列。以下是如何计算这些值的方法 −

Step 1 −查找图像的最小尺寸。

Step 2 −通过将总行数减去最小尺寸并除以 2 来计算起始行。

Step 3 −通过将起始行与最小尺寸相加来计算结束行。

Step 4 −使用类似方法计算起始列。

Step 5 −通过将起始列与最小尺寸相加来计算结束列。

使用计算出的起始行、结束行、起始列和结束列,我们可以从图像中裁剪正方形区域。我们可以通过使用适当的行和列范围来对图像数组进行索引来实现这一点。

Example

这里,我们尝试在正方形区域中裁剪图像 −

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()

执行上面的代码后,我们得到以下输出: -

cropping square region

Cropping a Circular Region

要在 mahotas 中将图像裁剪成圆形区域,我们需要确定圆心的坐标和半径。

我们可以通过将中心计算为图像尺寸的中点,并将半径设置为最小尺寸的一半来实现。

接下来,我们创建一个与图像尺寸相同的布尔掩码,其中 True 值表示圆形区域内的像素。

我们可以通过计算每个像素到中心的距离,并为落在指定半径内的像素设置 True 来实现此目的。

现在我们有了圆形掩码,可以通过将圆形区域外的值设置为零来将其应用于图像。最后,我们得到裁剪的图像。

Example

现在,我们尝试在圆形区域中裁剪图像 −

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()

获得的输出如下所示 −

cropping circular region