【Matplotlib】基础设置之图像处理05

图像基础

导入相应的包:

python 复制代码
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
%matplotlib inline

导入图像

我们首先导入上面的图像,注意 matplotlib 默认只支持 PNG 格式的图像,我们可以使用 mpimg.imread 方法读入这幅图像:

python 复制代码
img = mpimg.imread('stinkbug.png')
python 复制代码
img.shape
复制代码
(375L, 500L, 3L)

这是一个 375 x 500 x 3RGB 图像,并且每个像素使用 uint8 分别表示 RGB 三个通道的值。不过在处理的时候,matplotlib 将它们的值归一化到 0.0~1.0 之间:

python 复制代码
img.dtype
复制代码
dtype('float32')

显示图像

使用 plt.imshow() 可以显示图像:

python 复制代码
imgplot = plt.imshow(img)

伪彩色图像

从单通道模拟彩色图像:

python 复制代码
lum_img = img[:,:,0]
imgplot = plt.imshow(lum_img)

改变 colormap

python 复制代码
imgplot = plt.imshow(lum_img)
imgplot.set_cmap('hot')
python 复制代码
imgplot = plt.imshow(lum_img)
imgplot.set_cmap('spectral')

显示色度条:

python 复制代码
imgplot = plt.imshow(lum_img)
imgplot.set_cmap('spectral')
plt.colorbar()
plt.show()

限制显示范围

先查看直方图:

python 复制代码
plt.hist(lum_img.flatten(), 256, range=(0.0,1.0), fc='k', ec='k')
plt.show()

将显示范围设为 0.0-0.7

python 复制代码
imgplot = plt.imshow(lum_img)
imgplot.set_clim(0.0,0.7)

resize 操作

python 复制代码
from PIL import Image
img = Image.open('stinkbug.png')
rsize = img.resize((img.size[0]/10,img.size[1]/10))
rsizeArr = np.asarray(rsize) 
imgplot = plt.imshow(rsizeArr)

上面我们将这个图像使用 PIL 的 Image 对象导入,并将其 resize 为原来的 1/100,可以看到很多细节都丢失了。

在画图时,由于画面的大小与实际像素的大小可能不一致,所以不一致的地方会进行插值处理,尝试一下不同的插值方法:

python 复制代码
imgplot = plt.imshow(rsizeArr)
imgplot.set_interpolation('nearest')
python 复制代码
imgplot = plt.imshow(rsizeArr)
imgplot.set_interpolation('bicubic')
相关推荐
晚霞的不甘1 小时前
CANN 支持多模态大模型:Qwen-VL 与 LLaVA 的端侧部署实战
人工智能·神经网络·架构·开源·音视频
华玥作者7 小时前
[特殊字符] VitePress 对接 Algolia AI 问答(DocSearch + AI Search)完整实战(下)
前端·人工智能·ai
AAD555888997 小时前
YOLO11-EfficientRepBiPAN载重汽车轮胎热成像检测与分类_3
人工智能·分类·数据挖掘
王建文go7 小时前
RAG(宠物健康AI)
人工智能·宠物·rag
ALINX技术博客7 小时前
【202601芯动态】全球 FPGA 异构热潮,ALINX 高性能异构新品预告
人工智能·fpga开发·gpu算力·fpga
易营宝7 小时前
多语言网站建设避坑指南:既要“数据同步”,又能“按市场个性化”,别踩这 5 个坑
大数据·人工智能
春日见8 小时前
vscode代码无法跳转
大数据·人工智能·深度学习·elasticsearch·搜索引擎
Drgfd8 小时前
真智能 vs 伪智能:天选 WE H7 Lite 用 AI 人脸识别 + 呼吸灯带,重新定义智能化充电桩
人工智能·智能充电桩·家用充电桩·充电桩推荐
萤丰信息9 小时前
AI 筑基・生态共荣:智慧园区的价值重构与未来新途
大数据·运维·人工智能·科技·智慧城市·智慧园区
盖雅工场9 小时前
排班+成本双管控,餐饮零售精细化运营破局
人工智能·零售餐饮·ai智能排班