Matplotlib Introductory Image tutorial

本文介绍了Matplotlib库的两种绘图风格,imperative-style和declarative-style,以及如何使用imread和imshow处理图像。文章详细讨论了RGB图像、灰度图像和亮度图像,并展示了如何应用伪颜色增强对比。此外,还探讨了图像的特定数据范围分析、颜色缩放限制和不同的插值方法,如bicubic插值在放大图像中的作用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言

matplotlib 图片操作 讲得比较浅显
imread()
imshow()

Image tutorial

Startup commands

Matplotlib是一种可视化库,支持两种方式进行图形绘制,分别是imperative-style和declarative-style。

imperative-style是指Matplotlib的原始绘图方式,类似于面向过程的编程方式,通过依次执行一系列操作来构建图形。例如,使用plt.plot()函数逐步添加各个元素(如坐标轴、数据点、标签等)。

imperative-style的缺点是代码较长、可读性不强,操作顺序容易出错,并且代码复用和组合比较困难。但是,imperative-style也有其优点,例如自由度高、灵活性强、易于快速掌握和调试等。

相比之下,declarative-style则更加注重定义和描述图形的属性和特征,而非直接操作每个元素绘制图形。这种方式下开发者需要对数据进行抽象,而不是单纯的面对图形细节。declarative-style具有高层次的抽象性,并能够自动,优美地构建复杂图形构建数据集的映射关系,易于阅读、扩展和重新编排等。

Importing image data into Numpy arrays

  • imread
    png格式 会返回 0-1浮点数, 其他格式 会返回 int数组.
img = mpimg.imread('./stinkbug.png')
print(img)
'''
[[[0.40784314 0.40784314 0.40784314]
  [0.40784314 0.40784314 0.40784314]
  [0.40784314 0.40784314 0.40784314]
  ...
  [0.42745098 0.42745098 0.42745098]
  [0.42745098 0.42745098 0.42745098]
  [0.42745098 0.42745098 0.42745098]]

 [[0.4117647  0.4117647  0.4117647 ]
  [0.4117647  0.4117647  0.4117647 ]
  [0.4117647  0.4117647  0.4117647 ]
  ...
  [0.42745098 0.42745098 0.42745098]
  [0.42745098 0.42745098 0.42745098]
  [0.42745098 0.42745098 0.42745098]]

 [[0.41960785 0.41960785 0.41960785]
  [0.41568628 0.41568628 0.41568628]
  [0.41568628 0.41568628 0.41568628]
  ...
  [0.43137255 0.43137255 0.43137255]
  [0.43137255 0.43137255 0.43137255]
  [0.43137255 0.43137255 0.43137255]]

 ...

 [[0.4392157  0.4392157  0.4392157 ]
  [0.43529412 0.43529412 0.43529412]
  [0.43137255 0.43137255 0.43137255]
  ...
  [0.45490196 0.45490196 0.45490196]
  [0.4509804  0.4509804  0.4509804 ]
  [0.4509804  0.4509804  0.4509804 ]]

 [[0.44313726 0.44313726 0.44313726]
  [0.44313726 0.44313726 0.44313726]
  [0.4392157  0.4392157  0.4392157 ]
  ...
  [0.4509804  0.4509804  0.4509804 ]
  [0.44705883 0.44705883 0.44705883]
  [0.44705883 0.44705883 0.44705883]]

 [[0.44313726 0.44313726 0.44313726]
  [0.4509804  0.4509804  0.4509804 ]
  [0.4509804  0.4509804  0.4509804 ]
  ...
  [0.44705883 0.44705883 0.44705883]
  [0.44705883 0.44705883 0.44705883]
  [0.44313726 0.44313726 0.44313726]]]
'''

