# 數位影像處理(成大)
[toc]
## 1.Introduction
### 1.1 數位影像處理 Digital image processing
類比影像 → 量化(quantization)是數位化amplitude、採樣(sampling)是數位化座標 → 數位影像
也就是將影像離散化(discrete)的過程,讓電腦能夠處理。
取樣率(切割影像的精度),其數位影像的大小與空間的解析度(resolution)直接相關,
#### 數位影像分為 Color image 跟 gray image
每一格叫 Pixal,其值成為 gray-level 或 RGB三張影像通道 稱作 影像平面(image plane)

#### 空間的座標系
影像簡單的概念就是 空間位置+數值 的 Array
原點在左上角 y→ x↓

#### 影像處理 Image processing
對大小、數值的 Transformation
包含變形、噪點等變化

#### 影像分析 Image analysis
對影像的描述與識別等

#### 電腦視覺 Computer Vision
包括多張影像動作、參照、移動等分析

### 1.3 數位影像處理的領域
影像由波而來
構成紅外線、X光、可見光等等的影像

#### 1.3.1 Gamma-Ray Imaging
高能輻射,會直接穿透肌肉與水,硬組織穿透力差,光線被吸收後再透過底片以負片成像。

#### 1.3.2 X ray
與 1.3.1 同理,但能量較低(穿透力較弱)

#### 1.3.3 Ultraviolet Band 紫外線

#### 1.3.4 可見光 Visible 與 紅外線 Infrared
| Visible | Infrared |
| - | - |
| 如光學顯微鏡 | 如夜間拍攝 |
| ||
#### 1.3.6 核磁共振(無線電) Radio band
Magnetic Resonance Imaging(MRI)
如髖關節影像

#### Ultrasound 超音波
超音波由接收回波來成像
比如腹部(油、脂肪)

## 2.Digital Image Fundamentals 不重要
### 人的視覺
* 人的眼睛用視桿細胞、視錐細胞,分別感知光線明暗與動作、色彩細節。並透過水晶體調整焦距與範圍
* 人眼看到的並不是事實,有Optical illusions視錯覺
* Weber ratio 韋伯-費希納定理 心理量與物理量之間的定律,感覺量的大小與刺激強度的對數成正比
### 照相
成像是由光的能量構成,透過曝光將光線記錄到底片跟膠帶上。
分為:
* Single sensor
* Sensor stripes(Line sensor)
* Sensor arrays (Array sensor) 如X光
### Image interpolation 圖像插值法
利用已知數據推論未知圖像的方法,如放大、縮小等:
* nearest neighbor interpolation 最近鄰域法:放入原始圖中最接近的點

* Bilinear interpolation 線性內插法:參考周圍最接近的四點,按距離的權重決定填入多少



* Bicubic interpolation

### Basic relationships between pixels: pixel之間的基本關係
#### Neighbors of a pixel
* $N_4(p)$: 4-neighbors of p(x,y) 十字

* $N_D(p)$: diagonal neighbors of p(x,y) 對角線

* $N_8(p)$ = $N_4(p)∪N_D(p)$: 8-neighbors of p(x,y) 周圍

#### Adjacency and Connectivity
* 4-Adjacency in the set $N_4(p)$ p的上下左右皆為 Adjacency
* 8-Adjacency in the set $N_8(p)$ p的上下左右斜線皆為 Adjacency
* m-Adjacency(mixed adjacency)
* q is in $N_4(p)$ or
* q is in $N_D(p)$ and the set $N_4(p)∩N_D(p)$ has no pixels
 注意相鄰的線
#### Connected components

#### Regions: 指一個連通的區域
Adjacent: 兩個區域形成連接
Disjoint: 不相鄰,如DSU。
#### Boundaries
* Boundary: border or contour. In the region
當使用8-鄰接性時,圓圈中的點才會被視為1值像素區域的邊界

* Outer border: in the background

#### Edge: 則是邊緣不存在的資料
內部 Boundary 與 外部的中間
#### Distance measure
* Euclidean distance

* $D_4$ distance city-block

* $D_8$ distance

* $D_m$ distance m-path: 找包含兩者的最短路徑

### An Introduction to the Mathematical Tools Used in Digital Image Processing
#### Array versus Matrix Operation
* **array** product of these two **image**

* **matrix** product

#### Linear versus Nonlinear Operation

#### Addition (averaging) of noisy images for noise reduction 去除雜訊
* 圖像由原圖與雜訊構成:

* 如何將圖像去除雜訊

- 方法:同一個視角拍很多次,加起來k次取平均,降低noise的影響。


- 推導過程:

