缩放是对图像的大小进行调整,即使图像放大或缩小

cv2.resize(src, dsize, dst=None, fx=None, fy=None, interpolation=None)
  • src :输入图像
  • dsize:绝对尺寸,直接指定调整后图像大小
  • fx, fy:相对尺寸,将dsize设置为None,然后将fx和fy设置为比例因子即可
  • interpolation:插值方法。

插值

含义

cv2.INTER_LINEAR

双线性插值法

cv2.INTER_NEAREST

最近邻插值

cv2.INTER_AREA

像素区域采样(默认)

cv2.INTER_CUBIC

双三次插值

import cv2 as cv
import matplotlib.pyplot as plt

# 1.读取图片
img1 = cv.imread("1.png")

# 2.图像缩放
# 2.1 绝对尺寸
print(img1.shape)
rows, cols = img1.shape[:2] # 将图片的行赋值给rows, 图片的列赋值给cols
print(rows) # 打印行值
print(cols) # 打印列值
res = cv.resize(img1, (2 * cols, 2 * rows), interpolation=cv.INTER_CUBIC)
print(res.shape) # 打印放大后的数值

# 2.2 相对尺寸
res1 = cv.resize(img1, None, fx=0.5, fy=0.5)
print(res1.shape) # 打印缩小后的数值

# 3.图像显示
# 3.1使用opencv显示图像
cv.imshow("orignal", img1)
cv.imshow("enlarge", res)
cv.imshow("shrink", res1)
cv.waitKey(0)

# 3.2 使用matplotlib显示图像
fig, axes = plt.subplots(nrows=1, ncols=3, figsize=(10, 8), dpi=100)
axes[0].imshow(res[:, :, ::-1])
axes[0].set_title("绝对尺度 (放大)")
axes[1].imshow(img1[:, :, ::-1])
axes[1].set_title("原图")
axes[2].imshow(res1[:, :, ::-1])
axes[2].set_title("相对尺度 (缩小)")
plt.show()

OpenCV 图像缩放_显示图像