Note the dtype there - float32. Matplotlib has rescaled the 8 bit data from each channel to floating point data between 0.0 and 1.0. As a side note, the only datatype that Pillow can work with is uint8. Matplotlib plotting can handle float32 and uint8, but image reading/writing for any format other than PNG is limited to uint8 data. Why 8 bits? Most displays can only render 8 bits per channel worth of color gradation. Why can they only render 8 bits/channel? Because that’s about all the human eye can see.
请注意那里的 dtype - float32。Matplotlib 重新缩放了 8 位 从每个通道的数据到 0.0 到 1.0 之间的浮点数据。如 旁注,Pillow 可以使用的唯一数据类型是 uint8。 Matplotlib 绘图可以处理 float32 和 uint8,但图像 PNG 以外的任何格式的读/写仅限于 uint8 数据。为什么是 8 位?大多数显示器每通道只能渲染 8 位 值得的颜色渐变。为什么他们只能渲染 8 位/通道? 因为这是人眼所能看到的一切。
Each inner list represents a pixel. Here, with an RGB image, there are 3 values. Since it’s a black and white image, R, G, and B are all similar. An RGBA (where A is alpha, or transparency), has 4 values per inner list, and a simple luminance image just has one value (and is thus only a 2-D array, not a 3-D array). For RGB and RGBA images, Matplotlib supports float32 and uint8 data types. For grayscale, Matplotlib supports only float32. If your array data does not meet one of these descriptions, you need to rescale it.
每个内部列表代表一个像素。这里,有 RGB 图像,那里 是 3 个值。由于它是黑白图像,因此 R、G 和 B 都很相似。RGBA(其中 A 是 alpha 或透明度)有 4 个值, 每个内部列表,一个简单的亮度图像只有一个值(和 因此只是一个二维数组,而不是三维数组)。对于 RGB 和 RGBA 图像, Matplotlib 支持 float32 和 uint8 数据类型。对于灰度, Matplotlib 仅支持 float32。如果您的阵列数据不符合 这些描述之一,您需要重新缩放它。

  • imshow
imgplot = plt.imshow(img)
Applying pseudocolor schemes to image plots

在图象图中运用伪颜色方案.
伪颜色可以用来加深对比和更轻松地可视化数据.伪颜色只与单通道, 灰度, 亮度图像相关.

单通道图像是一种使用单个像素值来表示图像中每个位置颜色信息的图像格式。在图像处理中,通常是通过将RGB(红、绿和蓝)图像转换为单通道图像来实现,例如通过使用加权平均法将RGB三通道组合起来来计算一个单一的灰度值。
Grayscale image,中文翻译为灰度图像,是一种只包含明暗信息而不包含彩色信息的二维图像。灰度图像与彩色图像的不同之处在于没有色彩通道,每个像素只有一个数值来表示亮度或灰度。
Luminosity image,翻译成中文是亮度图像,是一种指图像中每个像素的明度(luminance)信息构成的二维数组。与彩色图像不同,亮度图像是单通道的灰度图像,通常在黑白照片和计算机视觉领域中使用。

我们现在有一个RGB的图像, 因为RGB的值比较相似, 我们可以只从数据中取出一个通道

lum_img = img[:, :, 0]
plt.imshow(lum_img)

有了亮度图像, 默认色图就可以使用了.

plt.imshow(lum_img, cmap="hot")

用set_cmap 方法设置色图.

imgplot = plt.imshow(lum_img)
imgplot.set_cmap('nipy_spectral')
Examining a specific data range

有的时候, 你想要在某个特定区域加强对比. 我们可以用直方图来查看.

plt.hist(lum_img.ravel(), bins=256, range=(0.0, 1.0), fc='k', ec='k')
  • set_clim 设置图像缩放的标准限制
imgplot = plt.imshow(lum_img, clim=(0.0, 0.7))
imgplot.set_clim(0.0, 0.7)
Array Interpolation schemes

插值计算了颜色或一个像素值应该是多少,根据不同的数学方案. 当你改变图像大小时, 像素的数量会改变, 但是你想要相同的信息. 因为像素是分离的, 有缺失空间. 插值就是你如何填充这个空间.

from PIL import Image

img = Image.open('stinkbug.png')
img.thumbnail((64, 64), Image.ANTIALIAS)  # resizes image in-place
imgplot = plt.imshow(img)

我们有效地丢掉了一些像素, 只保留了少数几个.

默认的插值是bilinear 选择不同的插值方法.

imgplot = plt.imshow(img, interpolation="nearest")
imgplot = plt.imshow(img, interpolation="bicubic")

bicubic 插值 经常被用于blowing up 图片, 人们更喜欢模糊而不是像素.

“Blowing up photos”可以翻译为“放大照片”,是指将照片或图像从原始大小放大到一个更大的尺寸。通常情况下,放大照片是为了获得更高的分辨率或更清晰的图像细节。但是,由于图像放大会导致图像失真和模糊,因此放大过程需要非常小心,采用专业的图像处理工具,并根据图像的具体情况选择不同的算法和技术以便达到最佳效果。在某些情况下,放大照片可能只能获得有限的增益,并且实际效果可能与期望的不同.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值