OpenCV 教學範例程式

Part 3

Enhancement

主要章節

  • 08_histogram
  • 09_convolution
  • 10_smoothing
  • 11_gradient
  • 12_sharpness

08_histogram

直方圖

影像亮度分佈

直方圖計算

Gamma Correction

直方圖均化

Histogram Equalization

......

09_convolution

卷積運算

過濾圖像是用相鄰像素的線性組合
取得每個像素的新值 (特徵圖)

Filtering 過濾

Corelation & Convolution

Correlation 關注的是兩個事物之間的相關性,而 Convolution 更多的是一個信號疊加的概念

Filtering

  1. 在圖像上滑動相關核的中心
  2. 將相關核中的每個權重乘以圖像中的像素
  3. 總結這些相乘的值

Correlation

Convolution

在兩個維度上翻轉過濾器(從下到上,從右到左)

再進行相關運算

這例子不好

計算方式

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
1 1 1
1 1 0
1 0 0
23

(10+20+30+20+60+30) / 6 = 23

Kernel

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
1 1 1
1 1 0
1 0 0
23 33

(20+30+40+30+40+40) / 6 = 33

Kernel

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
1 1 1
1 1 0
1 0 0
23 33 43

(30+40+50+40+50+50) / 6 = 43

Kernel

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
1 1 1
1 1 0
1 0 0
23 33 43
33

(20+30+40+30+40+40) / 6 = 33

Kernel

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
1 1 1
1 1 0
1 0 0
23 33 43
33 43

(30+40+50+40+50+60) / 6 = 43

Kernel

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
1 1 1
1 1 0
1 0 0
23 33 43
33 43 53

(40+50+60+50+60+60) / 6 = 53

Kernel

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80

由於原圖像為 (4x5),而計算之結果為 (2x3),尺寸不合

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80

故將原圖外加一圈,而外框需補上的數值有許多方法

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
0 0 0 0 0 0 0
0 10 20 30 40 50 0
0 20 30 40 50 60 0
0 30 40 50 60 70 0
0 40 50 60 70 80 0
0 0 0 0 0 0 0

方法一:給予固定數值 (cv2.BORDER_CONSTANT)

Correlation

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
30 20 30 40 50 60 50
20 10 20 30 40 50 40
30 20 30 40 50 60 50
40 30 40 50 60 70 60
50 40 50 60 70 80 70
40 30 40 50 60 70 60

方法二:取鏡像對稱的像素值 (cv2.BORDER_DEFAULT)

Correlation

1 1 1
1 1 0
1 0 0

(30+20+30+20+10+30) / 6 = 23

Kernel

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
30 20 30 40 50 60 50
20 10 20 30 40 50 40
30 20 30 40 50 60 50
40 30 40 50 60 70 60
50 40 50 60 70 80 70
40 30 40 50 60 70 60
23
23 33 43
33 43 53

Correlation

1 1 1
1 1 0
1 0 0

(20+30+40+10+20+20) / 6 = 23

Kernel

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
30 20 30 40 50 60 50
20 10 20 30 40 50 40
30 20 30 40 50 60 50
40 30 40 50 60 70 60
50 40 50 60 70 80 70
40 30 40 50 60 70 60
23 23
23 33 43
33 43 53

Correlation

1 1 1
1 1 0
1 0 0

全部依此規則計算

Kernel

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
30 20 30 40 50 60 50
20 10 20 30 40 50 40
30 20 30 40 50 60 50
40 30 40 50 60 70 60
50 40 50 60 70 80 70
40 30 40 50 60 70 60
23 23 33 43 50
23 23 33 43 50
33 33 43 53 60
40 40 50 60 67

所得到的陣列
和原本一樣大

Correlation

1 1 1
1 1 0
1 0 0

Kernel

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
23 23 33 43 50
23 23 33 43 50
33 33 43 53 60
40 40 50 60 67

Convolution

0 0 1
0 1 1
1 1 1

Kernel

10 20 30 40 50
20 30 40 50 60
30 40 50 60 70
40 50 60 70 80
23 30 40 50 50
30 37 47 57 57
40 47 57 67 67
40 47 57 67 67
1 1 1
1 1 0
1 0 0

Correlation vs Convolution

0 0 1
0 1 1
1 1 1

Kernel

23 30 40 50 50
30 37 47 57 57
40 47 57 67 67
40 47 57 67 67
1 1 1
1 1 0
1 0 0
23 23 33 43 50
23 23 33 43 50
33 33 43 53 60
40 40 50 60 67

Kernel Fllip

OpenCV 裡面沒有可以直接做卷積濾波的函式。要進行卷積濾波,有兩種不同的方法:

  1. 在代碼中在兩個維度上翻轉內核,然後調用 filter2D 函數。
  2. 使用 scipy 庫代替 OpenCV 進行卷積過濾。

OpenCV: The filter2D function does actually compute correlation, not the convolution: That is, the kernel is not mirrored around the anchor point. If you need a real convolution, flip the kernel using cv::flip and set the new anchor to `(kernel.cols - anchor.x - 1, kernel.rows - anchor.y - 1)`.

另一種邊緣的處理

Image & Kernel Convolutions

原始圖形資料

Images & Kernel Convolutions

(7+6+84) / 9 = 10.78

Kernel Convolutions

Convolutions 計算後

Convolutions 計算後

1 1 1
1 1 1
1 1 1

彩色圖為各通道都進行運算

不同的 Kernel

1 0 -1
1 0 -1
1 0 -1
1 0 -1
1 0 -1
1 0 -1
1 1 1
0 0 0
-1 -1 -1
-1 0 1
-1 0 1
-1 0 1
-1 -1 -1
0 0 0
1 1 1

不同的 Kernel

Kernel Convolutions

Blur Filter

Sharpen Filter

Edge Filter

Emboss Filter

Horizontal Derivative Filter

Vertical Derivative Filter

Sobel Operator

10_smoothing

平滑化

Filter 濾波器

......

11_gradient

圖像梯度

......

12_sharpness

銳化

......

......

Made with Slides.com