假設其 $\eta (x,y)$為常態分佈時,平均值為0,標準差為標準差noise或1。推出期望值為0。
根據大數理論,當k越大時,$\overline{g}(x,y)$效果越接近$f(x,y)$。

\begin{aligned}
\text{Var}(\overline{X}) =& \text{Var}\left(\frac{1}{n} \sum_{i=1}^{n} X_i\right)\\
=& \frac{1}{n^2} \sum_{i=1}^{n} \text{Var}(X_i)\\
\text{Var}(\overline{X}) =& \frac{1}{n^2} \cdot n \cdot \sigma^2 = \frac{\sigma^2}{n}\\
\text{Var}(\overline{X}) =& \frac{\sigma^2}{n}\\
\end{aligned}
#### Image <font color=red>subtraction</font> for enhancing differences 增強差異

* 概念:
只拍照物體但不要背景 = 拍一張背景 - 有物體的照片
有物體照片b - 背景a = c 物體的照片並抓出黑的背景
有物體照片b - c 黑背景= 物體照片
* 應用:

$(a)$原始圖像
$(b)$最低bit被設置為0
$(c)$a-b,並縮放到[0,255]

$(a)$先拍一張原圖(背景)
$(b)$再拍一張顯影劑的圖(有物體的圖)
$(c)$然後a-b 就會剩下左下心導管的影像
$(d)$再強化影像到右下圖
#### Using image <font color=red>multiplication</font> and <font color=red>division</font> for <font color=red>shading</font>(陰影) correction 陰影校正
* 工業檢測:乘以背景的倒數 = 原來影像

* 醫療影像:乘以背景的倒數,更清楚。

* 牙齒:拍全口 x 牙齒區塊是1其他0的mask = 就剩下單一牙齒

#### Full range of an arithmetic operation 改變範圍至[0,K]
改變 gray level 的 range 至 $[0,K]$

減掉最小,使$[0,max(f)]$

除最大值$[0,1]$,乘以k倍$[0,K]$
gray level 的 range 拉大到$[0,K]$的range
#### Set operations involving image intensities 圖像強度
正負片影像
$An=Ac={(x,y,255-z)| (x,y,z) ε A}$


#### Single-pixel operations 單點運算(強度值)
點$(x,y)$
原始 gray level $f(x,y) = z$
Transformation $f(x,y) = s$
$s = T(z)$
相同位置的轉換 → 單點運算(跟其他點無關)
只會改變影像中gray level上的值

#### Neighborhood operation 鄰域操作(強度值)

$n×m$的鄰域操作
該影像是透過n×m(41x41)的local框框中的gray level加起來做平均填回去
會讓影像**模糊化(平滑) smoothness**
#### <font color=red>Geometric spatial transformations</font> and image registration 空間轉換
* 幾何上空間的轉換,要做到 image registration 影像對位(對齊)

* 概念:不同大小的兩圖,上面的**單一點**如何對應到另一張圖的點,去做到T的spatial transformations 轉換。

affine transform 就是 linear transform
可以做到 scale, rotate, translate, or sheer 變形
都可經由這些矩陣達成

#### Image registration
* 影像對齊就是要找到 transform 的矩陣
* 兩圖分為 $(b)$moving image 跟 $(a)$reference image
moving image 找到對應的參考點tie points (control points):如四個角落的點,點的數量跟變數相關,才能找到對應方程式的解,貼到reference image上,得到結果$(c)$

但在做 interpolation 時還是會造成$(d)$影像失真
#### Vector and Matrix Operations

* Euclidean distance (vector norm1)

* linear transformations


* 標準的影像模型
拍出來的影像 $g=真實f經線性轉換H + noise$
要得到 $真實的圖像 = g - noise$ 做$H$反矩陣運算
* 問題:無法得知 $H$ 跟 $noise$
但假設 $n$ 是常態分布 再假設 $H$ 是旋轉等 找到反矩陣就能得到真實的$f$。
#### Image Transforms

* 傅立葉轉換後,再做反傅立葉轉換可以變回原圖
用這個方法可以對影像做處理,如下圖的恢復

* 有規則性的 $noise$ 圖$(a)$在傅立葉的圖$(b)$上也會顯示規律性雜訊,用mask$(c)$得到圖$(d)$,就能去除掉雜訊,又稱為濾波器。
## 3.Intensity transformation and spatial filtering
### 3.1 Background
#### intensity transformations
1. Spatial domain:空間(特定點)
2. Frequency domain:頻率域(Fourier)
##### Spatial domain
* 分單點 Single-pixel 跟鄰域 Larger neighborhood
* 單點 Single-pixel operations 就是 point processing

