自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

教程:使用Python進行基本圖像數(shù)據(jù)分析!

大數(shù)據(jù)
本教程將介紹如何導(dǎo)入圖像并觀察其屬性、拆分圖層以及查看灰度。在正式開始之前,我們先來了解一些關(guān)于像素的基礎(chǔ)知識。

本教程將介紹如何導(dǎo)入圖像并觀察其屬性、拆分圖層以及查看灰度。在正式開始之前,我們先來了解一些關(guān)于像素的基礎(chǔ)知識。

計算機將圖片以像素形式存儲,這就像馬賽克一樣。如果像素太大,很難制作光滑的邊緣和曲線。相反,我們使用的像素越多越小,看起來就會越平滑,或者說像素化程度越小,圖像就會越好看,有時,這也被稱為圖像分辨率。 

矢量圖形是一種有點不同的存儲圖像方法,旨在避免與像素相關(guān)的問題。但是,即使是矢量圖像,最終也會顯示為像素一樣的馬賽克。顏色像素表示圖像元素,描述每個像素的簡單方法是使用三種顏色的組合,即紅色,綠色,藍色,這就是我們所說的RGB圖像。

在RGB圖像中,每個像素分別與紅色,綠色,藍色的值相關(guān)聯(lián)的三個8比特數(shù)字表示。最后,如果使用放大鏡觀察縮放的圖片,我們會看到圖片由微小的光點或更具體的像素組成,更有趣的是這些小光點實際上具有多個不同顏色。

每張照片都以數(shù)字形式由像素組成,它們是構(gòu)成圖片的最小信息單位,通常是圓形或方形,它們通常布置在二維網(wǎng)格中。 

如果三個顏色都處于最大值,則意味著它們是255,那就會顯示為白色,如果三種顏色都處于最小值,或者值為0,則顏色顯示為黑色。反過來,這三者的組合將為我們提供特定的像素顏色。由于每個顏色數(shù)字都是8個比特,因此值范圍為0-255。 

由于每個值可以具有256個不同的強度或亮度值,因此三種顏色總共有1680萬個shade。 

以下是Numpyand非?;镜膱D像數(shù)據(jù)分析步驟,其中一些涉及Python pacakges,如imageio,matplotlib等。

  • 導(dǎo)入圖像并觀察其屬性
  • 拆分圖層
  • Greyscale
  • 對像素值使用邏輯運算符
  • 使用邏輯運算符進行運算
  • 衛(wèi)星圖像數(shù)據(jù)分析
  • 導(dǎo)入圖像

現(xiàn)在讓我們加載圖像并觀察各種屬性:

  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的形狀表明它是一個三層矩陣,這里的前兩個數(shù)字是長度和寬度,第三個數(shù)字(即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范圍。

現(xiàn)在,使用圖片分配變量,我們還可以訪問圖片的任何特定像素值,并進一步訪問每個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,我們可以意識到這個特殊像素中有很多綠色?,F(xiàn)在,我們可以通過給出三個通道的索引值來特別選擇其中一個數(shù)字:

  • 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 

好的,現(xiàn)在讓我們快速查看整個圖像中的每個頻道。

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

 

現(xiàn)在,我們可以更改RGB值的數(shù)量。例如,讓我們對紅色、綠色、藍色圖層設(shè)置跟隨行值的強度。

  • 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進行基本圖像數(shù)據(jù)分析!

  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進行基本圖像數(shù)據(jù)分析!

  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進行基本圖像數(shù)據(jù)分析!

為了更清楚,讓我們也改變列部分,這次我們將同時更改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進行基本圖像數(shù)據(jù)分析!

拆分圖層

現(xiàn)在,我們知道圖像的每個像素都由三個整數(shù)表示,將圖像分割成單獨的顏色分片只需拉出圖像陣列的正確切片。

  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進行基本圖像數(shù)據(jù)分析!

灰度

黑白圖像存儲在二維陣列中,有兩種類型的黑白圖像:

  • Greyscale:灰色陰影范圍:0~255
  • Binary:像素為黑色或白色:0或255

現(xiàn)在,Greyscaling是一個將圖像從全色轉(zhuǎn)換為灰色陰影的過程。在圖像處理工具中,例如:在OpenCV中,許多功能在處理之前使用灰度圖像,這樣做是因為它簡化了圖像,幾乎可以降噪并增加處理時間,因為圖像中的信息較少。

在python中有兩種方法可以將圖像轉(zhuǎn)換為灰度,但使用matplotlib的簡單方法是使用此公式獲取原始圖像的RGB值的加權(quán)平均值。

  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進行基本圖像數(shù)據(jù)分析!

然而,GIMP將顏色轉(zhuǎn)換為灰度圖像軟件有三種算法來完成任務(wù)。

灰度的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 

使用邏輯運算符處理像素值

我們可以使用邏輯運算符創(chuàng)建相同大小的bullion ndarray。但是,這不會創(chuàng)建任何新數(shù)組,它只是將值返回到其主變量。例如,如果考慮在RGB圖像中濾除一些低值像素或高值或(任何條件),可以先將RGB轉(zhuǎn)換為灰度。

首先加載圖像并在屏幕上顯示:

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

教程:使用Python進行基本圖像數(shù)據(jù)分析!

接下來,我們考慮轉(zhuǎn)儲該圖像,比如我們想要過濾所有低于20 的像素值。為此,我們將使用邏輯運算符執(zhí)行此任務(wù),返回所有索引的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) 

