PIL提供了通用的圖像處理功能,以及大量的基本圖像操作,如圖像縮放、裁剪、旋轉、顏色轉換等。
Matplotlib提供了強大的繪圖功能,其下的pylab/pyplot接口包含很多方便用戶創建圖像的函數。
為了觀察和進一步處理圖像數據,首先需要加載圖像文件,并且為了查看圖像數據,我們需要將其繪制出來。
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
# 加載圖像
img = Image.open("tmp.jpg")
# 轉為數組
img_data = np.array(img)
# 可視化
plt.imshow(img_data)
plt.show()
對于圖像,我們常見的操作有調整圖像尺寸,旋轉圖像以及灰度變換
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open("girl.jpg")
plt.figure()
# 子圖
plt.subplot(221)
# 原圖
plt.imshow(img)
plt.subplot(222)
# 將圖像縮放至 256 * 256
plt.imshow(img.resize((256, 256)))
plt.subplot(223)
# 將圖像轉為灰度圖
plt.imshow(img.convert('L'))
plt.subplot(224)
# 旋轉圖像
plt.imshow(img.rotate(45))
# 保存圖像
plt.savefig("tmp.jpg")
plt.show()
在平常的使用中,繪制圖像的輪廓也經常被使用,因為繪制輪廓需要對每個坐標(x, y)的像數值施加同一個闕值,所以需要將圖像灰度化
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
img = Image.open("girl.jpg")
gray_img = np.array(img.convert('L'))
plt.figure()
# 繪制圖像灰度化
plt.gray()
# 關閉坐標軸
plt.axis('off')
# 繪制灰度圖像
plt.contour(gray_img, origin='image')
plt.figure()
# 繪制直方圖,flatten()表示將數組展平
plt.hist(gray_img.flatten(), 128)
plt.show()
輪廓圖及直方圖:
圖像的直方圖用來表征該圖像的像素值的分布情況。用一定數目的小區間來指定表征像素值的范圍,每個小區間會得到落入該小區間表示范圍的像素數目。hist()函數用于繪制圖像的直方圖,其只接受一維數組作為第一個參數輸入,其第二個參數用于指定小區間的數目。
有時用戶需要和應用進行交互,如在一幅圖像中標記一些點。Pylab/pyplot庫中的ginput()函數就可以實現交互式標注
from PIL import Image
import matplotlib.pyplot as plt
img = Image.open(r"girl.jpg")
plt.imshow(img)
x = plt.ginput(3)
print("clicked point: ", x)
注:該交互在集成編譯環境(pyCharm)中如果不能調出交互窗口則無法進行點擊,可以在命令窗口下成功執行。
以上我們通過numpy的array()函數將Image對象轉換成了數組,以下將展示如何從數組轉換成Image對象
from PIL import Image
import numpy as np
img = Image.open(r"girl.jpg")
img_array = np.array(img)
img = Image.fromarray(img_array)
在圖像灰度變換中有一個非常有用的例子就是 直方圖均衡化 。直方圖均衡化是指將一幅圖像的灰度直方圖變平,使變換后的圖像中每個灰度值的分布概率都相同。直方圖均衡化通常是對圖像灰度值進行歸一化的一個非常好的方法,并且可以增強圖像的對比度。
直方圖均衡化的變換函數是圖像中像素值的 累積分布函數 (cumulative distribution function,將像素值的范圍映射到目標范圍的歸一化操作)。
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
def histogram_equalization(img: np, nbr_bins=256):
imhist, bins = np.histogram(img.flatten())
cdf = imhist.cumsum() # 累計分布函數
# 歸一化
cdf = 255 * cdf / cdf[-1]
# 使用累積分布函數進行線性插值,計算新的像素值
img2 = np.interp(img.flatten(), bins[:-1], cdf)
return img2.reshape(img.shape), cdf
img = Image.open(r"girl.jpg").convert('L')
img2, cdf = histogram_equalization(np.array(img))
plt.figure()
plt.gray()
# 繪制子圖
plt.subplot(232)
# 變換函數
plt.plot(cdf)
plt.subplot(231)
plt.hist(np.array(img).flatten(), 256)
# 關閉坐標軸,對上一個子圖有效
plt.axis('off')
plt.subplot(233)
plt.hist(np.array(img2).flatten(), 256)
plt.axis('off')
plt.subplot(234)
plt.imshow(img)
plt.axis('off')
plt.subplot(236)
plt.imshow(img2)
plt.axis('off')
# 保存繪制圖像
plt.savefig("tmp.jpg")
plt.show()
處理結果
可見,直方圖均衡化的圖像的對比度增強了,原先圖像灰色區域的斜街變得清晰。
PCA(Principal Component Analysis, 主成分分析)是一個非常有用的降維技巧,它可以在使用盡可能少的維數的前提下,盡可能多地保持訓練數據的信息。詳細介紹及使用見我的另一篇文章:PCA降維
SciPy是建立在Numpy基礎上,用于數值運算的開源工具包。Scipy提供很多高效的操作,可以實現數值積分、優化、統計、信號處理,以及對我們來說最為重要的圖像處理功能。
圖像的
高斯模糊
是非常經典的圖像卷積例子。本質上,圖像模糊就是將(灰度)圖像
\(I\)
和一個高斯核進行卷積操作:
\[ I_\sigma = I * G_\sigma \]
其中,
\(*\)
表示卷積操作;
\(G\)
表示標準差為
\(\sigma\)
的二維高斯核,定義為:
\[ G_\sigma = \frac{1}{2\pi \sigma^2} e^{-(x^2+y^2) / 2 \sigma^2} \]
高斯模糊通常是其他圖像處理操作的一部分,比如圖像插值操作、興趣點計算以及其他應用。
Scipy有用來做濾波操作的scipy.ndimage.filters模塊。該模塊使用快速一維分離的方式來計算卷積。使用方式:
from PIL import Image
import numpy as np
from scipy.ndimage import filters
img = Image.open(r"girl.jpg").convert('L')
img = np.array(img)
img2 = filters.gaussian_filter(img, 2)
img3 = filters.gaussian_filter(img, 5)
img4 = filters.gaussian_filter(img, 10)
上面使用的gaussian_filter()函數中的后一個參數表示標準差 \(\sigma\) ,可見隨著 \(\sigma\) 的增加,圖像變得越來越模糊。 \(\sigma\) 越大,處理后圖像細節丟失越多。如果是打算模糊一幅彩色圖像,只需要簡單地對每一個顏色通道進行高斯模糊:
from PIL import Image
import numpy as np
from scipy.ndimage import filters
img = Image.open(r"girl.jpg")
img = np.array(img)
img2 = np.zeros(img.shape)
for i in range(img2.shape[2]):
img2[:, :, i] = filters.gaussian_filter(img[:, :, i], 5)
# 將像素值用八位表示
img2 = np.array(img2, 'uint8')
在很多應用中,圖像強度的變化情況是非常重要的,強度的變化可以使用灰度圖像的 \(x\) 和 \(y\) 方向導數 \(I_x\) 和 \(I_y\) 進行描述
圖像的梯度向量為
\(\bigtriangledown I = [I_x, I_y]^T\)
。梯度有兩個重要屬性,一是梯度的大小:
\[ | \bigtriangledown I | = \sqrt{I_x^2 + I_y^2} \]
它描述了圖像強度變化的強弱,另一個是圖像的角度:
\[ \alpha = arctan2(I_x, I_y) \]
它描述了圖像在每個點上強度變化最大的方向。Numpy中的arctan2()函數返回弧度表示的有符號角度,角度的變化區間為
\((-\pi, \pi)\)
可以使用離散近似的方式來計算圖像的導數。圖像倒數大多數可以通過卷積簡單地實現:
\[ I_x = I*D_x 和 I_y = I*D_y \]
對于
\(D_x\)
和
\(D_y\)
,通常選擇Prewitt濾波器:
\[ D_x = \left[ \begin{matrix} -1 & 0 & 1 \\ -1 & 0 & 1 \\ -1 & 0 & 1 \end{matrix} \right] \]
和
\[ D_y = \left[ \begin{matrix} -1 & -1 & -1 \\ 0 & 0 & 0 \\ 1 & 1 & 1 \end{matrix} \right] \]
或者Sobel濾波器
\[ D_x = \left[ \begin{matrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{matrix} \right] \]
和
\[ D_y = \left[ \begin{matrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{matrix} \right] \]
這些導數濾波器可以使用scipy.ndimage.filters模塊地標準卷積操作來簡單地實現
from PIL import Image
import numpy as np
from scipy.ndimage import filters
img = Image.open(r"girl.jpg").convert('L')
img = np.array(img)
imgx = np.zeros(img.shape)
# Sobel導數濾波器
filters.sobel(img, 1, imgx)
imgy = np.zeros(img.shape)
filters.sobel(img, 0, imgy)
magnitude = np.sqrt(imgx**2+imgy**2)
sobel()函數的第二個參數選擇 \(x\) 或 \(y\) 方向的導數,第三個參數保存輸出變量。在圖像中,正導數顯示為亮的像素,負導數顯示為暗的像素,灰色區域表示導數的值接近零。
上面計算圖像導數的方法存在缺陷:在該方法中,濾波器的尺度需要隨著圖像分辨率的變化而變化(?)。為了在圖像噪聲方面更穩健,以及在任意尺度上計算導數,我們可以使用
高斯導數濾波器:
\[ I_x = I * G_{\sigma x} 和 I_y = I*G_{\sigma y} \]
其中,
\(G_{\sigma x}\)
和
\(G_{\sigma y}\)
表示
\(G_\sigma\)
在
\(x\)
和
\(y\)
方向上的導數,
\(G_\sigma\)
表示標準差為
\(\sigma\)
的高斯函數。以下給出使用樣例:
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import filters
img = Image.open(r"girl.jpg").convert('L')
img = np.array(img)
sigma = 2
imgx = np.zeros(img.shape)
imgy = np.zeros(img.shape)
filters.gaussian_filter(img, (sigma, sigma), (0, 1), imgx)
filters.gaussian_filter(img, (sigma, sigma), (1, 0), imgy)
magnitude = np.sqrt(imgx**2+imgy**2)
在對圖像進行處理時,去噪也是很重要的一環。 圖像去噪 是在去除圖像噪聲的同時,盡可能地保留圖像細節和結構地處理技術,以下給出使用ROF去噪模型地Demo:
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
from scipy.ndimage import filters
def de_noise(img, U_init, tolerance=0.1, tau=0.125, tv_weight=100):
U = U_init
Px = Py = img
error = 1
while error > tolerance:
Uold = U
# 變量U梯度的x分量
gradUx = np.roll(U, -1, axis=1)-U
# 變量U梯度的y分量
gradUy = np.roll(U, -1, axis=0)-U
# 更新對偶變量
PxNew = Px + (tau/tv_weight)*gradUx
PyNew = Py + (tau/tv_weight)*gradUy
NormNew = np.maximum(1, np.sqrt(PxNew**2+PyNew**2))
# 更新x,y分量
Px = PxNew / NormNew
Py = PyNew / NormNew
# 更新原始變量
RxPx = np.roll(Px, 1, axis=1) # 將x分量向x軸正方向平移
RyPy = np.roll(Py, 1, axis=0) # 將y分量向y軸正方向平移
DivP = (Px - RxPx) + (Py - RyPy) # 對偶域散度
U = img + tv_weight * DivP
error = np.linalg.norm(U - Uold)/np.sqrt(img.shape[0] * img.shape[1])
return U, img-U
if __name__ == '__main__':
im = np.zeros((500, 500))
im[100:400,100:400] = 128
im[200:300, 200:300] = 255
im = im + 30 * np.random.standard_normal((500, 500))
U, T = de_noise(im, im)
G = filters.gaussian_filter(im, 10)
plt.figure()
plt.gray()
plt.subplot(221).set_title("Original image")
plt.axis('off')
plt.imshow(im)
plt.subplot(222).set_title("Gauss blurred image")
plt.axis('off')
plt.imshow(G)
plt.subplot(223).set_title("ROF")
plt.axis('off')
plt.imshow(U)
plt.savefig('tmp.jpg')
plt.show()
ROF去噪后的圖像保留了邊緣和圖像的結構信息,同時模糊了“噪聲”。
np.roll()函數可以循環滾動元素,np.linalg.norm()用于衡量兩個數組間的差異。
之后有空將補充圖像去噪
參考書籍
Python計算機視覺
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
