教程:使用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 :  
  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]))  
  6. Value of only R channel 109  
  7. Value of only G channel 143  
  8. 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() 

教程:使用Python进行基本图像数据分析!

  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() 

教程:使用Python进行基本图像数据分析!

  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() 

教程:使用Python进行基本图像数据分析!

为了更清楚,让我们也改变列部分,这次我们将同时更改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() 

教程:使用Python进行基本图像数据分析!

拆分图层

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

  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) 

教程:使用Python进行基本图像数据分析!

灰度

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

  • Greyscale:灰色阴影范围:0~255
  • Binary:像素为黑色或白色:0或255

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

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

  1. Y' = 0.299 R + 0.587 G + 0.114 B  
  2. pic = imageio.imread('F:/demo_2.jpg' 
  3. gray = lambda rgb : np.dot(rgb[... , :3] , [0.299 , 0.587, 0.114])  
  4. gray = gray(pic)  
  5. plt.figure( figsize = (10,10))  
  6. plt.imshow(gray, cmap = plt.get_cmap(name = 'gray'))  
  7. plt.show() 

教程:使用Python进行基本图像数据分析!

然而,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 :  
  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() 

教程:使用Python进行基本图像数据分析!

接下来,我们考虑转储该图像,比如我们想要过滤所有低于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() 

教程:使用Python进行基本图像数据分析!

图层蒙版

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

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

  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.  
  18. array([[0, 1, 2, 3, 4]])]  
  19. '' 
  20. x , y = np.ogrid[:total_row , :total_col]  
  21. # get the center values of the image  
  22. cen_x , cen_y = total_row/2 , total_col/2  
  23. '' 
  24. Measure distance value from center to each border pixel.  
  25. To make it easy, we can think it's like, we draw a line from center-  
  26. to each edge pixel value --> s**2 = (Y-y)**2 + (X-x)**2 
  27.  '' 
  28. distance_from_the_center = np.sqrt((x-cen_x)**2 + (y-cen_y)**2)  
  29. Select convenient radius value  
  30. radius = (total_row/2)  
  31. # Using logical operator '>'  
  32. '' 
  33. logical operator to do this task which will return as a value  
  34. of True for all the index according to the given condition  
  35. '' 
  36. circular_pic = distance_from_the_center > radius  
  37. '' 
  38. let assign value zero for all pixel value that outside the cirular disc.  
  39. All the pixel value outside the circular disc, will be black now.  
  40. '' 
  41. pic[circular_pic] = 0  
  42. plt.figure(figsize = (10,10))  
  43. plt.imshow(pic)  
  44. plt.show() 

教程:使用Python进行基本图像数据分析!

卫星图像处理

卫星图像及其处理系统非常有用,我们可以用于做一些分析任务。

  1. load the image  
  2. pic = imageio.imread('F:\satimg.jpg' 
  3. plt.figure(figsize = (10,10)) 
  4. plt.imshow(pic)  
  5. plt.show() 

教程:使用Python进行基本图像数据分析!

我们来看一些基本信息:

  1. print(f'Shape of the image {pic.shape}' 
  2. print(f'hieght {pic.shape[0]} pixels' 
  3. print(f'width {pic.shape[1]} pixels' 
  4. Shape of the image (3725, 4797, 3)  
  5. hieght 3725 pixels  
  6. width 4797 pixels 

这张图片上有一些有趣的东西,像许多其他的图像可视化一样,每个RGB层中的颜色都有自己的意思。例如,红色的强度将表示像素中的地理数据点的高度,蓝色的强度表示方位的度量,绿色表示斜率。这些颜色将有助于以更快,更有效的方式传达此信息,而不是显示数字。

  • 红色像素表示:Altitude·
  • 蓝色像素表示:Aspect
  • 绿色像素表示: Slope

通过观察彩色图像,我们可以分辨出海拔是多少,斜率是多少,以及Slope是什么,这就是为颜色加载更多含义以表示更科学的分析的想法。

检测每个通道的像素

  1. Only Red Pixel value , higher than 180  
  2. pic = imageio.imread('F:\satimg.jpg' 
  3. red_mask = pic[:, :, 0] < 180  
  4. pic[red_mask] = 0  
  5. plt.figure(figsize=(15,15))  
  6. plt.imshow(pic)  
  7. Only Green Pixel value , higher than 180  
  8. pic = imageio.imread('F:\satimg.jpg' 
  9. green_mask = pic[:, :, 1] < 180  
  10. pic[green_mask] = 0  
  11. plt.figure(figsize=(15,15))  
  12. plt.imshow(pic)  
  13. Only Blue Pixel value , higher than 180  
  14. pic = imageio.imread('F:\satimg.jpg' 
  15. blue_mask = pic[:, :, 2] < 180  
  16. pic[blue_mask] = 0  
  17. plt.figure(figsize=(15,15))  
  18. plt.imshow(pic)  
  19. # Composite mask using logical_and  
  20. pic = imageio.imread('F:\satimg.jpg' 
  21. final_mask = np.logical_and(red_mask, green_mask, blue_mask)  
  22. pic[final_mask] = 40  
  23. plt.figure(figsize=(15,15))  
  24. plt.imshow(pic) 

教程:使用Python进行基本图像数据分析!

未完待续......这只是该教程的第一章节,其他内容将会在后续章节中呈现。

责任编辑:未丽燕 来源: IT168
相关推荐

2018-07-24 16:05:58

2017-09-26 19:02:09

PythonInstagram数据分析

2018-07-24 16:00:38

2019-01-15 14:21:13

Python数据分析数据

2020-10-28 18:28:12

Pandas数据分析GUI

2020-04-30 16:38:21

数据分析可视化代码

2020-07-09 15:21:58

大数据RStudioR语言

2017-04-26 14:02:18

大数据数据分析Excel

2020-06-05 14:29:07

PythonPandas数据分析

2022-06-09 11:47:21

工具数据仪连接器

2020-02-20 10:45:51

Python数据疾病

2016-12-20 16:42:57

iPhone数据分析

2021-12-28 11:23:36

SQLServerExcel数据分析

2013-06-27 15:21:38

App

2017-02-06 14:12:29

大数据数据分析基本思想

2020-12-17 09:45:54

数据分析互联网大数据

2012-08-08 09:53:23

HadoopMapReduce

2009-12-23 17:50:38

ADO.NET Fra

2020-05-13 11:32:28

数据分析数值分析

2020-05-26 16:25:33

Hadoop下载安装
点赞
收藏

51CTO技术栈公众号