s=T( r )

* 跟gray level有關 跟位置無關
但會掃描所有影像的點 把該亮度改成上圖新的亮度做調整。
* 左圖是亮度拉長,右圖是閥值變二元圖
所以有很多不一樣的應用方式,依照找哪種亮度的位置,強化我想找到的東西。
* Larger neighborhood
* 該區域稱為 masks (filters, kernels, templates, windows).
處理稱作 mask processing or filtering

### 3.2 Some basic gray level transformations
常用

* Image negatives
* Log transformation
* Power-Law transformation
* Piecewise-linear transformations
#### Image negatives
黑轉白 白轉黑
為正負片(反轉)的差異
range [o,L-1]



#### Log transformation



more details
noise 更明顯
#### Power-Law transformation



##### gamma correction
* 調整 gamma 值 or 有不同效果


* gamma<1暗強化

* gamma>1亮強化

#### Piecewise-linear transformations
##### Contrast stretching
對比度強化
r1→r2小 < s1→s2大


##### Gray-level slicing

(b)二元值影像, ($c$)經驗設置

##### Bit-plane slicing
bit-slicing image

越上層越重要,越下層越不重要
可用來影像壓縮

### 3.3 Histogram Processing
* 明亮度的統計圖
range: [0, L-1]
$h(r_k) = n_k$
* normalized histogram
$p(r_k) = n_k/MN$

#### Histogram Equalization(必考)
直方圖等化:改為均勻分佈,減少環境帶來的差異。


找出a到b的uniform probability density function

證明以下公式:
$s=T(r), 0<=r<=L-1$
$T(r)$為 strictly monotonically increasing 嚴格單調遞增
inverse transformation: $r=T^{-1}(s), 0<=s<=L-1$ 不是函數(多r對映單一個s)
<font color=red>$p_s(s)=p_r(r)|\frac{dr}{ds}|$</font> 其中$p_r(r)原始影像的Histogram,是已知的$


* 連續型:$p_s(s)=p_r(r)|\frac{dr}{ds}|$ 推導$=\frac{1}{L-1},0<=s<=L-1$

變成 uniform distribution
* 離散型:


##### Example


小數做四捨五入(有誤差)
但0,2,4皆是空的
##### Histogram Matching (Specification)
(a)原始影像的 Histogram
(b)希望轉換後特定的 Histogram
($c$) b轉換成uniform,生成對照表


3沒有,所以填入嚴格遞增3<x<5,x填入最小的4
#### Local Histogram processing
對其中一塊的 neighborhood 鄰近區域做 Histogram equalization
然後 shift 到下一格持續做區域內的 processing
稱為 Local Histogram processing

#### Using histogram statistics for image enhancement
* n th moment n階動量:

* m is the mean value:

* $\mu_2, n=2$:

* the sample mean and sample variance:


* example

* 以(x,y)為中心的區域算mean

* 以(x,y)為中心的區域算variance

* Local enhancement

* example

### 3.4 Fundamentals of Spatial <font color='red'>Filtering</font>
* neighborhood operation

#### Filtering
* Correlation → 內積

* convolution → 將mask旋轉

##### Smoothing mask
lowpass filtering:模糊化,低頻的背景被留下。


加起來為1
* Gaussian Filter
距離越遠,權重越小
* kernel

### 3.5 Smoothing Spatial Filters

#### Order-Statistics Filters
ordering (ranking)
比如 median filter

### 3.6 Sharpening Spatial Filters
highpass filtering:邊緣、雜訊
一階微分與二階微分
一階微分:$f(x+1)-f(x)$ 後面減掉前一項,絕對值最大**可能**為edge
二階微分:$f(x+1)+f(x-1)-2f(x)$ 前後項相加減兩倍項,產生一亮一暗的邊中間為反曲點(zero crossing)是edge

#### The Laplacian(二階)




|上下左右四點相加-四倍原點|
* 左上角的mask:

* 亮暗邊

* 加到原圖

#### Unsharp masking and high-boost filtering
* 原圖 - 低頻(模糊) = 高頻(銳利化)

* 原圖 + 高頻 = 強化邊界

* 結果


#### The gradient(一階)


##### sobel operation

使用步驟:
1. 先使用$G_x$或$G_y$的mask
2. $|G_x(x_0,y_0)|+|G_y(x_0,y_0)|$ 或 $\sqrt{G_x(x_0,y_0)^2+G_y(x_0,y_0)^2}$ > 閥值 就是 edge 的位置
邊界會隨著閥值而有差別
## 4.Image Enhancement in the Frequency Domain
### Preliminary Concept
一個函數可以用sin, cos函數在不同週期情況下相加而成。
$j=\sqrt{-1}$
* Euler’s formula
$e^{j\theta}=cos\theta+jsin\theta$
$e^{j\theta_1}e^{j\theta_2}=e^{j(\theta_1+\theta_2)}$
* Fourier series
$f(t) = \sum_{n=-\infty}^{n=\infty} C_ne^\frac{j 2 \pi n t}{T}$

