📅  最后修改于: 2021-01-07 06:33:20             🧑  作者: Mango
有时,有必要转换加载的图像。在图像处理中,我们需要调整图像大小以执行特定操作。图像通常存储在Numpy ndarray(array)中。 ndarray.shape用于获取图像的尺寸。我们可以通过使用维度变量的索引来获取每个像素的通道的宽度,高度和数量。
import cv2
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1)
scale = 60
width = int(img.shape[1] * scale / 100)
height = int(img.shape[0] * scale / 100)
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
print('Resized Dimensions : ', resized.shape)
cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
Resized Dimensions : (199, 300, 3)
调整图像大小意味着更改图像的尺寸,宽度或高度以及两者。而且,可以通过调整图像的大小来保持原始图像的纵横比。 OpenCV提供cv2.resize()函数来调整图像大小。语法为:
cv2.resize(src, dsize[, dst[, fx[,fy[,interpolation]]])
有几种调整图像大小的方法。以下是执行调整大小操作的一些示例:
保持宽高比
import cv2
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1)
print('Original Dimensions : ', img.shape)
scale = 60 # percent of original size
width = int(img.shape[1] * scale / 100)
height = int(img.shape[0] * scale / 100)
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
print('Resized Dimensions : ', resized.shape)
cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
Original Dimensions : (332, 500, 3)
Resized Dimensions : (199, 300, 3)
在上面的示例中,scale_per变量保存需要缩放的图像的百分比。值<100用于缩小提供的图像。我们将使用此scale_per值以及原始图像的尺寸来计算输出图像的宽度和高度。
使用resize()升级
import cv2
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1)
print('Original Dimensions : ', img.shape)
scale = 150 # percent of original size
width = int(img.shape[1] * scale / 100)
height = int(img.shape[0] * scale / 100)
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
print('Resized Dimensions : ', resized.shape)
cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
Original Dimensions : (332, 500, 3)
Resized Dimensions : (398, 600, 3)
在下面的示例中,我们以像素为单位提供了宽度的特定值,而高度将保持不变。
import cv2
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', cv2.IMREAD_UNCHANGED)
print('Original Dimensions : ', img.shape)
width = img.shape[1] # keep original width
height = 440
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
print('Resized Dimensions : ', resized.shape)
cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
Original Dimensions : (332, 500, 3)
Resized Dimensions : (440, 500, 3)
在下面的示例中, scale_per值包含必须缩放高度的百分比,或者我们可以提供以像素为单位的特定值。
import cv2
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1)
print('Original Dimensions : ', img.shape)
width = img.shape[1] # keep original width
height = 200
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
print('Resized Dimensions : ', resized.shape)
cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出:
Original Dimensions : (332, 500, 3)
Resized Dimensions : (200, 500, 3)
调整特定的宽度和高度
import cv2
img = cv2.imread(r'C:\Users\DEVANSH SHARMA\cat.jpeg', 1)
print('Original Dimensions : ', img.shape)
width = 350
height = 450
dim = (width, height)
# resize image
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
print('Resized Dimensions : ', resized.shape)
cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
输出: