04_OpenCV图片缩放

复制代码
import cv2
import matplotlib.pyplot as plt  # Python 的 2D绘图库
 
# 读入原图片
img = cv2.imread('libarary.JPG')
# 打印出图片尺寸
print(img.shape)
# 将图片高和宽分别赋值给x,y
x, y = img.shape[0:2]
 
# 显示原图
#cv.imshow('OriginalPicture', img)
 
# 缩放到原来的二分之一,输出尺寸格式为(宽,高)
img_test1 = cv2.resize(img, (int(y / 2), int(x / 2)))
# cv2.imshow('resize0', img_test1)
# cv2.waitKey()
 
# 最近邻插值法缩放
# 缩放到原来的八分之一
img_test2 = cv2.resize(img, (0, 0), fx=0.125, fy=0.125, interpolation=cv2.INTER_NEAREST)
# cv.imshow('resize1', img_test2)
# cv.waitKey()
# cv.destroyAllWindows()
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
dst1 = cv2.cvtColor(img_test1, cv2.COLOR_BGR2RGB)
dst2 = cv2.cvtColor(img_test2, cv2.COLOR_BGR2RGB)

# 显示原始图像
plt.imshow(img)
plt.show()
复制代码
(3456, 5184, 3)
复制代码
# 显示缩放1/2
plt.imshow(dst1)
plt.show()
复制代码
# 显示缩放1/8 邻插值法缩放
plt.imshow(dst2)
plt.show()

以下是matplotlib的一个小例子

参考教程:NumPy Matplotlib | 菜鸟教程

复制代码
import numpy as np 
from matplotlib import pyplot as plt 

x = np.arange(1,11) 
y =  2 * x +  5 
plt.title("Matplotlib demo") 
plt.xlabel("x axis caption") 
plt.ylabel("y axis caption") 
plt.plot(x,y) 
plt.show()
相关推荐
星融元asterfusion2 分钟前
突破AI瓶颈:基于实时感知的智能选路实现智算负载均衡优化
运维·人工智能·负载均衡
聚客AI7 分钟前
Masked LM革命:解析BERT如何用15%掩码率颠覆NLP预训练
人工智能·llm·掘金·日新计划
何玺16 分钟前
从Pura 80系列影像和鸿蒙AI融合看华为创新的“不可复制性”
人工智能·华为·harmonyos
仙人掌_lz1 小时前
AI与机器学习ML:利用Python 从零实现神经网络
人工智能·python·机器学习
我感觉。1 小时前
【医疗电子技术-7.2】血糖监测技术
人工智能·医疗电子
DeepSeek忠实粉丝1 小时前
微调篇--超长文本微调训练
人工智能·程序员·llm
XiaoQiong.Zhang1 小时前
简历模板3——数据挖掘工程师5年经验
大数据·人工智能·机器学习·数据挖掘
Akamai中国1 小时前
为何AI推理正推动云计算从集中式向分布式转型
人工智能·云原生·云计算·边缘计算
oil欧哟1 小时前
🧐 如何让 AI 接入自己的 API?开发了一个将 OpenAPI 文档转为 MCP 服务的工具
前端·人工智能·mcp
whoarethenext2 小时前
C++/OpenCV地砖识别系统结合 Libevent 实现网络化 AI 接入
c++·人工智能·opencv