获取内容资料
Python开发

python图像处理教程,python基础教程第四版

[来自IT168] 【IT168 评论】本教程将介绍如何导入图像并观察其属性、拆分图层以及查看灰度。在正式开始之前,我们先来了解一些关于像素的基础知识。 计算机将图片以像素形式存储,这就像马赛克一样。如果像素太大,很难制作光滑的边缘和曲线。相反,我们使用的像素越多越小,看起来就会越平滑,或者说像素化程度越小,图像就会越好看,有时,这也被称为图像分辨率。

矢量图形是一种有点不同的存储图像方法,旨在避免与像素相关的问题。但是,即使是矢量图像,最终也会显示为像素一样的马赛克。颜色像素表示图像元素,描述每个像素的简单方法是使用三种颜色的组合,即红色,绿色,蓝色,这就是我们所说的RGB图像。 在RGB图像中,每个像素分别与红色,绿色,蓝色的值相关联的三个8比特数字表示。最后,如果使用放大镜观察缩放的图片,我们会看到图片由微小的光点或更具体的像素组成,更有趣的是这些小光点实际上具有多个不同颜色。 每张照片都以数字形式由像素组成,它们是构成图片的最小信息单位,通常是圆形或方形,它们通常布置在二维网格中。

如果三个颜色都处于最大值,则意味着它们是255,那就会显示为白色,如果三种颜色都处于最小值,或者值为0,则颜色显示为黑色。反过来,这三者的组合将为我们提供特定的像素颜色。由于每个颜色数字都是8个比特,因此值范围为0-255。

由于每个值可以具有256个不同的强度或亮度值,因此三种颜色总共有1680万个shade。

以下是Numpyand非常基本的图像数据分析步骤,其中一些涉及Python pacakges,如imageio,matplotlib等。导入图像并观察其属性拆分图层Greyscale对像素值使用逻辑运算符使用逻辑运算符进行运算卫星图像数据分析 导入图像 现在让我们加载图像并观察各种属性: if __name__ == ‘__main__’: import imageio import matplotlib.pyplot as plt %matplotlib inline pic = imageio.imread(‘F:/demo_2.jpg’) plt.figure(figsize = (15,15)) plt.imshow(pic)观察图像的基本属性 print(‘Type of the image : ‘ , type(pic)) print(‘Shape of the image : ‘.format(pic.shape)) print(‘Image Hight ‘.format(pic.shape[0])) print(‘Image Width ‘.format(pic.shape[1])) print(‘Dimension of Image ‘.format(pic.ndim)) Type of the image : Shape of the image : (562, 960, 3) Image Hight 562 Image Width 960 Dimension of Image 3

ndarray的形状表明它是一个三层矩阵,这里的前两个数字是长度和宽度,第三个数字(即3)是三层:Red, Green, Blue。 因此,如果我们计算RGB图像的大小,则总大小将计为height x width x 3 print(‘Image size ‘.format(pic.size)) print(‘Maximum RGB value in this image ‘.format(pic.max)) print(‘Minimum RGB value in this image ‘.format(pic.min)) Image size 1618560 Maximum RGB value in this image 255 Minimum RGB value in this image 0 这些值对于验证很重要,因为8比特颜色强度不能超出0到255范围。 现在,使用图片分配变量,我们还可以访问图片的任何特定像素值,并进一步访问每个RGB通道。 ”’ Let’s pick a specific pixel located at 100 th Rows and 50 th Column. And view the RGB value gradually. ”’ pic[ 100, 50 ] Image([109, 143, 46], dtype=uint8) 在这种情况下:R = 109; G = 143; B = 46,我们可以意识到这个特殊像素中有很多绿色。现在,我们可以通过给出三个通道的索引值来特别选择其中一个数字: 0红色通道的索引值 1绿色通道的索引值 2蓝色通道的索引值 但是,在OpenCV中,图像不是RGB而是BGR,imageio.imread将图像加载为RGB(或RGBA),但OpenCV假定图像为BGR或BGRA(BGR是默认的OpenCV颜色格式)。 # A specific pixel located at Row : 100 ; Column : 50 # Each channel’s value of it, gradually R , G , B print(‘Value of only R channel ‘.format(pic[ 100, 50, 0])) print(‘Value of only G channel ‘.format(pic[ 100, 50, 1])) print(‘Value of only B channel ‘.format(pic[ 100, 50, 2])) Value of only R channel 109 Value of only G channel 143 Value of only B channel 46 好的,现在让我们快速查看整个图像中的每个频道。 plt.title(‘R channel’) plt.ylabel(‘Height ‘.format(pic.shape[0])) plt.xlabel(‘Width ‘.format(pic.shape[1])) plt.imshow(pic[ : , : , 0]) plt.show

plt.title(‘G channel’) plt.ylabel(‘Height ‘.format(pic.shape[0])) plt.xlabel(‘Width ‘.format(pic.shape[1])) plt.imshow(pic[ : , : , 1]) plt.show

plt.title(‘B channel’) plt.ylabel(‘Height ‘.format(pic.shape[0])) plt.xlabel(‘Width ‘.format(pic.shape[1])) plt.imshow(pic[ : , : , 2]) plt.show

现在,我们可以更改RGB值的数量。例如,让我们对红色、绿色、蓝色图层设置跟随行值的强度。 R频道:行 – 100到110 G频道:行 – 200到210 B频道:行 – 300到310 我们将加载一次图像,以便可以同时显示每个层的变化。 pic = imageio.imread(‘F:/demo_2.jpg’) pic[50:150 , : , 0] = 255 # full intensity to those pixel’s R channel plt.figure( figsize = (10,10)) plt.imshow(pic) plt.show

pic[200:300 , : , 1] = 255 # full intensity to those pixel’s G channel plt.figure( figsize = (10,10)) plt.imshow(pic) plt.show

pic[350:450 , : , 2] = 255 # full intensity to those pixel’s B channel plt.figure( figsize = (10,10)) plt.imshow(pic) plt.show

为了更清楚,让我们也改变列部分,这次我们将同时更改RGB通道。 # set value 200 of all channels to those pixels which turns them to white pic[ 50:450 , 400:600 , [0,1,2] ] = 200 plt.figure( figsize = (10,10)) plt.imshow(pic) plt.show

拆分图层 现在,我们知道图像的每个像素都由三个整数表示,将图像分割成单独的颜色分片只需拉出图像阵列的正确切片。 import numpy as np pic = imageio.imread(‘F:/demo_2.jpg’) fig, ax = plt.subplots(nrows = 1, ncols=3, figsize=(15,5)) for c, ax in zip(range(3), ax): # create zero matrix split_img = np.zeros(pic.shape, dtype=”u

Similar Posts

发表评论

邮箱地址不会被公开。 必填项已用*标注