Mahotas 简明教程

Mahotas - Resizing an Image

当我们提到调整图像大小时,我们的意思是更改图像的维度(宽度和高度),同时保持其宽高比。宽高比是指图像的宽度与高度的比例。调整大小可以使图像变大或变小。

When we refer to resizing an image, we mean changing the dimensions (width and height) of the image while maintaining its aspect ratio. Aspect ratio refers to the ratio of the width to the height of the image. Resizing can be done to either make the image larger or smaller.

当您调整图像大小时,您正在改变图像中的像素数量,并可能更改内容的可视表示。

When you resize an image, you are altering the number of pixels in the image and potentially changing the visual representation of the content.

Resizing an Image in Mahotas

要在 Mahotas 中调整图像大小,我们可以使用该库提供的 imresize() 函数。

To resize an image in Mahotas, we can use the imresize() function provided by the library.

此函数使用插值算法调整图像大小,并将调整后的大小作为新的 NumPy 数组返回。

This function resizes the image using an interpolation algorithm and returns the resized image as a new NumPy array.

插值算法是在调整图像大小或变换图像时填补已知像素值之间的空白所使用的方法。它通过考虑相邻像素的值来估计缺少的像素值。

The interpolation algorithm is the method used to fill in the gaps between known pixel values when resizing or transforming an image. It estimates the missing pixel values by considering the values of neighboring pixels.

插值有助于创建像素之间的平滑过渡,从而产生连续的图像。

Interpolation helps in creating a smooth transition between pixels, resulting in a continuous image.

The imresiz() function

Mahotas 中的 imresize() 函数有两个参数 - 要调整大小的图像和作为元组(new_height, new_width)的目标大小。它在保持宽高比的同时调整图像大小,并将调整后的大小作为新的 NumPy 数组返回。

The imresize() function in Mahotas takes two arguments − the image to be resized and the target size as a tuple (new_height, new_width). It resizes the image while maintaining the aspect ratio and returns the resized image as a new NumPy array.

Syntax

以下是在 mahotas 中 imresize() 函数的基本语法 −

Following is the basic syntax of the imresize() function in mahotas −

mahotas.imresize(image, nsize, order=3)

其中,

Where,

  1. image − It is input image that you want to resize.

  2. nsize − It specifies the desired size of the output image. It should be a tuple (height, width) representing the target dimensions.

  3. order (optional) − It determines the interpolation order to beused during resizing. It has a default value of 3, which corresponds to bicubic interpolation. You can also choose other interpolation orders such as 0 (nearest−neighbor), 1 (bilinear), or 2 (quadratic).

Example

在以下示例中,我们尝试使用 imresize() 函数将图像调整为特定的宽度和高度 −

In the following example, we are trying to resize an image to a specific width and height using the imresize() function −

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

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

After executing the above code, we get the following output −

Size before resizing  : 1079040
shape before resizing : (1280, 843)
Size after resizing   : 10000
shape after resizing  : (100, 100)

获得的图像如下所示:

The image obtained is as shown below −

resizing image

Using Bilinear Interpolation

双线性插值是一种通常用于图像调整大小的插值算法。它通过考虑四个最近邻近像素的加权平均值来估计新的像素值。

Bilinear interpolation is an interpolation algorithm commonly used for image resizing. It estimates the new pixel values by considering the weighted average of the four nearest neighboring pixels.

这四个像素在目标像素周围形成一个正方形,并且它们的值有助于确定新的像素值。

These four pixels form a square around the target pixel, and their values contribute to determining the new pixel value.

要在 mahotas 中使用双线性插值调整图像大小,我们需要在 imresize() 函数中将插值顺序指定为 1。

To resize an image using the bilinear interpolation in mahotas, we need to specify the interpolation order as 1 in the imresize() function.

Example

在此处,我们尝试使用双线性插值调整图像大小 −

In here, we are trying to resize an image using the bilinear interpolation −

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)

获得的输出如下 −

The output obtained is as follows −

[[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

二次插值也是一种常用的图像调整大小插值算法。它通过考虑附近像素的加权平均值来估计新的像素值。

Quadratic interpolation is also an interpolation algorithm commonly used for image resizing. It estimates the new pixel values by considering the weighted average of nearby pixels.

在处理曲线或非线性数据时,二次插值尤其有用。

Quadratic interpolation is particularly useful when working with curved or non−linear data.

简单来说,它就是通过三个相邻像素值拟合一条抛物线,来近似它们之间想要的某个位置上的值。

In simple terms, it involves fitting a parabolic curve through three neighboring pixel values to approximate the value at a desired position between them.

要在 mahotas 中使用二次插值调整图像大小,我们需要在 imresize() 函数中将插值顺序指定为 2。

To resize an image using the quadratic interpolation in mahotas, we need to specify the interpolation order as 2 in the imresize() function.

Example

现在,我们尝试在 mahotas 中使用二次插值调整图像大小 −

Now, we are trying to resize an image using the quadratic interpolation in 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)

以下是上面代码的输出: -

Following is the output of the above code −

[[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. ]]