OpenCV-Python實戰(4)——OpenCV常見影像處理技術(??萬字長文,含大量示例??)
- 0. 前言
- 1. 拆分與合并通道
- 2. 影像的幾何變換
- 2.1 縮放影像
- 2.2 平移影像
- 2.3 旋轉影像
- 2.4 影像的仿射變換
- 2.5 影像的透視變換
- 2.6 裁剪影像
- 3. 影像濾波
- 3.1 應用濾波器(卷積核或簡稱為核)
- 3.2 影像平滑
- 3.2.1 均值濾波
- 3.2.2 高斯濾波
- 3.2.3 中值濾波
- 3.2.4 雙邊濾波
- 3.3 影像銳化
- 3.4 影像處理中的常用濾波器
- 總結
- 相關鏈接
0. 前言
影像處理技術是計算機視覺專案的核心,通常是計算機視覺專案中的關鍵工具,可以使用它們來完成各種計算機視覺任務,因此,如果要構建計算機視覺專案,就需要對影像處理有足夠的了解,在本文中,將介紹計算機視覺專案中常見的影像處理技術,主要包括影像的幾何變換和影像濾波等,
1. 拆分與合并通道
再進行影像處理時,有時我們僅需要使用特定通道,因此,必須首先將多通道影像拆分為多個單通道影像,為了拆分通道,可以使用 cv2.split() 函式,cv2.split() 函式將源多通道影像拆分為多個單通道影像,此外,處理完成后,可能希望將多個的單通道影像合并創建為一個多通道影像,為了合并通道,可以使用 cv2.merge() 函式,cv2.merge() 函式將多個單通道影像合并為一個多通道影像,
使用 cv2.split() 函式,從加載的 BGR 影像中獲取三個通道:
# 通道拆分
image = cv2.imread('sigonghuiye.jpeg')
(b, g, r) = cv2.split(image)
使用 cv2.merge() 函式,將三個通道合并構建 BGR 影像:
# 通道合并
image_copy = cv2.merge((b, g, r))
需要注意的是,cv2.split() 是一個耗時的操作,所以應該只在絕對必要的時候使用它,作為替代,可以使用 NumPy 切片語法來處理特定通道,例如,如果要獲取影像的藍色通道:
b = image[:, :, 0]
此外,可以消除多通道影像的某些通道(通過將通道值設定為 0 ),得到的影像具有相同數量的通道,但相應通道中的值為 0;例如,如果要消除 BGR 影像的藍色通道:
image_without_blue = image.copy()
image_without_blue[:, :, 0] = 0
消除其他通道的方法與上述代碼原理相同:
# 紅藍通道
image_without_green = image.copy()
image_without_green[:,:,1] = 0
# 藍綠通道
image_without_red = image.copy()
image_without_red[:,:,2] = 0
然后將得到的影像的通道拆分:
(b_1, g_1, r_1) = cv2.split(image_without_blue)
(b_2, g_2, r_2) = cv2.split(image_without_green)
(b_3, g_3, r_3) = cv2.split(image_without_red)
顯示拆分后的通道:
def show_with_matplotlib(color_img, title, pos):
# Convert BGR image to RGB
img_RGB = color_img[:,:,::-1]
ax = plt.subplot(3, 6, pos)
plt.imshow(img_RGB)
plt.title(title,fontsize=8)
plt.axis('off')
image = cv2.imread('sigonghuiye.jpeg')
plt.figure(figsize=(13,5))
plt.suptitle('splitting and merging channels in OpenCV', fontsize=12, fontweight='bold')
show_with_matplotlib(image, "BGR - image", 1)
show_with_matplotlib(cv2.cvtColor(b, cv2.COLOR_GRAY2BGR), "BGR - (B)", 2)
show_with_matplotlib(cv2.cvtColor(g, cv2.COLOR_GRAY2BGR), "BGR - (G)", 2 + 6)
show_with_matplotlib(cv2.cvtColor(r, cv2.COLOR_GRAY2BGR), "BGR - (R)", 2 + 6 * 2)
show_with_matplotlib(image_merge, "BGR - image (merge)", 1 + 6)
show_with_matplotlib(image_without_blue, "BGR without B", 3)
show_with_matplotlib(image_without_green, "BGR without G", 3 + 6)
show_with_matplotlib(image_without_red, "BGR without R", 3 + 6 * 2)
show_with_matplotlib(cv2.cvtColor(b_1, cv2.COLOR_GRAY2BGR), "BGR without B (B)", 4)
show_with_matplotlib(cv2.cvtColor(g_1, cv2.COLOR_GRAY2BGR), "BGR without B (G)", 4 + 6)
show_with_matplotlib(cv2.cvtColor(r_1, cv2.COLOR_GRAY2BGR), "BGR without B (R)", 4 + 6 * 2)
# 顯示其他拆分通道的方法完全相同,只需修改通道名和子圖位置
# ...
plt.show()
代碼運行結果如下:

