Mahotas 简明教程
Mahotas - Cropping an Image
图像裁剪是指从图像中选择和提取一个特定的感兴趣区域,然后丢弃其余部分。它使我们可以专注于图像中的特定区域或对象,同时删除无关或不需要的部分。
Cropping an image refers to selecting and extracting a specific region of interest from an image and discarding the rest. It allows us to focus on a particular area or object within an image while removing irrelevant or unwanted portions.
要裁剪一般图像,您需要定义要保留区域的坐标或维度。
To crop an image in general, you need to define the coordinates or dimensions of the region you want to keep.
Cropping an Image in Mahotas
要使用 Mahotas 裁剪图像,我们可以使用 NumPy 数组切片操作来选择图像的所需区域。我们需要定义所需 ROI 的坐标或维度。这可以通过指定要裁剪区域的起始点、宽度和高度来完成。
To crop an image using Mahotas, we can use NumPy array slicing operation to select the desired region of the image. We need to define the coordinates or dimensions of the desired ROI. This can be done by specifying the starting point, width, and height of the region to be cropped.
通过提取和隔离 ROI,我们只能分析、操作或显示图像的相关部分。
By extracting and isolating the ROI, we can analyze, manipulate, or display only the relevant part of the image.
Example
在以下示例中,我们将图像裁剪为所需的大小 −
In the following example, we are cropping the image to the desired size −
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()
以下是上述代码的输出 −
Following is an output of the above code −
Cropping a Square Region
要在 mahotas 中裁剪一个正方形区域,我们需要确定起始行和结束行以及起始列和结束列。以下是如何计算这些值的方法 −
To crop a square region in mahotas, we need to determine the starting and ending rows and columns. Here is an approach to calculate these values −
Step 1 −查找图像的最小尺寸。
Step 1 − Find the minimum dimension of the image.
Step 2 −通过将总行数减去最小尺寸并除以 2 来计算起始行。
Step 2 − Compute the starting row by subtracting the minimum dimension from the total number of rows and dividing the result by 2.
Step 3 −通过将起始行与最小尺寸相加来计算结束行。
Step 3 − Calculate the ending row by adding the starting row to the minimum dimension.
Step 4 −使用类似方法计算起始列。
Step 4 − Compute the starting column using a similar approach.
Step 5 −通过将起始列与最小尺寸相加来计算结束列。
Step 5 − Calculate the ending column by adding the starting column to the minimum dimension.
使用计算出的起始行、结束行、起始列和结束列,我们可以从图像中裁剪正方形区域。我们可以通过使用适当的行和列范围来对图像数组进行索引来实现这一点。
Using the calculated starting and ending rows and columns, we can crop the square region from the image. We accomplish this by indexing the image array with the appropriate row and column ranges.
Example
这里,我们尝试在正方形区域中裁剪图像 −
Here, we are trying to crop an image in a square region −
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()
执行上面的代码后,我们得到以下输出: -
After executing the above code, we get the following output −
Cropping a Circular Region
要在 mahotas 中将图像裁剪成圆形区域,我们需要确定圆心的坐标和半径。
To crop the image to a circular region in mahotas, we need to determine the center coordinates and radius of the circle.
我们可以通过将中心计算为图像尺寸的中点,并将半径设置为最小尺寸的一半来实现。
We can achieve this by calculating the center as the midpoint of the image dimensions and setting the radius as half the minimum dimension.
接下来,我们创建一个与图像尺寸相同的布尔掩码,其中 True 值表示圆形区域内的像素。
Next, we create a boolean mask with the same dimensions as the image, where True values indicate the pixels within the circular region.
我们可以通过计算每个像素到中心的距离,并为落在指定半径内的像素设置 True 来实现此目的。
We accomplish this by calculating the distance of each pixel from the center and setting True for pixels that fall within the specified radius.
现在我们有了圆形掩码,可以通过将圆形区域外的值设置为零来将其应用于图像。最后,我们得到裁剪的图像。
Now that we have the circular mask, we can apply it to the image by setting the values outside the circular region to zero. Finally, we get the cropped image.
Example
现在,我们尝试在圆形区域中裁剪图像 −
Now, we are trying to crop an image in a circular region −
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()
获得的输出如下所示 −
The output obtained is as shown below −