resized = cv2.resize(image, dim, interpolation=inter)
时间: 2024-04-26 10:24:50 浏览: 139
这也是一个使用OpenCV库来调整图像大小的函数,与之前提到的函数类似。它接受三个参数:image表示要调整大小的图像,dim表示调整后的大小,是一个包含宽度和高度的元组,interpolation表示插值方法,用于调整像素值。常用的插值方法包括cv2.INTER_AREA、cv2.INTER_LINEAR、cv2.INTER_CUBIC和cv2.INTER_NEAREST。这个函数同样可以用于图像处理、计算机视觉和机器学习等领域中。两个函数的区别在于参数传递的方式和默认值的不同。
相关问题
import cv2 import numpy as np # 读取原始图像 img = cv2.imread('image.webp') # 缩放变换 scale_percent = 50 # 缩放比例 width = int(img.shape[1] * scale_percent / 100) # 新图像宽度 height = int(img.shape[0] * scale_percent / 100) # 新图像高度 dim = (width, height) resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA) # 旋转变换 angle = 45 # 旋转角度 rotation_matrix = cv2.getRotationMatrix2D((img.shape[1]/2, img.shape[0]/2), angle, 1) rotated = cv2.warpAffine(img, rotation_matrix, (img.shape[1], img.shape[0])) # 平移变换 x = 50 # 水平方向平移像素数 y = 100 # 垂直方向平移像素数 translation_matrix = np.float32([[1, 0, x], [0, 1, y]]) translated = cv2.warpAffine(img, translation_matrix, (img.shape[1], img.shape[0])) # 裁剪变换 x1 = 100 # 左上角横坐标 y1 = 100 # 左上角纵坐标 x2 = 300 # 右下角横坐标 y2 = 300 # 右下角纵坐标 cropped = img[y1:y2, x1:x2] # 镜像变换 mirrored = cv2.flip(img, 1) # 在同一个窗口内分成六个子窗口来分别显示原图和变换后的图像 cv2.namedWindow('Image', cv2.WINDOW_NORMAL) cv2.resizeWindow('Image', 700, 700) cv2.imshow('Image', np.vstack((img, resized, rotated, translated, cropped, mirrored))) cv2.waitKey(0) cv2.destroyAllWindows()优化这段代码,并显示结果图
可以将这段代码封装成一个函数,并将变换操作作为参数传递给函数,以避免代码重复。以下是一个示例:
```python
import cv2
import numpy as np
def apply_transformations(img, transformations):
transformed_images = []
for transformation in transformations:
if transformation == 'resize':
scale_percent = 50
width = int(img.shape[1] * scale_percent / 100)
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
transformed_images.append(resized)
elif transformation == 'rotate':
angle = 45
rotation_matrix = cv2.getRotationMatrix2D((img.shape[1]/2, img.shape[0]/2), angle, 1)
rotated = cv2.warpAffine(img, rotation_matrix, (img.shape[1], img.shape[0]))
transformed_images.append(rotated)
elif transformation == 'translate':
x = 50
y = 100
translation_matrix = np.float32([[1, 0, x], [0, 1, y]])
translated = cv2.warpAffine(img, translation_matrix, (img.shape[1], img.shape[0]))
transformed_images.append(translated)
elif transformation == 'crop':
x1 = 100
y1 = 100
x2 = 300
y2 = 300
cropped = img[y1:y2, x1:x2]
transformed_images.append(cropped)
elif transformation == 'mirror':
mirrored = cv2.flip(img, 1)
transformed_images.append(mirrored)
return transformed_images
# 读取原始图像
img = cv2.imread('image.webp')
# 定义要应用的变换
transformations = ['resize', 'rotate', 'translate', 'crop', 'mirror']
# 应用变换并显示结果图像
transformed_images = apply_transformations(img, transformations)
cv2.namedWindow('Transformed Images', cv2.WINDOW_NORMAL)
cv2.resizeWindow('Transformed Images', 1200, 800)
cv2.imshow('Transformed Images', np.hstack(transformed_images))
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这个函数将原始图像和要应用的变换作为参数,返回一个包含所有变换后图像的列表。通过调用这个函数并将返回的结果合并成一张图像,就可以显示所有变换后的图像。
cv2.resize(img, dim, interpolation=cv2.INTER_AREA)
这是一个OpenCV的函数,用于调整图像大小。其中,img是要调整大小的图像,dim是一个元组,表示调整后的图像大小,interpolation是插值方法。cv2.INTER_AREA是一种插值方法,用于缩小图像。以下是一个例子:
```python
import cv2
# 读取图像
img = cv2.imread('image.jpg')
# 调整图像大小
resized = cv2.resize(img, (500, 500), interpolation=cv2.INTER_AREA)
# 显示原始图像和调整后的图像
cv2.imshow('Original Image', img)
cv2.imshow('Resized Image', resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
```
这个例子将读取名为'image.jpg'的图像,将其大小调整为500x500像素,并使用cv2.INTER_AREA插值方法进行缩小。最后,它将显示原始图像和调整后的图像。
阅读全文
相关推荐
















