cv2.resize()用法
时间: 2023-07-12 15:48:35 浏览: 96
cv2.resize() 是 OpenCV 中的一个函数,用于改变图像的大小。
语法:
cv2.resize(src, dsize[, dst[, fx[, fy[, interpolation]]]])
参数:
src - 原始图像
dsize - 目标图像的大小,格式为(宽度,高度)
dst - 用于存储结果的图像
fx - 水平缩放因子
fy - 垂直缩放因子
interpolation - 插值方法,常用的有cv2.INTER_LINEAR, cv2.INTER_NEAREST, cv2.INTER_AREA 等。
例子:
```python
import cv2
# Load an image
img = cv2.imread("example.jpg")
# Resize the image to half of its original size
resized_img = cv2.resize(img, (img.shape[1]//2, img.shape[0]//2), interpolation = cv2.INTER_LINEAR)
# Show the image
cv2.imshow("Resized image", resized_img)
cv2.waitKey(0)
```
在这个例子中,我们将图像的大小缩小一半并使用 cv2.INTER_LINEAR 插值方法。
相关问题
img = cv2.resize(img, (1000, 1000))
这段代码使用 `cv2.resize()` 函数将图像 `img` 的大小调整为 1000x1000 像素。如果你遇到了 `cv2.error: OpenCV(4.7.0) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4062: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'` 这样的错误,那么可能是因为调整大小的目标大小有误。
请确保你的 `img` 图像不是空的(即 `empty()`),并且输入的目标大小 `(1000, 1000)` 是正确的。如果你想调整图像大小为其他尺寸,可以将目标大小替换成其他值。例如,如果你想将图像大小调整为 800x800,可以使用以下代码:
```python
img = cv2.resize(img, (800, 800))
```
需要注意的是,`cv2.resize()` 函数可能会改变图像的长宽比,因此在调整图像大小之前,你需要确保了解自己的数据和调整大小的方法,以避免失真或形变。
cv2.resize()参数
cv2.resize()函数有以下参数:
1. src:表示输入图像,可以是任何维度的numpy数组。
2. dsize:表示输出图像的大小,可以是一个元组(width,height),也可以是一个整数,这种情况下输出图像的大小为(dsize,dsize)。
3. fx:表示在水平方向上的缩放因子。
4. fy:表示在垂直方向上的缩放因子。
5. interpolation:表示缩放时使用的插值方法,有以下几种可选:
- cv2.INTER_NEAREST:最近邻插值法。
- cv2.INTER_LINEAR:双线性插值法(默认值)。
- cv2.INTER_AREA:区域插值法。
- cv2.INTER_CUBIC:双三次插值法。
- cv2.INTER_LANCZOS4:Lanczos插值法。
阅读全文
相关推荐