* <font color='red'>Fourier transform</font> in <font color='red'>continuous</font> domain

對$f(t)$作傅立葉轉換
* Fourier transform may be written for convenience as

與上者相同
* Inverse Fourier transform

可逆的
* Using Euler’s formula

* 舉例

$橫軸從t轉成\mu$
代入$w/2到-w/2$的範圍

* Fourier spectrum(energy)
$(r,\theta)$以下為r長度的統計圖,另外還有$\theta$的Fourier phase angle


#### <font color="red">Convolution</font>



兩個函數作Convolution的結果作Fourier = 各別作Fourier的乘積。
### Sampling and the Fourier Transform of Sampled Functions
#### Sampling

第一、二張圖作乘積變為採樣後的第四張圖

* 第一張為Fourier的圖
* 第二三四張為不同的採樣頻率:
* 第二張太大
* 中間剛好,週期相同

* 第三張太小


將離散的值作積分:

也就能直接推導成

### Discrete Fourier Transform (DFT) of One Variable
* Discrete Fourier transform (DFT)

* Inverse discrete Fourier transform (IDFT)

* DFT計算範例:

$e^{j\theta}=cos\theta+jsin\theta$
$e^{-j\theta}=cos\theta-jsin\theta$



* IDFT計算範例
### The 2-D Discrete Fourier Transform and Its Inverse 必考
* 2-D discrete Fourier transform (DFT)

* Inverse discrete Fourier transform (IDFT)

* 觀念

* 計算範例

* 圖片示例

原圖→Fourier→原圖先經由以下公式轉換後再Fourier

會將亮點移到中間,方便處理


### Fourier Spectrum and Phase Angle

### zero padding
5x5 3x3 -> 9x9 padding zero
nxn mxm
n+m+1
### The 2-D convolution Theorem




### Frequency Domain Filtering Foundamentals

為什麼轉到 Frequency Domain ,因為在空間域做convolution需要花很多時間。
左:低頻,均值被拉高,模糊化
中右:高頻,背景消失,邊緣強化
### Summary of Steps for Filtering in the Frequency Domain
1. MxN -> PxQ, P = 2M and Q = 2N.
2. padded
3. f$_p(x, y)$ by $(-1)^{x+y}$
4. DFT
5. 位置相乘 array multiplication
6. IDFT

7. MxN 挖出來

### Extension to Functions of Two Variables
## 5. Image Restoration
### A model of the image degradation/restoration process

意外加入了雜訊,如何去做還原。
### Noise Models
#### Gaussian (normal) noise model


#### Rayleigh noise model
超音波雜訊

PDF:

Mean and Variance:


#### Other
* Erlang (gamma) noise model

* Exponential noise model

* Uniform noise model

* Impulse (salt-and-pepper) noise model



### Restoration in the presence of noise only-spatial filtering

無法完全去除雜訊的影響,只能盡可能減少
#### Mean Filters 考試
$\hat{f(x,y)}$皆是估計出來的,而非原圖$f(x,y)$
* Arithmetic mean filter

算出mask的算術平均值:mxn的mask,mask內相加除以mxn。
畫面:越大越模糊,越小存在更多雜訊
計算:越大越久,越小越快
* Geometric mean filter

算出mask的幾何平均值:mask內相乘開mxn的根號
* Harmonic mean filter

算出mask的調和平均值
* Contraharmonic mean filter

* 還原效果


#### Order-Statistics Filters
* Median filter

1. 取出排序
2. 填入中位數
* Max and min filters


* Midpoint filter

取最大跟最小除二
* Alpha-trimmed mean filters

砍掉頭尾d個算中間的平均
* 還原效果



#### Adaptive Filters
## Faster-RCNN: Anchor Base
1. RPN(Region Proposal Network)
Anchor 瞄框
GT 真實框
去產生提議框(Proposal box) -> 正提議框(I.O.U≧0.7), 負提議框(I.O.U≦0.3)
2. 提取提議框在特徵圖內的區域(部分)的特徵圖,再以提議框與真實框來提取bounding box(結果)
R.O.I Pooling, R.O.I Aligment 提議框大小一致
3. NMS(non-maximun supression)來去除多餘框
非最大值壓抑
TWO-stage methon