Mahotas 简明教程
Mahotas - Resizing an Image
当我们提到调整图像大小时,我们的意思是更改图像的维度(宽度和高度),同时保持其宽高比。宽高比是指图像的宽度与高度的比例。调整大小可以使图像变大或变小。
当您调整图像大小时,您正在改变图像中的像素数量,并可能更改内容的可视表示。
Resizing an Image in Mahotas
要在 Mahotas 中调整图像大小,我们可以使用该库提供的 imresize() 函数。
此函数使用插值算法调整图像大小,并将调整后的大小作为新的 NumPy 数组返回。
插值算法是在调整图像大小或变换图像时填补已知像素值之间的空白所使用的方法。它通过考虑相邻像素的值来估计缺少的像素值。
插值有助于创建像素之间的平滑过渡,从而产生连续的图像。
The imresiz() function
Mahotas 中的 imresize() 函数有两个参数 - 要调整大小的图像和作为元组(new_height, new_width)的目标大小。它在保持宽高比的同时调整图像大小,并将调整后的大小作为新的 NumPy 数组返回。
Syntax
以下是在 mahotas 中 imresize() 函数的基本语法 −
mahotas.imresize(image, nsize, order=3)
其中,
-
image - 它是要调整大小的输入图像。
-
nsize - 它指定输出图像的所需大小。它应为表示目标尺寸的元组(高度,宽度)。
-
order (optional) - 它确定调整大小期间使用的插值顺序。它的默认值为 3,对应于双三次插值。您还可以选择其他插值顺序,例如 0(最近邻居)、1(双线性)或 2(二次)。
Example
在以下示例中,我们尝试使用 imresize() 函数将图像调整为特定的宽度和高度 −
import mahotas as mh
import numpy as np
import matplotlib.pyplot as plt
image = mh.imread('sun.png', as_grey = True)
print('Size before resizing :'+' '+str(image.size))
print('shape before resizing :'+' '+str(image.shape))
resize=mh.imresize(image,[100,100])
print('Size after resizing :'+' '+str(resize.size))
print('shape after resizing :'+' '+str(resize.shape))
# 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 resized image
axes[1].imshow(resize, cmap='gray')
axes[1].set_title('Resized Image')
axes[1].axis('off')
# Adjust the layout and display the plot
plt.tight_layout()
plt.show()
执行上面的代码后,我们得到以下输出: -
Size before resizing : 1079040
shape before resizing : (1280, 843)
Size after resizing : 10000
shape after resizing : (100, 100)
获得的图像如下所示:
Using Bilinear Interpolation
双线性插值是一种通常用于图像调整大小的插值算法。它通过考虑四个最近邻近像素的加权平均值来估计新的像素值。
这四个像素在目标像素周围形成一个正方形,并且它们的值有助于确定新的像素值。
要在 mahotas 中使用双线性插值调整图像大小,我们需要在 imresize() 函数中将插值顺序指定为 1。
Example
在此处,我们尝试使用双线性插值调整图像大小 −
import mahotas as mh
image = mh.imread("nature.jpeg", as_grey = True)
# Specifying the desired width and height
new_width = 800
new_height = 600
# Resizing the image using nearest-neighbor interpolation
resized_image = mh.imresize(image, [new_height, new_width], 1)
print(resized_image)
获得的输出如下 −
[[193.71 193.71 193.71 ... 208.17 208.17 0. ]
[193.71 193.71 193.71 ... 208.17 208.17 0. ]
[193.71 193.71 193.71 ... 208.17 208.17 0. ]
...
[ 98.49 98.49 95.49 ... 7.11 4.85 0. ]
[ 90.05 90.05 94.12 ... 5.33 5.07 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]]
Using Quadratic Interpolation
二次插值也是一种常用的图像调整大小插值算法。它通过考虑附近像素的加权平均值来估计新的像素值。
在处理曲线或非线性数据时,二次插值尤其有用。
简单来说,它就是通过三个相邻像素值拟合一条抛物线,来近似它们之间想要的某个位置上的值。
要在 mahotas 中使用二次插值调整图像大小,我们需要在 imresize() 函数中将插值顺序指定为 2。
Example
现在,我们尝试在 mahotas 中使用二次插值调整图像大小 −
import mahotas as mh
image = mh.imread("nature.jpeg", as_grey = True)
# Resizing the image using nearest-neighbor interpolation
resized_image = mh.imresize(image, [700, 550], 2)
print(resized_image)
以下是上面代码的输出: -
[[193.71 193.71 193.71 ... 208.17 208.17 0. ]
[193.71 193.71 193.71 ... 208.17 208.17 0. ]
[193.71 193.71 193.71 ... 208.17 208.17 0. ]
...
[ 92.2 93.49 94.12 ... 6.22 6.22 0. ]
[ 92.27 98.05 92.42 ... 6.33 4.85 0. ]
[ 0. 0. 0. ... 0. 0. 0. ]]