正如上文所說,傳統(tǒng)上不使用宿主變量,但我之所以提到是因為它只保留True值。 所以,如果我們看到low_pixel和pic的 shape,我們會發(fā)現(xiàn)它們都具有相同的 shape。

  1. print(pic.shape)  
  2. print(low_pixel.shape)  
  3. (1079, 1293, 3)  
  4. (1079, 1293, 3) 

我們使用全局比較運算符為所有小于200的值生成低值濾波器。但是,我們可以使用此low_pixel數(shù)組作為索引將這些低值設(shè)置為某些特定值,這些值可能高于或低于先前的像素值。

  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進行基本圖像數(shù)據(jù)分析!

圖層蒙版

圖像蒙版是一種圖像處理技術(shù),用于去除具有模糊邊緣,透明或頭發(fā)部分的照片背景。

現(xiàn)在,我們將創(chuàng)建一個圓盤形狀的蒙版。首先,我們將測量從圖像中心到每個邊界像素值的距離。我們設(shè)置一個比較方便的半徑值,然后使用邏輯運算符創(chuàng)建一個圓盤,以下為代碼:

  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進行基本圖像數(shù)據(jù)分析!

衛(wèi)星圖像處理

衛(wèi)星圖像及其處理系統(tǒng)非常有用,我們可以用于做一些分析任務(wù)。

  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進行基本圖像數(shù)據(jù)分析!

我們來看一些基本信息:

  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層中的顏色都有自己的意思。例如,紅色的強度將表示像素中的地理數(shù)據(jù)點的高度,藍色的強度表示方位的度量,綠色表示斜率。這些顏色將有助于以更快,更有效的方式傳達此信息,而不是顯示數(shù)字。

  • 紅色像素表示:Altitude·
  • 藍色像素表示:Aspect
  • 綠色像素表示: Slope

通過觀察彩色圖像,我們可以分辨出海拔是多少,斜率是多少,以及Slope是什么,這就是為顏色加載更多含義以表示更科學(xué)的分析的想法。

檢測每個通道的像素

  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進行基本圖像數(shù)據(jù)分析!

未完待續(xù)......這只是該教程的第一章節(jié),其他內(nèi)容將會在后續(xù)章節(jié)中呈現(xiàn)。

責(zé)任編輯:未麗燕 來源: IT168
相關(guān)推薦

2018-07-24 16:05:58

2024-07-01 13:51:14

2017-09-26 19:02:09

PythonInstagram數(shù)據(jù)分析

2018-07-24 16:00:38

2019-01-15 14:21:13

Python數(shù)據(jù)分析數(shù)據(jù)

2020-10-28 18:28:12

Pandas數(shù)據(jù)分析GUI

2020-07-09 15:21:58

大數(shù)據(jù)RStudioR語言

2020-04-30 16:38:21

數(shù)據(jù)分析可視化代碼

2017-04-26 14:02:18

大數(shù)據(jù)數(shù)據(jù)分析Excel

2020-06-05 14:29:07

PythonPandas數(shù)據(jù)分析

2022-06-09 11:47:21

工具數(shù)據(jù)儀連接器

2024-07-30 12:10:22

2016-12-20 16:42:57

iPhone數(shù)據(jù)分析

2020-02-20 10:45:51

Python數(shù)據(jù)疾病

2021-12-28 11:23:36

SQLServerExcel數(shù)據(jù)分析

2013-06-27 15:21:38

App

2017-02-06 14:12:29

大數(shù)據(jù)數(shù)據(jù)分析基本思想

2020-12-17 09:45:54

數(shù)據(jù)分析互聯(lián)網(wǎng)大數(shù)據(jù)

2012-08-08 09:53:23

HadoopMapReduce

2009-12-23 17:50:38

ADO.NET Fra
點贊
收藏

51CTO技術(shù)棧公眾號