python cv2 图像缩放
时间: 2023-10-28 22:05:33 浏览: 98
图像缩放是指改变图像的尺寸大小。在Python中,可以使用OpenCV库中的cv2.resize()函数来实现图像缩放。该函数可以通过设置缩放比例或指定目标尺寸来进行图像缩放。
以下是三种常用的图像缩放方法示例:
方法一:
```python
import cv2
img = cv2.imread('9.jpg') # 读取图像
dst = cv2.resize(img, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_LINEAR) # 缩放图像
cv2.imshow('dst', dst) # 显示缩放后的图像
cv2.waitKey(0) # 等待按键退出
cv2.destroyAllWindows() # 关闭窗口
```
方法二:
```python
import cv2
img1 = cv2.imread("./nba.png") # 读取图像
img2 = cv2.resize(img1, (600, 600), interpolation=cv2.INTER_NEAREST) # 缩放图像
cv2.imshow("img2", img2) # 显示缩放后的图像
cv2.waitKey(0) # 等待按键退出
cv2.destroyAllWindows() # 关闭窗口
```
方法三:
```python
import cv2
img = cv2.imread('9.jpg') # 读取图像
height, width = img.shape[:2] # 获取原图像的尺寸
dst = cv2.resize(img, (int(0.6 * width), int(0.5 * height)), interpolation=cv2.INTER_LINEAR) # 缩放图像
cv2.imshow('dst', dst) # 显示缩放后的图像
cv2.waitKey(0) # 等待按键退出
cv2.destroyAllWindows() # 关闭窗口
```
阅读全文
相关推荐
