在上圖中,也可以更好的看出 RGB 顏色空間的加法屬性,例如,沒有 B 通道的子圖大部分是黃色的,這是因為 綠色+紅色 會得到黃色值,
2. 影像的幾何變換
幾何變換主要包括縮放、平移、旋轉、仿射變換、透視變換和影像裁剪等,執行這些幾何變換的兩個關鍵函式是 cv2.warpAffine() 和 cv2.warpPerspective(),
cv2.warpAffine() 函式使用以下 2 x 3 變換矩陣來變換源影像:
d
s
t
(
x
,
y
)
=
s
r
c
(
M
11
x
+
M
12
y
+
M
13
,
M
21
x
+
M
22
y
+
M
23
)
dst(x,y)=src(M_{11}x+M_{12}y+M_{13}, M_{21}x+M_{22}y+M_{23})
dst(x,y)=src(M11?x+M12?y+M13?,M21?x+M22?y+M23?)
cv2.warpPerspective() 函式使用以下 3 x 3 變換矩陣變換源影像:
d
s
t
(
x
,
y
)
=
s
r
c
(
M
11
x
+
M
12
y
+
M
13
M
31
x
+
M
32
y
+
M
33
,
M
21
x
+
M
22
y
+
M
23
M
31
x
+
M
32
y
+
M
33
)
dst(x,y)=src(\frac {M_{11}x+M_{12}y+M_{13}} {M_{31}x+M_{32}y+M_{33}}, \frac {M_{21}x+M_{22}y+M_{23}} {M_{31}x+M_{32}y+M_{33}})
dst(x,y)=src(M31?x+M32?y+M33?M11?x+M12?y+M13??,M31?x+M32?y+M33?M21?x+M22?y+M23??)
接下來,我們將了解最常見的幾何變換技術,
2.1 縮放影像
縮放影像時,可以直接使用縮放后影像尺寸呼叫 cv2.resize():
# 指定縮放后影像尺寸
resized_image = cv2.resize(image, (width * 2, height * 2), interpolation=cv2.INTER_LINEAR)
除了上述用法外,也可以同時提供縮放因子 fx 和 fy 值,例如,如果要將影像縮小 2 倍:
# 使用縮放因子
dst_image = cv2.resize(image, None, fx=0.5, fy=0.5, interpolation=cv2.INTER_AREA)
如果要放大影像,最好的方法是使用 cv2.INTER_CUBIC 插值方法(較耗時)或 cv2.INTER_LINEAR,如果要縮小影像,一般的方法是使用 cv2.INTER_LINEAR,
OpenCV 提供的五種插值方法如下表所示:
| 插值方法 | 原理 |
|---|---|
| cv2.INTER_NEAREST | 最近鄰插值 |
| cv2.INTER_LINEAR | 雙線性插值 |
| cv2.INTER_AREA | 使用像素面積關系重采樣 |
| cv2.INTER_CUBIC | 基于4x4像素鄰域的3次插值 |
| cv2.INTER_LANCZOS4 | 正弦插值 |
顯示縮放后的影像:
def show_with_matplotlib(color_img, title, pos):
# Convert BGR image to RGB
img_RGB = color_img[:,:,::-1]
ax = plt.subplot(1, 3, pos)
plt.imshow(img_RGB)
plt.title(title,fontsize=8)
# plt.axis('off')
show_with_matplotlib(image, 'Original image', 1)
show_with_matplotlib(dst_image, 'Resized image', 2)
show_with_matplotlib(dst_image_2, 'Resized image 2', 3)
plt.show()
可以通過坐標系觀察圖片的縮放情況:

