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()
相关推荐
zhangfeng11331 分钟前
2026-08-10/11 AI 领域重要动态筛选(侧重 AI coding 与具身智能)
人工智能·microsoft
leoZ23111 分钟前
Vue3 还原一个企业级后台-01-项目背景与选题
图像处理·人工智能·chatgpt·智慧城市·边缘计算·openvino·dreamfusion
ltqvibe20 分钟前
企业数智化中台的五层架构——AI框架为什么需要纵向贯通
大数据·人工智能·架构
兮动人21 分钟前
AI 开始接管工作台:谁会成为下一代电脑入口?
人工智能·ai·chatgpt·codex·workbuddy
办公室马主任29 分钟前
制造业数字化的系统架构:从车间数据到经营闭环怎么设计
人工智能·系统架构·制造
Claire_8837 分钟前
基于知识图谱构建的学习资料分类整理方案:以多模态检索与智能生成为例
人工智能·powerpoint
花椒技术41 分钟前
一个人已经有 Agent 了,我们为什么还要建设统 Agent 平台
人工智能·agent·ai编程
Olafur_zbj1 小时前
【AI】CUDA的新编程模型:tile编程模型 的好处
人工智能
Linguwen2 小时前
外贸GEO01|GEO是什么?生成式引擎优化,AI时代的新流量密码
人工智能