Mahotas - 影像縮放



當我們提到調整影像大小的時候,指的是在保持縱橫比的同時改變影像的尺寸(寬度和高度)。縱橫比指的是影像寬度與高度的比率。調整大小可以使影像變大或變小。

當您調整影像大小時,您正在更改影像中的畫素數量,並可能更改內容的視覺表示。

在 Mahotas 中調整影像大小

要在 Mahotas 中調整影像大小,我們可以使用庫提供的 imresize() 函式。

此函式使用插值演算法調整影像大小,並將調整大小後的影像作為新的 NumPy 陣列返回。

插值演算法是在調整影像大小或變換影像時用於填充已知畫素值之間間隙的方法。它透過考慮相鄰畫素的值來估計缺失的畫素值。

插值有助於在畫素之間建立平滑過渡,從而產生連續的影像。

imresiz() 函式

Mahotas 中的 imresize() 函式接受兩個引數 - 要調整大小的影像和目標大小作為元組 (new_height, new_width)。它在保持縱橫比的同時調整影像大小,並將調整大小後的影像作為新的 NumPy 陣列返回。

語法

以下是 mahotas 中 imresize() 函式的基本語法 -

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

其中,

  • image − 它是您要調整大小的輸入影像。

  • nsize − 它指定輸出影像的所需大小。它應該是一個元組 (height, width),表示目標尺寸。

  • order (可選) − 它確定在調整大小期間使用的插值順序。它的預設值為 3,對應於雙三次插值。

    您還可以選擇其他插值順序,例如 0(最近鄰)、1(雙線性)或 2(二次)。

示例

在以下示例中,我們嘗試使用 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)

獲得的影像如下所示 -

Resizing Image

使用雙線性插值

雙線性插值是一種常用用於影像縮放的插值演算法。它透過考慮四個最近鄰畫素的加權平均值來估計新的畫素值。

這四個畫素圍繞目標畫素形成一個正方形,它們的值有助於確定新的畫素值。

要在 mahotas 中使用雙線性插值調整影像大小,我們需要在 imresize() 函式中將插值順序指定為 1。

示例

在這裡,我們嘗試使用雙線性插值調整影像大小 -

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

使用二次插值

二次插值也是一種常用用於影像縮放的插值演算法。它透過考慮附近畫素的加權平均值來估計新的畫素值。

二次插值在處理曲線或非線性資料時特別有用。

簡單來說,它涉及透過三個相鄰畫素值擬合拋物線,以近似它們之間所需位置的值。

要在 mahotas 中使用二次插值調整影像大小,我們需要在 imresize() 函式中將插值順序指定為 2。

示例

現在,我們嘗試在 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. ]]
廣告