2.2 平移影像
為了平移物件,需要使用 NumPy 陣列創建 2 x 3 變換矩陣,其中提供了 x 和 y 方向的平移距離(以像素為單位):
M = np.float32([[1, 0, x], [0, 1, y]])
其對應于以下變換矩陣:
[
1
0
t
x
0
1
t
y
]
\begin{bmatrix} 1 & 0 & t_x \\ 0 & 1 & t_y \end{bmatrix}
[10?01?tx?ty??]
創建此矩陣后,呼叫 cv2.warpAffine() 函式:
dst_image = cv2.warpAffine(image, M, (width, height))
cv2.warpAffine() 函式使用提供的 M 矩陣轉換源影像,第三個引數 (width, height) 用于確定輸出影像的大小,
例如,如果圖片要在 x 方向平移 200 個像素,在 y 方向移動 30 像素:
height, width = image.shape[:2]
M = np.float32([[1, 0, 200], [0, 1, 30]])
dst_image_1 = cv2.warpAffine(image, M, (width, height))
平移也可以為負值,此時為反方向移動:
M = np.float32([[1, 0, -200], [0, 1, -30]])
dst_image_2 = cv2.warpAffine(image, M, (width, height))
顯示圖片如下:

2.3 旋轉影像
為了旋轉影像,需要首先使用 cv.getRotationMatrix2D() 函式來構建 2 x 3 變換矩陣,該矩陣以所需的角度(以度為單位)旋轉影像,其中正值表示逆時針旋轉,旋轉中心 (center) 和比例因子 (scale) 也可以調整,使用這些元素,以下方式計算變換矩陣:
[
α
β
(
1
?
a
)
?
c
e
n
t
e
r
.
x
?
β
?
c
e
n
t
e
r
.
y
?
β
α
β
?
c
e
n
t
e
r
.
x
?
(
1
?
α
)
?
c
e
n
t
e
r
.
y
]
\begin{bmatrix} \alpha & \beta & (1-a)\cdot center.x-\beta\cdot center.y \\ -\beta & \alpha & \beta\cdot center.x-(1-\alpha)\cdot center.y \end{bmatrix}
[α?β?βα?(1?a)?center.x?β?center.yβ?center.x?(1?α)?center.y?]
其中:
α
=
s
c
a
l
e
?
c
o
s
θ
,
β
=
s
c
a
l
e
?
s
i
n
θ
\alpha=scale\cdot cos\theta, \beta=scale\cdot sin\theta
α=scale?cosθ,β=scale?sinθ
以下示例構建 M 變換矩陣以相對于影像中心旋轉 180 度,縮放因子為 1(不縮放),之后,將這個 M 矩陣應用于影像,如下所示:
height, width = image.shape[:2]
M = cv2.getRotationMatrix2D((width / 2.0, height / 2.0), 180, 1)
dst_image = cv2.warpAffine(image, M, (width, height))
接下來使用不同的旋轉中心進行旋轉:
M = cv2.getRotationMatrix2D((width/1.5, height/1.5), 30, 1)
dst_image_2 = cv2.warpAffine(image, M, (width, height))
顯示旋轉后的影像:

