当前位置:首页 > 专题范文 > 公文范文 > 使用Python进行基本图像数据分析

使用Python进行基本图像数据分析

发布时间:2022-08-17 14:15:04 来源:网友投稿

下面是小编为大家整理的使用Python进行基本图像数据分析,供大家参考。

使用Python进行基本图像数据分析

 

 本教程将介绍如何导入图像并观察其属性、拆分图层以及查看灰度。在正式开始之前,我们先来了解一些关于像素的基础知识。

 计算机将图片以像素形式存储,这就像马赛克一样。如果像素太大,很难制作光滑的边缘和曲线。相反,我们使用的像素越多越小,看起来就会越平滑,或者说像素化程度越小,图像就会越好看,有时,这也被称为图像分辨率。

 矢量图形是一种有点不同的存储图像方法,旨在避免与像素相关的问题。但是,即使是矢量图像,最终也会显示为像素一样的马赛克。颜色像素表示图像元素,描述每个像素的简单方法是使用三种颜色的组合,即红色,绿色,蓝色,这就是我们所说的 RGB 图像。

 在 RGB 图像中,每个像素分别与红色,绿色,蓝色的值相关联的三个 8 比特数字表示。最后,如果使用放大镜观察缩放的图片,我们会看到图片由微小的光点或更具体的像素组成,更有趣的是这些小光点实际上具有多个不同颜色。

  每张照片都以数字形式由像素组成,它们是构成图片的最小信息单位,通常是圆形或方形,它们通常布置在二维网格中。

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

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

 以下是Numpyand非常基本的图像数据分析步骤,其中一些涉及Python pacakges,如 imageio,matplotlib 等。

  导入图像并观察其属性  拆分图层  Greyscale

  对像素值使用逻辑运算符  使用逻辑运算符进行运算  卫星图像数据分析 导入图像 现在让我们加载图像并观察各种属性:

 1. if __name__ == "__main__": 2. import imageio 3. import matplotlib.pyplot as plt 4. %matplotlib inline 5. pic = imageio.imread("F:/demo_2.jpg") 6. plt.figure(figsize = (15,15)) 7. plt.imshow(pic)观察图像的基本属性 8. print("Type of the image : " , type(pic)) 9. print("Shape of the image : {}".format(pic.shape)) 10. print("Image Hight {}".format(pic.shape[0])) 11. print("Image Width {}".format(pic.shape[1])) 12. print("Dimension of Image {}".format(pic.ndim)) 13. Type of the image : <class "imageio.core.util.Image"> 14. Shape of the image : (562, 960, 3) 15. Image Hight 562 16. Image Width 960 17. Dimension of Image 3

  ndarray 的形状表明它是一个三层矩阵,这里的前两个数字是长度和宽度,第三个数字(即 3)是三层:Red, Green, Blue。

 因此,如果我们计算 RGB 图像的大小,则总大小将计为 height x width x 3 1. print("Image size {}".format(pic.size)) 2. print("Maximum RGB value in this image {}".format(pic.max())) 3. print("Minimum RGB value in this image {}".format(pic.min())) 4. Image size 1618560 5. Maximum RGB value in this image 255 6. Minimum RGB value in this image 0 这些值对于验证很重要,因为 8 比特颜色强度不能超出 0 到 255 范围。

 现在,使用图片分配变量,我们还可以访问图片的任何特定像素值,并进一步访问每个 RGB 通道。

 1. """ 2. Let"s pick a specific pixel located at 100 th Rows and 50 th Column. 3. And view the RGB value gradually. 4. """ 5. pic[ 100, 50 ] 6. 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颜色格式)。

 1. # A specific pixel located at Row : 100 ; Column : 50 2. # Each channel"s value of it, gradually R , G , B 3. print("Value of only R channel {}".format(pic[ 100, 50, 0])) 4. print("Value of only G channel {}".format(pic[ 100, 50, 1])) 5. print("Value of only B channel {}".format(pic[ 100, 50, 2])) 1. Value of only R channel 109 2. Value of only G channel 143 3. Value of only B channel 46 好的,现在让我们快速查看整个图像中的每个频道。

 1. plt.title("R channel") 2. plt.ylabel("Height {}".format(pic.shape[0])) 3. plt.xlabel("Width {}".format(pic.shape[1])) 4. plt.imshow(pic[ : , : , 0]) 5. plt.show()

  1. plt.title("G channel") 2. plt.ylabel("Height {}".format(pic.shape[0])) 3. plt.xlabel("Width {}".format(pic.shape[1])) 4. plt.imshow(pic[ : , : , 1]) 5. plt.show()

 1. plt.title("B channel")

 2. plt.ylabel("Height {}".format(pic.shape[0])) 3. plt.xlabel("Width {}".format(pic.shape[1])) 4. plt.imshow(pic[ : , : , 2]) 5. plt.show()

 现在,我们可以更改 RGB 值的数量。例如,让我们对红色、绿色、蓝色图层设置跟随行值的强度。

  R 频道:行 - 100 到 110  G 频道:行 - 200 到 210  B 频道:行 - 300 到 310 我们将加载一次图像,以便可以同时显示每个层的变化。

 1. pic = imageio.imread("F:/demo_2.jpg") 2. pic[50:150 , : , 0] = 255 # full intensity to those pixel"s R channel 3. plt.figure( figsize = (10,10)) 4. plt.imshow(pic) 5. plt.show()

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

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

 4. plt.show()

 为了更清楚,让我们也改变列部分,这次我们将同时更改 RGB 通道。

 1. # set value 200 of all channels to those pixels which turns them to white 2. pic[ 50:450 , 400:600 , [0,1,2] ] = 200 3. plt.figure( figsize = (10,10)) 4. plt.imshow(pic) 5. plt.show()

 拆分图层 现在,我们知道图像的每个像素都由三个整数表示,将图像分割成单独的颜色分片只需拉出图像阵列的正确切片。

 1. import numpy as np 2. pic = imageio.imread("F:/demo_2.jpg") 3. fig, ax = plt.subplots(nrows = 1, ncols=3, figsize=(15,5)) 4. for c, ax in zip(range(3), ax): 5. # create zero matrix 6. split_img = np.zeros(pic.shape, dtype="uint8") # "dtype" by default: "numpy.float64" 7. # assing each channel 8. split_img[ :, :, c] = pic[ :, :, c] 9. # display each channel 10. ax.imshow(split_img)

 灰度 黑白图像存储在二维阵列中,有两种类型的黑白图像:

  Greyscale:灰色阴影范围:0~255  Binary:像素为黑色或白色:0 或 255 现在,Greyscaling 是一个将图像从全色转换为灰色阴影的过程。在图像处理工具中,例如:在 OpenCV 中,许多功能在处理之前使用灰度图像,这样做是因为它简化了图像,几乎可以降噪并增加处理时间,因为图像中的信息较少。

 在 python 中有两种方法可以将图像转换为灰度,但使用 matplotlib 的简单方法是使用此公式获取原始图像的 RGB 值的加权平均值。

 1. Y" = 0.299 R + 0.587 G + 0.114 B 1. pic = imageio.imread("F:/demo_2.jpg")

 2. gray = lambda rgb : np.dot(rgb[... , :3] , [0.299 , 0.587, 0.114]) 3. gray = gray(pic) 4. plt.figure( figsize = (10,10)) 5. plt.imshow(gray, cmap = plt.get_cmap(name = "gray")) 6. plt.show()

 然而,GIMP 将颜色转换为灰度图像软件有三种算法来完成任务。

 灰度的 Lightness 等级计算为 1. Lightness = ½ × (max(R,G,B) + min(R,G,B)) 灰度的 Luminosity 等级计算为 1. Luminosity = 0.21 × R + 0.72 × G + 0.07 × B 灰度的 Average 计算为 1. Average Brightness = (R + G + B) ÷ 3 让我们尝试一下算法,Luminosity 效果如何? 1. pic = imageio.imread("F:/demo_2.jpg") 2. gray = lambda rgb : np.dot(rgb[... , :3] , [0.21 , 0.72, 0.07])

 3. gray = gray(pic) 4. plt.figure( figsize = (10,10)) 5. plt.imshow(gray, cmap = plt.get_cmap(name = "gray")) 6. plt.show() 7. """ 8. Let"s take a quick overview some the changed properties now the color image. 9. Like we observe some properties of color image, same statements are applying 10. now for gray scaled image. 11. """ 12. print("Type of the image : " , type(gray)) 13. print() 14. print("Shape of the image : {}".format(gray.shape)) 15. print("Image Hight {}".format(gray.shape[0])) 16. print("Image Width {}".format(gray.shape[1])) 17. print("Dimension of Image {}".format(gray.ndim)) 18. print() 19. print("Image size {}".format(gray.size)) 20. print("Maximum RGB value in this image {}".format(gray.max())) 21. print("Minimum RGB value in this image {}".format(gray.min())) 22. print("Random indexes [X,Y] : {}".format(gray[100, 50])) 23. Type of the image : <class "imageio.core.util.Image"> 24. Shape of the image : (562,960) 25. Image Height 562 26. Image Widht 960 27. Dimension of Image 2 28. Image size 539520 29. Maximum RGB value in this image 254.9999999997 30. Minimum RGB value in this image 0.0 31. Random indexes [X,Y] : 129.07 使用逻辑运算符处理像素值

 我们可以使用逻辑运算符创建相同大小的 bullion ndarray。但是,这不会创建任何新数组,它只是将值返回到其主变量。例如,如果考虑在 RGB 图像中滤除一些低值像素或高值或(任何条件),可以先将 RGB 转换为灰度。

 首先加载图像并在屏幕上显示:

 1. pic = imageio.imread("F:/demo_1.jpg") 2. plt.figure(figsize = (10,10)) 3. plt.imshow(pic) 4. plt.show()

 接下来,我们考虑转储该图像,比如我们想要过滤所有低于 20 的像素值。为此,我们将使用逻辑运算符执行此任务,返回所有索引的 True 值。

 1. low_pixel = pic < 20 2. # to ensure of it let"s check if all values in low_pixel are True or not 3. if low_pixel.any() == True:

 4. print(low_pixel.shape) 5. (1079, 1293, 3) 正如上文所说,传统上不使用宿主变量,但我之所以提到是因为它只保留 True值。

 所以,如果我们看到 low_pixel 和 pic 的 shape,我们会发现它们都具有相同的 shape。

 1. print(pic.shape) 2. print(low_pixel.shape) 3. (1079, 1293, 3) 4. (1079, 1293, 3) 我们使用全局比较运算符为所有小于 200 的值生成低值滤波器。但是,我们可以使用此 low_pixel 数组作为索引将这些低值设置为某些特定值,这些值可能高于或低于先前的像素值。

 1. # randomly choose a value 2. import random 3. # load the orginal image 4. pic = imageio.imread("F:/demo_1.jpg") 5. # set value randomly range from 25 to 225 - these value also randomly choosen 6. pic[low_pixel] = random.randint(25,225) 7. # display the image 8. plt.figure( figsize = (10,10)) 9. plt.imshow(pic) 10. plt.show()

  图层蒙版 图像蒙版是一种图像处理技术,用于去除具有模糊边缘,透明或头发部分的照片背景。

 现在,我们将创建一个圆盘形状的蒙版。首先,我们将测量从图像中心到每个边界像素值的距离。我们设置一个比较方便的半径值,然后使用逻辑运算符创建一个圆盘,以下为代码:

 1. if __name__ == "__main__": 2. # load the image 3. pic = imageio.imread("F:/demo_1.jpg") 4. # seperate the row and column values 5. total_row , total_col , layers = pic.shape 6. """ 7. Create vector. 8. Ogrid is a compact method of creating a multidimensional- 9. ndarray operations in single lines.

 10. for ex: 11. >>> ogrid[0:5,0:5] 12. output: [array([[0], 13. [1], 14. [2], 15. [3], 16. [4]]), 17. array([[0, 1, 2, 3, 4]])] 18. """ 19. x , y = np.ogrid[:total_row , :total_col] 20. # get the center values of the image 21. cen_x , cen_y = total_row/2 , total_col/2 22. """ 23. Measure distance value from center to each border pixel. 24. To make it easy, we can think it"s like, we draw a line from center- 25. to each edge pixel value --> s**2 = (Y-y)**2 + (X-x)**2 26. """ 27. distance_from_the_center = np.sqrt((x-cen_x)**2 + (y-cen_y)**2) 28. # Select convenient radius value 29. radius = (total_row/2) 30. # Using logical o...

推荐访问:python数据分析报告总结 使用Python进行基本图像数据分析 图像 分析 数据

版权所有:袖书文档网 2002-2024 未经授权禁止复制或建立镜像[袖书文档网]所有资源完全免费共享

Powered by 袖书文档网 © All Rights Reserved.。备案号:鲁ICP备20026461号-1