2.4 影像的仿射變換
在仿射變換中,首先需要使用 cv2.getAffineTransform() 函式來構建 2 x 3 變換矩陣,該矩陣將從輸入影像和變換影像中的相應坐標中獲得,最后,將 M 矩陣傳遞給 cv2.warpAffine():
pts_1 = np.float32([[135, 45], [385, 45], [135, 230]])
pts_2 = np.float32([[135, 45], [385, 45], [150, 230]])
M = cv2.getAffineTransform(pts_1, pts_2)
dst_image = cv2.warpAffine(image_points, M, (width, height))
仿射變換是保留點、直線和平面的變換,此外,平行線在此變換后將保持平行,但是,仿射變換不會同時保留像素點之間的距離和角度,
可以通過以下影像觀察仿射變換的結果:

2.5 影像的透視變換
為了進行透視變換,首先需要使用 cv2.getPerspectiveTransform() 函式創建 3 x 3 變換矩陣,該函式需要四對點(源影像和輸出影像中四邊形的坐標),函式會根據這些點計算透視變換矩陣,然后,將 M 矩陣傳遞給 cv2.warpPerspective() :
pts_1 = np.float32([[450, 65], [517, 65], [431, 164], [552, 164]])
pts_2 = np.float32([[0, 0], [300, 0], [0, 300], [300, 300]])
M = cv2.getPerspectiveTransform(pts_1, pts_2)
dst_image = cv2.warpPerspective(image, M, (300, 300)
透視變換效果如下所示:

2.6 裁剪影像
可以使用 NumPy 切片裁剪影像:
dst_image = image[80:200, 230:330]
裁剪結果如下所示:

3. 影像濾波
在本節中,將介紹如何模糊和銳化影像,然后應用自定義核,此外,還將介紹一些用于執行其他影像處理功能的常見核,
3.1 應用濾波器(卷積核或簡稱為核)
OpenCV 提供了 cv2.filter2D() 函式,以將任意核應用于影像,將影像與提供的核進行卷積操作,為了使用此函式,首先需要構建將使用的核:
# 使用 5 x 5 核
kernel_averaging_5_5 = np.array([[0.04, 0.04, 0.04, 0.04, 0.04], [0.04,
0.04, 0.04, 0.04, 0.04], [0.04, 0.04, 0.04, 0.04, 0.04],[0.04, 0.04, 0.04,
0.04, 0.04], [0.04, 0.04, 0.04, 0.04, 0.04]])
以上示例創建了 5 x 5 平均卷積核,也可以使用以下方式創建卷積核核:
kernel_averaging_5_5 = np.ones((5, 5), np.float32) / 25
然后將應用 cv2.filter2D() 函式將核應用于源影像:
smooth_image_f2D = cv2.filter2D(image, -1, kernel_averaging_5_5)
上述方法可以將任意核應用于影像,示例中,創建了一個平均卷積核來平滑影像,或者,我們也可以使用 OpenCV 內置的函式,從而在無需創建核的情況下執行影像平滑(也稱為影像模糊),
3.2 影像平滑
平滑技術通常用于減少噪聲,此外,這些技識訓可用于減少低解析度影像中的像素化,
3.2.1 均值濾波
可以使用 cv2.blur() 或 cv2.boxFilter() 通過將影像與核卷積來執行均值濾波,在使用 cv2.boxFilter() 時可以不執行規范化,只是取核區域下所有像素的平均值,并用這個平均值替換中心元素,可以控制核大小和錨點位置(默認情況下錨點位于核中心),當 cv2.boxFilter() 的 normalize 引數等于 True 時,兩個函式完全等價,兩個函式都使用如下核平滑影像:
K
=
α
[
1
1
?
1
1
1
?
1
?
?
?
?
1
1
?
1
]
K=\alpha\begin{bmatrix} 1 & 1 & \cdots & 1 \\ 1 & 1 & \cdots & 1\\ \vdots&\vdots&\ddots&\vdots \\ 1 & 1 & \cdots & 1 \end{bmatrix}
K=α??????11?1?11?1??????11?1???????
cv2.boxFilter() 函式:
α
=
{
k
s
i
z
e
.
w
i
d
t
h
?
k
s
i
z
e
.
h
e
i
g
h
t
,
if normalize=true
1
,
otherwise
\alpha = \begin{cases} ksize.width\cdot ksize.height, & \text{if normalize=true} \\[2ex] 1, & \text{otherwise} \end{cases}
α=????ksize.width?ksize.height,1,?if normalize=trueotherwise?
在 cv2.blur() 函式的情況下:
α
=
k
s
i
z
e
.
w
i
d
t
h
?
k
s
i
z
e
.
h
e
i
g
h
t
\alpha = ksize.width\cdot ksize.height
α=ksize.width?ksize.height
換句話說, cv2.blur() 是使用歸一化的 boxFilter():
# 以下兩行代碼是等價的
smooth_image_b = cv2.blur(image, (10, 10))
smooth_image_bfi = cv2.boxFilter(image, -1, (10, 10), normalize=True)
均值濾波后的影像如下所示:

3.2.2 高斯濾波
OpenCV 提供了 cv2.GaussianBlur() 函式用于高斯濾波,該函式使用高斯核對影像進行模糊處理,可以使用以下引數控制高斯核:ksize (核大小)、sigmaX (高斯核 x 方向的標準差) 和 sigmaY (高斯核 y 方向的標準差),為了獲取所應用的高斯核,可以使用 cv2.getGaussianKernel() 函式構建高斯核:
# (9, 9)表示高斯矩陣的長與寬都是5,標準差取0
smooth_image_gb = cv2.GaussianBlur(image, (9, 9), 0)
# 標準差取0.3
smooth_image_gb_2 = cv2.GaussianBlur(image, (9, 9), 0.3)
# 構建高斯核
print(cv2.getGaussianKernel(9,0))
高斯濾波后的影像如下所示:

3.2.3 中值濾波
OpenCV 提供了 cv2.medianBlur() 函式用于中值濾波,該函式使用中值核對影像進行模糊處理:
smooth_image_mb = cv2.medianBlur(image, 9)
smooth_image_mb_2 = cv2.medianBlur(image, 3)
此濾波器可用于減少影像中的椒鹽噪聲,效果如下:

3.2.4 雙邊濾波
cv2.bilateralFilter() 函式應用于輸入影像可以執行雙邊濾波,與上述所有平滑濾波器傾向于全域平滑不同,此函式可用于在保持邊緣銳利的同時減少噪聲:
smooth_image_bf = cv2.bilateralFilter(image, 5, 10, 10)
雙邊濾波效果如下圖所示:

3.3 影像銳化
銳化影像的邊緣的一種簡單方法是執行非銳化蒙版 (unsharp masking),即從原始影像中減去影像的非銳化或平滑版本,在以下示例中,首先應用了高斯平滑濾波器,然后從原始影像中減去生成的影像:
smoothed = cv2.GaussianBlur(img, (9, 9), 10)
unsharped = cv2.addWeighted(img, 1.5, smoothed, -0.5, 0)
另一種方法是使用特定的核來銳化邊緣,然后應用 cv2.filter2D() 函式:
kernel_sharpen_1 = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
kernel_sharpen_2 = np.array([[-1, -1, -1],
[-1, 9, -1],
[-1, -1, -1]])
kernel_sharpen_3 = np.array([[1, 1, 1],
[1, -7, 1],
[1, 1, 1]])
kernel_sharpen_4 = np.array([[-1, -1, -1, -1, -1],
[-1, 2, 2, 2, -1],
[-1, 2, 8, 2, -1],
[-1, 2, 2, 2, -1],
[-1, -1, -1, -1, -1]]) / 8.0
sharp_image_1 = cv2.filter2D(image, -1, kernel_sharpen_1)
sharp_image_2 = cv2.filter2D(image, -1, kernel_sharpen_2)
sharp_image_3 = cv2.filter2D(image, -1, kernel_sharpen_3)
sharp_image_4 = cv2.filter2D(image, -1, kernel_sharpen_4)
銳化后影像輸出如下所示:

3.4 影像處理中的常用濾波器
還可以定義了一些用于不同目的的通用核,例如:邊緣檢測、平滑、銳化或浮雕等,定義內核后,可以使用 cv2.filter2D() 函式:
image = cv2.imread('sigonghuiye.jpeg')
kernel_identity = np.array([[0, 0, 0],
[0, 1, 0],
[0, 0, 0]])
# 邊緣檢測
kernel_edge_detection_1 = np.array([[1, 0, -1],
[0, 0, 0],
[-1, 0, 1]])
kernel_edge_detection_2 = np.array([[0, 1, 0],
[1, -4, 1],
[0, 1, 0]])
kernel_edge_detection_3 = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
# 銳化
kernel_sharpen = np.array([[0, -1, 0],
[-1, 5, -1],
[0, -1, 0]])
kernel_unsharp_masking = -1 / 256 * np.array([[1, 4, 6, 4, 1],
[4, 16, 24, 16, 4],
[6, 24, -476, 24, 6],
[4, 16, 24, 16, 4],
[1, 4, 6, 4, 1]])
# 模糊
kernel_blur = 1 / 9 * np.array([[1, 1, 1],
[1, 1, 1],
[1, 1, 1]])
gaussian_blur = 1 / 16 * np.array([[1, 2, 1],
[2, 4, 2],
[1, 2, 1]])
# 浮雕
kernel_emboss = np.array([[-2, -1, 0],
[-1, 1, 1],
[0, 1, 2]])
# 邊緣檢測
sobel_x_kernel = np.array([[1, 0, -1],
[2, 0, -2],
[1, 0, -1]])
sobel_y_kernel = np.array([[1, 2, 1],
[0, 0, 0],
[-1, -2, -1]])
outline_kernel = np.array([[-1, -1, -1],
[-1, 8, -1],
[-1, -1, -1]])
# 應用卷積核
original_image = cv2.filter2D(image, -1, kernel_identity)
edge_image_1 = cv2.filter2D(image, -1, kernel_edge_detection_1)
edge_image_2 = cv2.filter2D(image, -1, kernel_edge_detection_2)
edge_image_3 = cv2.filter2D(image, -1, kernel_edge_detection_3)
sharpen_image = cv2.filter2D(image, -1, kernel_sharpen)
unsharp_masking_image = cv2.filter2D(image, -1, kernel_unsharp_masking)
blur_image = cv2.filter2D(image, -1, kernel_blur)
gaussian_blur_image = cv2.filter2D(image, -1, gaussian_blur)
emboss_image = cv2.filter2D(image, -1, kernel_emboss)
sobel_x_image = cv2.filter2D(image, -1, sobel_x_kernel)
sobel_y_image = cv2.filter2D(image, -1, sobel_y_kernel)
outline_image = cv2.filter2D(image, -1, outline_kernel)

總結
在本文中,介紹了計算機視覺專案中常見的影像處理技術,主要包括影像的幾何變換和影像濾波等,
相關鏈接
OpenCV-Python實戰(1)——OpenCV簡介與影像處理基礎(內含大量示例,📕建議收藏📕)
OpenCV-Python實戰(2)——影像與視頻檔案的處理(兩萬字詳解,?📕建議收藏📕)
OpenCV-Python實戰(3)——OpenCV中繪制圖形與文本(萬字總結,?📕建議收藏📕)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/298670.html
標籤:python
