Chang, Wei Che
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    影像處理 === *** 2025 成大資工研究所課程 25/10/24: 更新到Ch2 25/10/30: 更新到Ch3 25/11/13: 期中考 *** # Chapter 1 ## 1.1 What is DIP(digital image processing) ### 1.1.1名詞解釋 #### Image - 2-D 函式 *f(x, y)*. - *x, y* : 空間座標 - amplitude(振幅) *f* : intensity(強度) or gray-level(灰階) #### Digital Image - *x, y, f* 都 **離散(discrete)** 的image - *f* 可為RGB或多光譜(multispectral)的image #### Video - *f(x, y, t)* : frames. #### Digital Image Processing - 由電腦或硬體將影像轉為數位(digital) - 輸入與輸出皆為 **影像** #### Digital Image Analysis - 識別與描述影像內容 - 輸入為影像,輸出為 **象徵性描述(symbolic description)** 的有意義資訊 #### Computer Vision - 使用數位電子設備 **模擬(emulate)** 人類視覺 ## 1.2 The Origins of DIP **廢話** ## 1.3 Examples of Fields that use DIP 好像也不是太重要,認個圖。 ![image](https://hackmd.io/_uploads/BJx5uFdRlg.png) ### 1.3.1 Gamma-Ray ![image](https://hackmd.io/_uploads/rkgOFKO0xg.png) a. 注入放射性同位素,其衰變時釋放正子;正子與電子碰撞湮滅,產生兩個伽馬射線 ### 1.3.2 X-Ray ![image](https://hackmd.io/_uploads/r19wqK_Axx.png) ### 1.3.3 Ultraviolet Band (UV紫外線) ![image](https://hackmd.io/_uploads/BybJiKOCex.png) ### 1.3.4 Visible & infrared Band (可見光) ![image](https://hackmd.io/_uploads/HyKBjFu0xx.png) ### 1.3.5 LANDSAT-7 (衛星主題波段) - 用了紅字,意義不明 ![image](https://hackmd.io/_uploads/S1C12YdAxe.png) ### 1.3.6 Microwave Band (微波) - 太空雷達影像 ![image](https://hackmd.io/_uploads/r1Hz2KORel.png) ### 1.3.7 Radio (無線電波) #### Magnetic Resonance Imaging (MRI) ![image](https://hackmd.io/_uploads/SJpCnY_Cll.png) ### 1.3.8 Others #### Ultrasound Imaging (聲納) ![image](https://hackmd.io/_uploads/ByIYpY_0ex.png) #### SEM Imaging ![image](https://hackmd.io/_uploads/B1tqpYOAxe.png) #### 3D computer Models ![image](https://hackmd.io/_uploads/r1GopFuRex.png) ## 1.4 樹梅派 (Raspberry Pi) ![image](https://hackmd.io/_uploads/BkhD6Yd0el.png) ![image](https://hackmd.io/_uploads/rkMd6Fu0gx.png) # Chapter 2 ## 2.1 Elements of Visual Perception ### 2.1.1 Structure of the Human Eye #### 角膜(Cornea) & 晶體(Lens) - 將影像聚焦到視網膜(retina)上 #### 虹彩(Iris) - 控制進入瞳孔(pupil)的光線量 ### 2.1.2 Image Formation - 影像為倒置,大腦修正為正向 - 要會算成像大小(下圖) ![image](https://hackmd.io/_uploads/HJ59U5d0lg.png) ### 2.1.3 Brightness Adaptation and Discrimination #### 馬赫帶效應(Mach Band Effect) - 在不同亮度的邊界處,人眼會感知到實際不存在的更亮或更暗的條紋。 ![image](https://hackmd.io/_uploads/B157v9dRlg.png) #### 同時對比(Simultaneous Contrast) - 感知到的亮度不僅取決於該區域的強度,也受周圍背景的影響。 ![image](https://hackmd.io/_uploads/HyGIwcdCge.png) #### 視覺幻覺(Optical illusions) - 透過幾何排列、視覺成像規律等手段,製作有「視覺欺騙」成分的圖像進行眼球欺騙。 ![image](https://hackmd.io/_uploads/HJ0nwqORle.png) ## 2.2 Light & the Electromagnetic Spectrum ### 2.2.1 Electromagnetic(EM) Spectrum - 可見光光譜只占電磁光譜(EM Spectrum)的極小部分 - 約430nm(紫)到790nm(紅) ![image](https://hackmd.io/_uploads/BJEEu5O0gg.png) ### 2.2.2 EM expression - 能量 $E = h\nu$($h$ = 普朗克常數, $\nu$ = 頻率)。 - 波長 $\lambda = \frac{c}{\nu}$($c$ = 光速)。 ![image](https://hackmd.io/_uploads/Bk_h_qdAgl.png) ## 2.3 Image Sensing & Acquisition ### 2.3.1 影像擷取 ![image](https://hackmd.io/_uploads/S1mk39dCel.png) #### 單一感測器(Single Sensor) - 如光電二極體(photodiode),需移動以掃描整個影像 #### 條狀感測器 (Sensor Strips) - 用於線掃描(line-scan)應用,如影印機、CAT掃描。 #### 陣列感測器 (Sensor Arrays) - 如CCD(Charge-Coupled Device)或CMOS(Complementary Metal-Oxide Semiconductor)元件,用於數位相機,可一次擷取2D影像 。 ### 2.3.2 影像形成模型(Image Formatoion Model) - $f(x,y) = i(x,y) \cdot r(x,y)$ - $f(x,y)$: 影像強度 - $i(x,y)$: **照明 (illumination)** (光源能量, $0 < i(x,y) < \infty$) - $r(x,y)$: **反射 (reflectance)** (物體反射比例, $0 < r(x,y) < 1$) ### 2.3.3 灰階(Gray Level & GrAY Scale) - Gray Level: 單色(monochrome)影像的強度(intensity) - Gray Scale: $[L_{min},L_{max}]$的區間,通常為$[0,L-1]$ - $l = f(x,y),L_{min} \le l \le L_{max}$ ## 2.4 Image Sampling and Quantization ![image](https://hackmd.io/_uploads/B1l9Byo_Aeg.png) ### 2.4.1 取樣(Sampling) - 在**座標**上進行離散化 ### 2.4.2 量化(Quantization) - 在**振幅**上進行離散化 ### 2.4.3 Dynamic Range & Image Storage Size - Gray level通常為**二的次方**($L=2^{k}$) #### 動態範圍(Dynamic Range) - 影像中灰階所橫跨的值域(the range of value spanned by the gray levels)。 - 雜訊(Noise) - 在最暗的區域,雜訊會以顆粒狀紋理出現,這會掩蓋掉真正最低的可偵測強度值。 - 飽和(Saturation) - 在最亮的區域,當強度高過一個上限時,所有值都會被clipped成一個固定的最高值。 #### Image Storage Size - Bits Required = $M*N*k\ (L=2^{k})$ ![image](https://hackmd.io/_uploads/By_KzsdRex.png) ### 2.4.4 Spatial Resolution - 影像中**可辨識的最小細節** - 由**取樣(Sampling)** 決定 ![image](https://hackmd.io/_uploads/SJyeSj_Cxe.png) ### 2.4.5 Intensity Resolution - $k$值大小決定影像深度 - 由**量化(Quantization)** 決定 ![image](https://hackmd.io/_uploads/SJwdSsu0ge.png) ### 2.4.6 Level of Details - 影像的複雜程度 ![image](https://hackmd.io/_uploads/rJusHsOAex.png) #### ISO-preference curves - 描述影像**主觀品質(subjective quality)**的一種方式 - 這條曲線是繪製在一個 $N-k$ 平面上 - **N 軸(X軸):** 代表**空間解析度**(Spatial Resolution)。 - **k 軸(Y軸):** 代表**灰階解析度**(Gray-level Resolution,即位元深度)。 - 曲線解讀 - 曲線上的相同點表示有**相同品質(equal subject quality)** - **Crowd(高細節):** 曲線較陡峭。這意味著對於高細節影像,**空間解析度($N$)** 對主觀品質的影響遠大於灰階解析度($k$)。 - **Face(低細節):** 曲線較平緩。這意味著在細節較少的影像中,**灰階解析度($k$)** 的重要性相對提高。 - 記憶:**低細節重深度**!!! ![image](https://hackmd.io/_uploads/r15zOo_Rge.png) ### 2.4.7 Image Interpolation - 利用**已知數據**來**估計未知位置上的值**的過程 #### Neareat Neighbor Interpolation - 新網格直接疊加在舊網格上面 - 快速、鋸齒明顯 #### Bilinear Interpolation - 原始影像中**最近4個鄰居**($2\times2$鄰域)來估算新像素的值。 - 公式: $v(x,y) = ax + by + cxy + d$ #### BiCubic Interpolation - 使用原始影像中**最近16個鄰居**($4\times4$鄰域)來估算新像素的值。 - 公式: $v(x,y) = \sum_{i=0}^{3}\sum_{j=0}^{3} a_{ij}x^{i}y^{j}$ - 慢、最為平滑 ![image](https://hackmd.io/_uploads/S1jtsoO0lg.png) ## 2.5 Some Basic Relationships Between Pixels ### 2.5.1 Neighbors of a pixel #### $N_4(p)$ (4-鄰居) - 這是 $p$ 的水平和垂直鄰居。 - 其座標集合為:$(x+1, y), (x-1, y), (x, y+1), (x, y-1)$。 #### $N_D(p)$ (對角鄰居) - 這是 $p$ 的對角鄰居。 - 其座標集合為:$(x+1, y+1), (x+1, y-1), (x-1, y+1), (x-1, y-1)$。 #### $N_8(p)$ (8-鄰居) - 這是 $p$ 的 4-鄰居和對角鄰居的聯集 (union)。 - $N_8(p) = N_4(p) \cup N_D(p)$。 ### 2.5.2 Adjacency - 鄰接性用於定義像素間的連接。我們需要一個強度值集合 $V$ 來定義鄰接性。 #### 4-鄰接 (4-adjacency) - 如果 $q$ 在 $N_4(p)$ 集合中,則兩個來自 $V$ 集合的像素 $p$ 和 $q$ 是 4-鄰接的。 #### 8-鄰接 (8-adjacency) - 如果 $q$ 在 $N_8(p)$ 集合中,則兩個來自 $V$ 集合的像素 $p$ 和 $q$ 是 8-鄰接的。 #### m-鄰接 (mixed adjacency) - 這是 8-鄰接的修改版,用以消除歧義。 - 如果 $p$ 和 $q$ 的值都在 $V$ 集合中,它們是 m-鄰接的,若: 1. $q$ 在 $N_4(p)$ 中,**或** 2. $q$ 在 $N_D(p)$ 中,**且** $N_4(p) \cap N_4(q)$ 集合中沒有來自 $V$ 集合的像素。 ![image](https://hackmd.io/_uploads/H1mAPeY0ex.png) ### 2.5.3 Connectivity - 看起來同離散的概念 #### Digital path/curve - 從座標 $(x,y)$ 的像素 $p$ 到座標 $(s,t)$ 的像素 $q$ 的路徑,是一系列座標為 $(x_0, y_0), (x_1, y_1), ..., (x_n, y_n)$ 的相異像素序列。 - 其中 $(x_0, y_0) = (x, y)$ 且 $(x_n, y_n) = (s, t)$。 - 對於 $1 \le i \le n$, $(x_i, y_i)$ 和 $(x_{i-1}, y_{i-1})$ 必須是鄰接的。 - $n$ 是路徑的長度。 - 如果 $(x_0, y_0) = (x_n, y_n)$,則這是一條 **封閉路徑(Closed Path)**。 #### Connected - S 是一個影像中的像素子集。 - 如果 S 中的兩個像素 $p$ 和 $q$ 之間存在一條完全由 S 中像素組成的路徑,則稱 $p$ 和 $q$ 在 S 中是連通的。 #### Connected component - 對於 S 中的任何像素 $p$,S 中所有與 $p$ 連通的像素集合,稱為 S 的一個 **Connected Component**。 #### Connected set - 如果 S 僅包含一個 **Connected Component**,則 S 稱為一個 **連通集(Connected Set)**。 ### 2.5.4 Regions #### Region - 如果一個像素子集 $R$ 是一個 **Connected Set**,則 $R$ 稱為一個區域。 #### Adjacent - 如果兩個區域的 **聯集(union)** 是一個 **Connected Set**,則它們是鄰近的。 ![image](https://hackmd.io/_uploads/ryuZ3eYRle.png) #### Disjoint - 不鄰近的區域。 ### 2.5.5 Boundaries #### 內邊界 (inner border) - 一個區域 $R$ 的邊界,是 $R$ 中這樣一個像素集合:這些像素至少有一個鄰居不在 $R$ 中。 #### 外邊界 (Outer border) - 背景中對應的邊界。 ### 2.5.3 Distance measures #### Distance function / metric - 對於座標分別為 $(x, y), (s, t), (v, w)$ 的像素 $p, q, z$, $D$ 是一個距離函數,若滿足: 1. $D(p, q) \ge 0$ ($D(p, q) = 0$ iff $p = q$) 2. $D(p, q) = D(q, p)$ (對稱性) 3. $D(p, z) \le D(p, q) + D(q, z)$ (三角不等式) #### Euclidean Distance - $D_e(p, q) = [(x-s)^2 + (y-t)^2]^{1/2}$ #### $D_4$ (City-Block Distance) - $D_4(p, q) = |x-s| + |y-t|$ - $D_4 = 1$ 的像素即是 $(x, y)$ 的 4-鄰居。 #### $D_8$ (Chessboard Distance) - $D_8(p, q) = \max(|x-s|, |y-t|)$ - $D_8 = 1$ 的像素即是 $(x, y)$ 的 8-鄰居。 #### $D_m$ - 定義為兩點之間的最短 m-路徑 **(m-adjacency)**。 ## 2.6 Mathematical Tools used in DIP ### 2.6.1 Array vs Matrix Operation #### 陣列乘積 (Array product) - 這是 **逐元素 (element-by-element)** 的運算。 - $$\begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix} = \begin{bmatrix} a_{11}b_{11} & a_{12}b_{12} \\ a_{21}b_{21} & a_{22}b_{22} \end{bmatrix}$$ #### 矩陣乘積 (Matrix product) - 這是標準的矩陣數學運算。 - $$\begin{bmatrix} a_{11} & a_{12} \\ a_{21} & a_{22} \end{bmatrix} \begin{bmatrix} b_{11} & b_{12} \\ b_{21} & b_{22} \end{bmatrix} = \begin{bmatrix} a_{11}b_{11}+a_{12}b_{21} & a_{11}b_{12}+a_{12}b_{22} \\ a_{21}b_{11}+a_{22}b_{21} & a_{21}b_{12}+a_{22}b_{22} \end{bmatrix}$$ ### 2.6.2 Linear & Nonlinear Operations - 一個運算子 $H$ 將輸入影像 $f(x,y)$ 轉換為輸出影像 $g(x,y)$,即 $H[f(x,y)] = g(x,y)$。 #### 線性運算子 (Linear Operator) - 如果 $H[a_i f_i(x,y) + a_j f_j(x,y)] = a_i H[f_i(x,y)] + a_j H[f_j(x,y)]$,則 $H$ 稱為線性運算子。 - 例如,**總和 (Sum, $\Sigma$)** 運算子是線性的。 #### 非線性運算子 (Nonlinear Operator) - 不滿足上述條件的即為非線性。 - 例如,**max** 運算子是非線性的(可用上點反證非線性)。 ### 2.6.3 Arithmetic Operations #### 影像間的算術運算是陣列運算,意指在對應的像素對之間進行。 - **加法:** $s(x,y) = f(x,y) + g(x,y)$ - **減法:** $d(x,y) = f(x,y) - g(x,y)$ - **乘法:** $p(x,y) = f(x,y) \times g(x,y)$ - **除法:** $v(x,y) = f(x,y) \div g(x,y)$ #### 應用:影像加法 (平均) 進行降噪 - 模型:$g(x,y) = f(x,y) + \eta(x,y)$。 - 將 $K$ 個雜訊影像平均:$\overline{g}(x,y) = \frac{1}{K}\sum_{i=1}^{K}g_i(x,y)$。 - 平均影像的標準差 $\sigma_{\overline{g}(x,y)}$ 降為 $\frac{1}{\sqrt{K}}\sigma_{\eta(x,y)}$。 - **前提:** 影像必須先**對齊 (registered)**。 #### 應用:影像減法 強化差異 - $g(x,y) = f(x,y) - h(x,y)$。 - 主要應用是**數位減法血管攝影 (Digital Subtraction Angiography, DSA)**。 - ![image](https://hackmd.io/_uploads/HyfDvbKCeg.png) #### 應用:影像乘法/除法 進行陰影校正 (Shading Correction) - 將有陰影的影像乘以陰影圖案的倒數,可得到校正後的影像。 ![image](https://hackmd.io/_uploads/SkavwWtRel.png) - 乘法也可用於**感興趣區域 (ROI) 遮罩**。 ![image](https://hackmd.io/_uploads/BJD9vZK0ex.png) #### Full Range - 為了將結果縮放到固定的 $K$ 位元範圍($[0, K]$): - 正規化到 $[0, 1]$,乘上 $K$ 1. $f_m = f - \min(f)$ (移至 $0$) 2. $f_s = K[f_m / \max(f_m)]$ (縮放到 $K$) ### 2.6.4 Set and Logical Operations #### Set Operations - **聯集 ($A \cup B$)**、**交集 ($A \cap B$)**、**補集 ($A^c$)**、**差集 ($A - B$)**。 - **德摩根定律 (DeMorgan's laws):** $(A \cup B)^c = A^c \cap B^c$ 和 $(A \cap B)^c = A^c \cup B^c$。 ![image](https://hackmd.io/_uploads/SkOQY-t0ex.png) #### Gray-Level Image Operation - **補集 (影像負片):** $A^c = \{(x, y, 255-z) | (x, y, z) \in A\}$。 - **聯集:** $A \cup B = \{\max_z(a, b) | a \in A, b \in B\}$。 ![image](https://hackmd.io/_uploads/ryrKtWKRel.png) #### Logical Operation - NOT, AND, OR, XOR ![image](https://hackmd.io/_uploads/SJO0tbKRex.png) #### Fuzzy Sets - 用於處理隸屬關係不明確的集合。 - (我看起來)非0即1的反話 - 使用**隸屬函數 (membership function)**,其值可在 0 和 1 之間漸變。 ### 2.6.5 Spatial Operations #### 單像素運算 (Single-pixel operations):** - $s = T(z)$,$T$ 是一個轉換函數。 - 例如:影像負片 $s = (L-1) - z,\ L=2^k$。 ![image](https://hackmd.io/_uploads/r16XibKAxx.png) #### 鄰域運算 (Neighborhood operation):** - 輸出像素的值由輸入像素 $(x,y)$ 的一個鄰域 $S_{xy}$ 決定。 - 例如:**Local averaging** $g(x,y) = \frac{1}{mn}\sum_{(r,c) \in S_{xy}} f(r,c)$。 ![image](https://hackmd.io/_uploads/HyrnoZF0gx.png) #### Geometric spatial transformations - 兩個操作: 1. **座標的空間轉換**:$(x, y) = T\{(v, w)\}$。 2. **強度內插 (Intensity interpolation)**。 - **仿射轉換 (Affine Transform):** - $[x, y, 1] = [v, w, 1]T$。 - 可實現**scale**、**rotate**、**translate**或**sheer**。 ![image](https://hackmd.io/_uploads/ByPb3bF0lg.png) #### 影像對齊 (Image Registration):** - 目的是估計轉換函數,以對齊**輸入影像**與**參考影像**。 - **繫點 (Tie points / Control points):** 是解決此問題的主要方法之一。 - 可使用**雙線性模型 (bilinear approximation)** 來求解轉換係數。 - $x = c_1v + c_2w + c_3vw + c_4$ - $y = c_5v + c_6w + c_7vw + c_8$ ### 2.6.6 Vector and Matrix Operations - 像素可以表示為向量。例如,RGB 影像的像素 $z = [R, G, B]^T$。 - **Euclidean distance:** $D(z,a) = [(z-a)^T(z-a)]^{1/2} = ||z-a||$ (vector norm)。 - **Linear Transformation:** $w = A(z-a)$。 - 一個 $M \times N$ 的影像可以被表示為一個 $MN \times 1$ 的向量 (攤平)。 - 線性過程可表示為 $g = Hf + n$。 *** 此處以下內容我都不知道在幹嘛 *** ### 2.6.7 Image Transforms - 有些任務最好在**轉換域 (transform domain)** 中進行。 #### 流程 - 空間域 $f(x,y)$ $\rightarrow$ 轉換 $T(u,v)$ $\rightarrow$ 運算 $R[T(u,v)]$ $\rightarrow$ 反轉換 $g(x,y)$。 #### 2-D 線性轉換 (General Form) - **正向轉換:** $T(u,v) = \sum_{x} \sum_{y} f(x,y) r(x,y,u,v)$ - **反向轉換:** $f(x,y) = \sum_{u} \sum_{v} T(u,v) s(x,y,u,v)$ - $r(...)$ 和 $s(...)$ 分別是**正向**和**反向**的**轉換核 (kernel)**。 #### 可分離核 (Separable Kernel) - $r(x,y,u,v) = r_1(x,u) r_2(y,v)$。 #### 離散傅立葉轉換 (DFT) 看第四章 - 正向核: $r(x,y,u,v) = e^{-j2\pi(ux/M + vy/N)}$ - 反向核: $s(x,y,u,v) = \frac{1}{MN} e^{j2\pi(ux/M + vy/N)}$ #### 矩陣形式 - 如果核是可分離且對稱的 ($T = AFA$)。 - 反轉換為 $F = BTB$。如果 $B = A^{-1}$,則 $F$ 可以完全恢復。 ### 2.6.8 Probabilistic Method #### 強度等級 $z_k$ 出現的機率 $p(z_k)$ - $p(z_k) = n_k / (MN)$,其中 $n_k$ 是 $z_k$ 強度出現的次數。 - $\sum_{k=0}^{L-1} p(z_k) = 1$。 #### 影像特徵 - **平均強度 (Mean):** $m = \sum_{k=0}^{L-1} z_k p(z_k)$ - **變異數 (Variance, $\sigma^2$):** $\sigma^2 = \sum_{k=0}^{L-1} (z_k - m)^2 p(z_k)$ - **$n$ 階動差 (nth moment):** $\mu_n(z) = \sum_{k=0}^{L-1} (z_k - m)^n p(z_k)$ - $\mu_2(z)=\sigma^2$(變異數)可作為影像**對比度 (contrast)** 的量測。 ![image](https://hackmd.io/_uploads/H1QWyfFCxe.png) # Chapter 3 ## 3.1 Background ### 3.1.1 影像增強的兩大類別 - **空間域 (Spatial Domain):** (The image plane itself, dealing directly with pixel values)。直接對影像中的像素進行操作。 - **頻率域 (Frequency Domain):** (Deals with the image's rate of change in pixel values, often manipulated via Fourier Transform)。透過修改影像的傅立葉轉換來達成。 ### 3.1.2 空間域處理 (Spatial Domain Process) - 可表示為 $g(x,y) = T[f(x,y)]$,其中 $f(x,y)$ 是輸入影像, $g(x,y)$ 是處理後的影像,T 是在 (x,y) 點的某個鄰域 (Neighborhood - a set of pixels surrounding a central pixel) 上定義的運算子。 #### 點處理 (Point Processing) - 最簡單的形式是當鄰域大小為 1x1。 - 轉換函式簡化為 $s = T(r)$,其中 r 是 $f(x,y)$ 的灰階,s 是 $g(x,y)$ 的灰階。 - 範例:對比度拉伸 (Contrast stretching) 和閾值處理 (Thresholding)。 ![image](https://hackmd.io/_uploads/rk0TUv90gx.png) #### 遮罩處理 (Mask Processing / Filtering) - (An operation where the new pixel value depends on the values in its neighborhood, as defined by a mask or kernel)。 - 當使用大於 1x1 的鄰域時,會使用「遮罩」(mask)(也稱為濾波器 (filter)、核心 (kernel)、模板 (template) 或窗口 (window))。 ## 3.2 Some Basic Gray Level Transformations ![image](https://hackmd.io/_uploads/SJFLDw5Cxl.png) ### 3.2.1 Image Negative - 公式: $s = L - 1 - r$,適用於灰階範圍在 $[0, L-1]$ 的影像。 - 用途: 增強嵌入在黑暗區域中的 **白色或灰色細節** (例如:乳房 X 光攝影影像)。 ![image](https://hackmd.io/_uploads/H1j-OP9Alx.png) ### 3.2.2 Log Transformation (值取Log) - 公式: $s = c \log(1 + r)$,其中 c 是常數,且 $r \ge 0$。 - 用途: 壓縮像素值變化範圍大的影像之動態範圍。例如,用於顯示 **傅立葉頻譜 (Fourier spectrum)**。 ![image](https://hackmd.io/_uploads/ryDLdDq0lx.png) ### 3.2.3 Power-Law Transformation - 公式: $s = cr^{\gamma}$,其中 c 和 $\gamma$ 是常數。 #### Gamma ( $\gamma$ ) 的影響 - $\gamma < 1$:擴展暗態灰階,壓縮亮態灰階。適用於需要灰階擴展的影像(例如:對比度不足的 MRI 影像)。 - $\gamma > 1$:壓縮暗態灰階,擴展亮態灰階。適用於需要灰階壓縮的影像(例如:曝光過度的空拍影像)。 - ![image](https://hackmd.io/_uploads/BkG4tD9Ceg.png) (思考): 取Log = $\gamma < 1$ #### Gamma 修正 (Gamma Correction) - 用於校正影像擷取、列印和顯示設備(如 CRT 螢幕)的冪律響應。 ![image](https://hackmd.io/_uploads/SkpxqvcAxe.png) ### 3.2.4 Piecewise-linear Transformation #### Contrast Stretching (對比度拉伸) - (Expanding a narrow range of pixel values to fill the full dynamic range)。 - 轉換由 $(r_1, s_1)$ 和 $(r_2, s_2)$ 兩個點控制。 ![image](https://hackmd.io/_uploads/Hy91JdcRle.png) #### Gray-Level Slicing (灰階切片) - Highlight 特定區間,將其他非目標範圍變暗或保持不變。 ![image](https://hackmd.io/_uploads/r1rnkuqRlg.png) ![image](https://hackmd.io/_uploads/BkQ6ku5Cll.png) #### Bit-Plane Slicing (位元平面切片) - 突顯特定位元對影像的貢獻。 - 一個 8 位元影像可分解為 8 個 1 位元的位元平面。 - 高階位元平面(如 7 和 8)通常包含視覺上最重要的數據。 - (備註): Bit-0幾乎為噪音。 ![image](https://hackmd.io/_uploads/BJtzxd5Cex.png) ## 3.3 Histogram Processing ### 3.3.1 Histogram - **定義:** 一個離散函式 $h(r_k) = n_k$,其中 $r_k$ 是第 k 個灰階, $n_k$ 是影像中具有該灰階的像素數量。 #### 標準化直方圖 (Normalized Histogram) - $p(r_k) = n_k / MN$,其中 M 和 N 是影像的行列維度。 - 即 $r_k$ 出現的機率 ### 3.3.2 Histogram Equalization - **目的:** 調整影像的灰階分佈,使其直方圖盡可能均勻 (uniform)。這是一種 global 處理。 #### 轉換函式 $T(r)$ 需滿足 - (a) $T(r)$ 區間內 **single-valued** 且 **monotonically increasing** (斜率>=0)。 - (b) $T(r)$ 的值域也在 **$[0, L-1]$** 區間內。 #### Continuous Form Derivation (CDF) 1. **(PDF Relationship):** 根據機率論,輸出與輸入 PDF 之間的關係由以下公式給出: - $p_s(s) = p_r(r) \left| \frac{dr}{ds} \right|$。 2. **(The Transformation):** 轉換 $T(r)$ 正是 $r$ 的**累積分布函式 (Cumulative Distribution Function, CDF)**: - $s = T(r) = (L-1) \int_{0}^{r} p_r(w) dw$。 - (這裡 $w$ 是一個虛擬變數 (dummy variable),$(L-1)$ 則是用於將值域從 [0, 1] 縮放回 [0, L-1])。 3. **(Find the Derivative):** 為了使用步驟 1 的公式,我們需要 $\frac{ds}{dr}$。根據微積分基本定理,我們對 $s$ 進行微分: -$\frac{ds}{dr} = \frac{d}{dr} \left[ (L-1) \int_{0}^{r} p_r(w) dw \right]$。 - $\frac{ds}{dr} = (L-1) p_r(r)$。 4. **(Substitution):** 現在,我們將 $\frac{ds}{dr}$ 代入步驟 1 的公式中: - $p_s(s) = p_r(r) \left| \frac{1}{(L-1) p_r(r)} \right|$。 5. **(The Result):** $p_r(r)$ 被消去,我們得到: - $p_s(s) = \frac{1}{L-1}$ (對於 $0 \le s \le L-1$)。 - **結論:** $p_s(s)$ 是一個**均勻機率密度函式 (uniform probability density function)**。這證明了使用 CDF 作為轉換函式,理論上可以產生一個直方圖完全均勻的輸出影像。 #### 連續範例推導 (Continuous Form Derivation - Example) - (此範例展示如何將特定 PDF 轉換為均勻 PDF)。 1. **假設一個 PDF (Assume a PDF):** - 假設影像的灰階強度 $r$ 具有以下 PDF: - $p_r(r) = \begin{cases} \frac{2r}{(L-1)^2} & \text{for } 0 \le r \le L-1 \\ 0 & \text{otherwise} \end{cases}$ 2. **應用 CDF 轉換 (Apply the CDF Transformation):** - $s = T(r) = (L-1) \int_{0}^{r} p_r(w) dw = (L-1) \int_{0}^{r} \frac{2w}{(L-1)^2} dw$ - $s = \frac{2(L-1)}{(L-1)^2} \int_{0}^{r} w dw = \frac{2}{L-1} \left[ \frac{w^2}{2} \right]_{0}^{r}$ - **得到轉換函式 $T(r)$:** $s = \frac{r^2}{L-1}$ 3. **驗證 $p_s(s)$ 是否均勻 (Verify Uniformity):** - 我們需要 $\left| \frac{dr}{ds} \right|$。 - 從 $s = \frac{r^2}{L-1}$ 可得 $\frac{ds}{dr} = \frac{2r}{L-1}$,因此 $\left| \frac{dr}{ds} \right| = \left| \frac{L-1}{2r} \right|$。 - 代入 $p_s(s) = p_r(r) \left| \frac{dr}{ds} \right|$: - $p_s(s) = \left( \frac{2r}{(L-1)^2} \right) \left| \frac{L-1}{2r} \right| = \frac{1}{L-1}$ - **驗證結果:** 輸出的 PDF $p_s(s)$ 確實是一個均勻分佈。 #### 離散形式 (Discrete Form - The Practical Implementation) - 在數位影像中,我們處理的是離散的灰階值,因此 integral 變成 **summation**,PDF 變成標準化直方圖 $p_r(r_k) = \frac{n_k}{MN}$。 - **(Discrete CDF):** 連續轉換 $s = T(r)$ 的離散形式為: - $s_k = T(r_k) = (L-1) \sum_{j=0}^{k} p_r(r_j)$。 - **(Pixel Count Form):** 將 $p_r(r_j) = \frac{n_j}{MN}$ 代入,得到更實用的計算公式: - $s_k = \frac{(L-1)}{MN} \sum_{j=0}^{k} n_j$。 - (其中 $M \times N$ 是總像素數, $n_j$ 是灰階 $j$ 的像素數, $\sum n_j$ 是累積像素數)。 - **(Rounding):** 由於 $s_k$ 的計算結果通常是浮點數,但像素灰階必須是整數,因此需要將 $s_k$ 四捨五入 rounded 到 $[0, L-1]$ 範圍內最接近的整數。 #### 必考範例 (Discrete Example) - 假設一個 3-bit 影像 ($L=8$,灰階範圍 0-7),總像素 $MN = 4096$。 - **1. 原始直方圖 (Original Histogram) & PDF:** | $r_k$ | $p_r(r_k) = n_k / 4096$ | $n_k$ | |:-------:|:-----------------------:|:-----:| | $r_0=0$ | 0.19 | 790 | | $r_1=1$ | 0.25 | 1023 | | $r_2=2$ | 0.21 | 850 | | $r_3=3$ | 0.16 | 656 | | $r_4=4$ | 0.08 | 329 | | $r_5=5$ | 0.06 | 245 | | $r_6=6$ | 0.03 | 122 | | $r_7=7$ | 0.02 | 81 | - **2. 計算 $s_k$ (Discrete CDF):** 我們使用 $s_k = (L-1) \sum_{j=0}^{k} p_r(r_j)$,其中 $L-1 = 7$。 - $s_0 = 7 \times p_r(r_0) = 7 \times 0.19 = 1.33$。 - $s_1 = 7 \times (p_r(r_0) + p_r(r_1)) = 7 \times (0.19 + 0.25) = 7 \times 0.44 = 3.08$。 - $s_2 = 7 \times (0.19 + 0.25 + 0.21) = 7 \times 0.65 = 4.55$ - $s_3 = 7 \times (0.65 + 0.16) = 7 \times 0.81 = 5.67$ - $s_4 = 7 \times (0.81 + 0.08) = 7 \times 0.89 = 6.23$ - $s_5 = 7 \times (0.89 + 0.06) = 7 \times 0.95 = 6.65$ - $s_6 = 7 \times (0.95 + 0.03) = 7 \times 0.98 = 6.86$ - $s_7 = 7 \times (0.98 + 0.02) = 7 \times 1.00 = 7.00$ - **3. 四捨五入 (Rounding) & 映射 (Mapping):** - $s_0 = 1.33 \rightarrow \text{round}(1.33) = \mathbf{1}$。 - $s_1 = 3.08 \rightarrow \text{round}(3.08) = \mathbf{3}$。 - $s_2 = 4.55 \rightarrow \text{round}(4.55) = \mathbf{5}$。 - $s_3 = 5.67 \rightarrow \text{round}(5.67) = \mathbf{6}$。 - $s_4 = 6.23 \rightarrow \text{round}(6.23) = \mathbf{6}$。 - $s_5 = 6.65 \rightarrow \text{round(6.65)} = \mathbf{7}$。 - $s_6 = 6.86 \rightarrow \text{round(6.86)} = \mathbf{7}$。 - $s_7 = 7.00 \rightarrow \text{round(7.00)} = \mathbf{7}$。 - **4. 均化後的直方圖 (Equalized Histogram):** - (原始灰階為 0 的 790 個像素 $\rightarrow$ 映射到新灰階 1) - (原始灰階為 1 的 1023 個像素 $\rightarrow$ 映射到新灰階 3) - (原始灰階為 2 的 850 個像素 $\rightarrow$ 映射到新灰階 5) - (原始灰階為 3 的 656 個像素 $\rightarrow$ 映射到新灰階 6) - (原始灰階為 4 的 329 個像素 $\rightarrow$ 映射到新灰階 6) - (原始灰階為 5, 6, 7 的 (245+122+81) 個像素 $\rightarrow$ 映射到新灰階 7) - **注意:** 由於離散化和四捨五入,均化後的直方圖很少是**完美**均勻的,但它會更廣泛地分佈在整個灰階範圍內。 #### 3.3.3 Histogram Matching (會考!!!) - Histogram Matching 允許我們**指定**一個輸出影像擁有的特定直方圖形狀 **$p_z(z)$**。 - 將輸入影像和目標直方圖**兩者**都轉換為均勻分佈,然後建立它們之間的聯繫。 #### 推導過程 (The Derivation) 1. **Input Transform:** 讓 $r$ 為輸入影像的灰階,其 PDF 為 $p_r(r)$。我們對它進行直方圖均化(CDF 轉換): - $s = T(r) = (L-1) \int_{0}^{r} p_r(w) dw$ - 如我們所知,這個轉換會產生一個均勻分佈的 $s$。 2. **Target Transform:** 讓 $z$ 為目標(輸出)影像的灰階,其**指定**的 PDF 為 $p_z(z)$。我們也對**它**進行 CDF 轉換: - $s = G(z) = (L-1) \int_{0}^{z} p_z(t) dt$ - 這個轉換**同樣**會產生一個(理論上)與 $T(r)$ 相同的均勻分佈 $s$。 3. **Linking the Transforms:** 既然 $T(r)$ 和 $G(z)$ 都等於同一個均勻分佈 $s$,我們可以將它們相等: - $G(z) = T(r)$ 4. **Finding the Mapping:** 找到一個從 $r$ 到 $z$ 的映射。因此,我們對 $z$ 求解: - $z = G^{-1}[T(r)]$ - 這就是最終的**直方圖匹配轉換**。 #### 轉換流程 (Transformation Flow) - `Input Pixel (r)` $\xrightarrow{\text{T(r) (均化)}}$ `Uniform Value (s)` $\xrightarrow{G^{-1}(s) \text{ (反CDF)}}$ `Output Pixel (z)` #### 在實際的數位影像中,我們使用 **summation** 來代替 integral - **步驟 1: 計算輸入影像的 CDF (Compute Input CDF)** - 計算輸入影像的直方圖 $p_r(r_k)$。 - 計算其離散 CDF(即均化轉換 $T(r)$): - $s_k = T(r_k) = (L-1) \sum_{j=0}^{k} p_r(r_j)$ - 將計算出的 $s_k$ 值四捨五入 (round) 到 $[0, L-1]$ 範圍內的整數。 - **步驟 2: 計算目標直方圖的 CDF (Compute Target CDF)** - 獲取**指定**的目標直方圖 $p_z(z_q)$。 - 計算其離散 CDF(即轉換 $G(z)$): - $G(z_q) = (L-1) \sum_{i=0}^{q} p_z(z_i)$ - 將計算出的 $G(z_q)$ 值四捨五入到整數。 - **步驟 3: 建立映射 (Create the Mapping)** - 建立一個從 $s_k$ (步驟 1 的結果) 到 $z_q$ (步驟 2 的輸入) 的映射。 - 遍歷**每一個** $s_k$ 值 (從 $k=0$ 到 $L-1$): - 尋找一個 $z_q$ 值,使得 $G(z_q)$ **最接近** $s_k$。 - **規則:** $z_q = \arg \min_{z_j} \{ |s_k - G(z_j)| \}$ - (如果有多個 $z_q$ 同樣接近,按照慣例選擇最小的 $z_q$ 值)。 - 儲存這個 $s_k \rightarrow z_q$ 的映射。 - **步驟 4: 應用映射 (Apply the Mapping)** - 遍歷原始影像中的每一個像素: 1. 取得該像素的灰階值 $r_k$。 2. 使用步驟 1 的 $T(r_k)$ 找到其均化值 $s_k$。 3. 使用步驟 3 的 $s_k \rightarrow z_q$ 映射表,找到 $s_k$ 對應的 $z_q$。 4. 將該像素的新值設為 $z_q$。 - (註:實際上,步驟 1 和 3 可以合併,直接建立一個 $r_k \rightarrow z_q$ 的映射表)。 #### 此範例承接了直方圖均化(Page 37)的結果 **A. 已知條件 (Given)** - **1. 輸入影像 $r_k$ (來自 Page 37):** - $r_k$ 值: (0, 1, 2, 3, 4, 5, 6, 7) - **$s_k = T(r_k)$ (步驟 1 的結果,已均化並四捨五入):** - **$s_k$ 值: (1, 3, 5, 6, 6, 7, 7, 7)** - **2. 指定直方圖 $p_z(z_q)$ (來自 Fig 3.22b):** | $z_q$ (灰階) | $p_z(z_q)$ (指定機率) | | :---: | :---: | | 0 | 0.00 | | 1 | 0.00 | | 2 | 0.00 | | 3 | 0.15 | | 4 | 0.20 | | 5 | 0.30 | | 6 | 0.20 | | 7 | 0.15 | **B. 計算過程 (Calculation)** - **步驟 2: 計算目標 CDF $G(z_q)$** - 使用公式 $G(z_q) = (L-1) \sum_{i=0}^{q} p_z(z_i)$,其中 $L-1 = 7$。 - $G(z_0) = 7 \times (0.00) = 0.00 \rightarrow \mathbf{0}$ - $G(z_1) = 7 \times (0.00 + 0.00) = 0.00 \rightarrow \mathbf{0}$ - $G(z_2) = 7 \times (0.00 + 0.00 + 0.00) = 0.00 \rightarrow \mathbf{0}$ - $G(z_3) = 7 \times (0.00 + ... + 0.15) = 1.05 \rightarrow \mathbf{1}$ - $G(z_4) = 7 \times (0.15 + 0.20) = 7 \times (0.35) = 2.45 \rightarrow \mathbf{2}$ - $G(z_5) = 7 \times (0.35 + 0.30) = 7 \times (0.65) = 4.55 \rightarrow \mathbf{5}$ - $G(z_6) = 7 \times (0.65 + 0.20) = 7 \times (0.85) = 5.95 \rightarrow \mathbf{6}$ - $G(z_7) = 7 \times (0.85 + 0.15) = 7 \times (1.00) = 7.00 \rightarrow \mathbf{7}$ - **目標 CDF (四捨五入):$G(z_q) = \{0, 0, 0, 1, 2, 5, 6, 7\}$** - **步驟 3: 建立 $s_k \rightarrow z_q$ 映射** - 我們要將 $s_k = \{1, 3, 5, 6, 6, 7, 7, 7\}$ 映射到 $z_q$。 - 我們尋找 $z_q$ 值,使其 $G(z_q)$(步驟 2 的結果)最接近 $s_k$。 | $s_k$ (來自步驟 1) | 最接近的 $G(z_q)$ (來自步驟 2) | 對應的 $z_q$ | | :---: | :---: | :---: | | $s_k=1$ | $G(z_3)=1$ | **$z_q=3$** | | $s_k=3$ | $G(z_4)=2$ (距離 1) (比 $G(z_5)=5$ 距離 2 更近) | **$z_q=4$** | | $s_k=5$ | $G(z_5)=5$ (完美匹配) | **$z_q=5$** | | $s_k=6$ | $G(z_6)=6$ (完美匹配) | **$z_q=6$** | | $s_k=6$ | $G(z_6)=6$ (完美匹配) | **$z_q=6$** | | $s_k=7$ | $G(z_7)=7$ (完美匹配) | **$z_q=7$** | | $s_k=7$ | $G(z_7)=7$ (完美匹配) | **$z_q=7$** | | $s_k=7$ | $G(z_7)=7$ (完美匹配) | **$z_q=7$** | - **步驟 4: 應用 (合併 $r_k \rightarrow z_q$ 映射)** - 將步驟 1 的 $r_k$ 與步驟 3 的 $z_q$ 直接對應起來: | $r_k$ (原始灰階) | $\rightarrow$ $s_k$ (均化) | $\rightarrow$ $z_q$ (最終灰階) | | :---: | :---: | :---: | | 0 | 1 | **3** | | 1 | 3 | **4** | | 2 | 5 | **5** | | 3 | 6 | **6** | | 4 | 6 | **6** | | 5 | 7 | **7** | | 6 | 7 | **7** | | 7 | 7 | **7** | **C. 結果 (Result - Fig 3.22d)** - 原始影像中 $p_r(0)=0.19$ 的像素 $\rightarrow$ 全部變為灰階 3。 - 原始影像中 $p_r(1)=0.25$ 的像素 $\rightarrow$ 全部變為灰階 4。 - 原始影像中 $p_r(2)=0.21$ 的像素 $\rightarrow$ 全部變為灰階 5。 - 原始影像中 $p_r(3)=0.16$ 和 $p_r(4)=0.08$ 的像素 $\rightarrow$ 全部變為灰階 6。 - 原始影像中 $p_r(5)=0.06$, $p_r(6)=0.03$, $p_r(7)=0.02$ 的像素 $\rightarrow$ 全部變為灰階 7。 - **最終的實際直方圖 (Actual Histogram) $p_z(z_k)$:** - $p_z(3) = p_r(0) = 0.19$ - $p_z(4) = p_r(1) = 0.25$ - $p_z(5) = p_r(2) = 0.21$ - $p_z(6) = p_r(3) + p_r(4) = 0.16 + 0.08 = 0.24$ - $p_z(7) = p_r(5) + p_r(6) + p_r(7) = 0.06 + 0.03 + 0.02 = 0.11$ - **結論:** 得到的實際直方圖 (Fig 3.22d) 與我們指定的目標直方圖 (Fig 3.22b) 非常接近,證明了此方法的有效性。 ### 3.3.4 Local Histogram Processing - 像直方圖均化這樣的**全域 (global)** 方法是基於整個影像的直方圖,適用於整體增強。 - 但當影像不同區域的明暗分佈差異很大時(例如,一張同時有陰影和陽光直射的臉),全域處理會顧此失彼,無法增強所有區域的細節。 #### 解決方案 (Solution) - 在影像中的每一個像素上,定義一個鄰域 (neighborhood),例如 3x3 或 5x5 的方框。 - **步驟 (Steps):** 1. 將鄰域的中心移動到某個像素 (x, y)。 2. 計算**該鄰域內**的直方圖。 3. 根據這個**區域直方圖 (local histogram)** 進行均化或匹配,得到一個區域轉換函式。 4. 僅使用此函式來映射鄰域**中心**像素 (x, y) 的新灰階值。 5. 將鄰域中心移動到下一個像素,重複此過程。 ![image](https://hackmd.io/_uploads/S1EjvTa0lx.png) - **結果 (Result):** - 這種方法可以顯著增強局部區域的細節,但計算成本非常高。 ### 3.3.5 Using Histogram Statistics #### Concept - 影像直方圖的統計數據可以描述影像的外觀: - **平均值 (Mean):** $m = \sum_{i=0}^{L-1}r_i p(r_i)$ - (Measures the average gray-level value, corresponding to brightness)。 - **變異數 (Variance):** $\sigma^2 = \mu_2(r) = \sum_{i=0}^{L-1}(r_i - m)^2 p(r_i)$ - (Measures the spread of gray levels, corresponding to contrast)。 #### Local Enhancement - 我們可以在一個鄰域 $S_{xy}$ 內計算 **local mean** $m_{S_{xy}}$ 和 **local variance** $\sigma_{S_{xy}}^2$。 - 透過這些統計數據,我們可以有條件地增強像素。 - **Enhancement Formula** - $g(x,y) = \begin{cases} E \cdot f(x,y) & \text{if } m_{S_{xy}} \le k_0 m_G,\ k_1 \sigma_G \le \sigma_{S_{xy}} \le k_2 \sigma_G \\ f(x,y) & \text{otherwise} \end{cases}$ - **Explanation** - 只有當一個像素 (x,y) 位於一個**暗區** (local mean $m_{S_{xy}}$ 低於 global mean $m_G$) **且** 該區域具有**足夠的細節** (local variance $\sigma_{S_{xy}}$ 在 global variance $\sigma_G$ 的特定範圍內) 時,才將該像素值乘以一個常數 $E$ (例如 $E=4.0$) 來增強它。 - 否則,保持像素不變。 ## 3.4 Fundamentals of Spatial Filtering ### 3.4.1 What is Spatial Filtering? - 空間濾波涉及使用一個小的子影像(稱為**mask**、**kernel**、**filter**、**template**、**window**)。 - 這個遮罩會在影像上逐像素滑動。在影像的每一個點 (x, y) 上,濾波器的響應 (response) 是透過遮罩係數 (coefficients) 與遮罩下方的影像像素值進行運算來計算的。 ### 3.4.2 Linear Spatial Filtering - 線性濾波的響應是透過將濾波器係數 $w(s,t)$ 與其對應的影像像素 $f(x+s, y+t)$ 相乘後求和得到的。 #### 公式 - $g(x,y) = \sum_{s=-a}^{a}\sum_{t=-b}^{b}w(s,t)f(x+s,y+t)$ - 線性空間濾波常被稱為「將遮罩與影像進行摺積 (convolving a mask with an image)」。 - 因此,濾波器遮罩也常被稱為** convolution mask** 或 **convolution kernel**。 #### 向量表示法 - 濾波操作可以簡化為兩個向量的內積 (dot product): $R = w^T z$。 ### 3.4.3 相關 (Correlation) vs. 摺積 (Convolution) #### 相關 (Correlation) - 這就是上一節中**線性濾波**的直接定義。 - **公式:** $w(x,y)\otimes^{\wedge}f(x,y)=\sum_{s=-a}^{a}\sum_{t=-b}^{b}w(s,t)f(x+s,y+t)$ #### 摺積 (Convolution) - **公式:** $w(x,y)*f(x,y)=\sum_{s=-a}^{a}\sum_{t=-b}^{b}w(s,t)f(x-s,y-t)$ - **關鍵差異:** 摺積在運算前,會先將濾波器 $w$ 旋轉 180 度(即 $x$ 軸和 $y$ 軸都翻轉)。 #### 特性 (Properties) - 摺積滿足**交換律 (Commutative)** ($f*g = g*f$) 和**結合律 (Associative)** ($f*(g*h) = (f*g)*h$)。 - 相關則不滿足這些特性。 - 兩者都滿足**分配律 (Distributive)** ($f*(g+h) = f*g + f*h$)。 ![image](https://hackmd.io/_uploads/HJBg1CpAxe.png) ![image](https://hackmd.io/_uploads/BkDLk06Rge.png) ### 3.4.4 Example Kernels #### 平滑核 (Smoothing Kernels) - **方框核 (Box Kernel):** 一個所有係數皆相等的均值濾波器。 - **高斯核 (Gaussian Kernel):** 一個加權均值濾波器,權重由高斯函式決定。 #### 高斯核 (Gaussian Kernel) - **公式:** $w(s,t) = G(s,t) = Ke^{-\frac{s^{2}+t^{2}}{2\sigma^{2}}}$。 - 離散的核心是透過對這個連續函式採樣生成的。 (看不懂下圖 呵) ![image](https://hackmd.io/_uploads/rJFX-AT0eg.png) (Lowpass = Low-frequency pass,模糊非邊界...等高頻細節) ![image](https://hackmd.io/_uploads/Hk9Sb0TRxe.png) ### 3.4.5 Padding #### 當濾波器中心靠近影像邊界時,遮罩的一部分會超出影像範圍。處理方法包括: - **(Zero Padding):** 用 0 填充影像外部。 - **(Mirror Padding):** 以影像邊界為鏡像來填充外部。 - **(Replicate Padding):** 複製影像邊界的像素值來填充外部。 ## 3.5 Smoothing Spatial Filters - 平滑濾波器的輸出(響應)是濾波器遮罩鄰域內像素的平均值。 - 這些濾波器也常被稱為**均值濾波器 (averaging filters)** 或**低通濾波器 (lowpass filters)**。 - **範例 - 3x3 平滑核 (Example 3x3 Smoothing Kernels):** - **(a) 方框核 (Box Kernel) / 非加權均值:** - $R = \frac{1}{9}\sum_{i=1}^{9}z_{i}$ - 遮罩:$\frac{1}{9} \times \begin{bmatrix} 1 & 1 & 1 \\ 1 & 1 & 1 \\ 1 & 1 & 1 \end{bmatrix}$ - **(b) 加權均值核 (Weighted Average Kernel):** - 遮罩:$\frac{1}{16} \times \begin{bmatrix} 1 & 2 & 1 \\ 2 & 4 & 2 \\ 1 & 2 & 1 \end{bmatrix}$ - 每個遮罩前的常數乘數,等於 1 除以其係數的總和,這是計算平均值所必需的。 ### 3.5.1 Weighted Average Filter - 對一個 $M \times N$ 影像使用一個 $m \times n$ (m, n 為奇數) 加權平均濾波器的一般實作公式為: - $g(x,y)=\frac{\sum_{s=-a}^{a}\sum_{t=-b}^{b}w(s,t)f(x+s,y+t)}{\sum_{s=-a}^{a}\sum_{t=-b}^{b}w(s,t)}$ ### 3.5.2 Examples of Linear Smoothing ![image](https://hackmd.io/_uploads/rk7shAaRgg.png) - 展示使用不同大小(如 $m=3, 5, 9, 15, 35$)的方框均值濾波器(Box Kernel)對影像進行平滑處理的結果。 ![image](https://hackmd.io/_uploads/Hktph06All.png) - a. 來自哈伯太空望遠鏡的原始影像。 - b. 使用 $15 \times 15$ 均值遮罩濾波後的影像。 - c. 對 b. 進行閾值處理 (thresholding) 後的結果。 ### 3.5.3 Order-Statistics Filters - 這類濾波器是**非線性 (nonlinear)** 空間濾波器。 - 它們的響應基於濾波器所包圍影像區域中的像素**排序 (ordering / ranking)**。 - 然後用排序結果所決定的值來取代中心像素的值。 #### 中值濾波器 (Median Filter) - 是一種非線性操作 (nonlinear operation)。 - 對於**脈衝雜訊 (impulse noise)**(即椒鹽雜訊)特別有效。 - 濾波器通常**不會**產生新的像素值(輸出值總是來自鄰域中的某個原始值)。 - 其他流行的順序統計濾波器還包括**最大值 (max) 濾波器**和**最小值 (min) 濾波器**。 ![image](https://hackmd.io/_uploads/r1SnAA6Rll.png) - a. 一張被**椒鹽雜訊 (salt-and-pepper noise)** 污染的電路板 X 光影像。 - b. 使用 $3 \times 3$ **均值遮罩**降噪的結果(雜訊被模糊化)。 - c. 使用 $3 \times 3$ **中值濾波器**降噪的結果(雜訊被有效去除)。 ## 3.6 Sharpening Spatial Filters ### 3.6.1 Objective - 銳利化的主要目的是突顯影像中的精細細節。 - 或是增強那些已經變得模糊的細節。 ### 3.6.2 Basis: Derivatives - 影像銳利化是透過**微分**來達成的。 #### 一階導數 (First Derivative) - 在灰階恆定的平坦區域,其值必須為零。 - 在灰階階躍 (step) 或斜坡 (ramp) 的起始點,其值必須不為零。 - 在灰階斜坡上,其值必須不為零。 - 1D 離散定義:$\frac{\partial f}{\partial x} = f(x+1)-f(x)$。 #### 二階導數 (Second Derivative) - 在灰階恆定的平坦區域,其值必須為零。 - 在灰階階躍或斜坡的起始點和結束點,其值必須不為零。 - 在斜率恆定的灰階斜坡上,其值必須為零。 - 1D 離散定義:$\frac{\partial^{2}f}{{\partial x}^{2}} = f(x+1)+f(x-1)-2f(x)$。 #### 關鍵特性#### - 二階導數在突顯精細細節(細線、孤立點)方面比一階導數更有效。 (必考圖!!!) ![image](https://hackmd.io/_uploads/ryHfWyRAxl.png) ### 3.6.3 二階導數:The Laplacian #### 概念 (Concept) - 這是一種基於二階導數的銳利化方法。 - 最簡單的**等向性 (isotropic)**(旋轉不變性)導數運算子是**拉普拉斯 (Laplacian)**。 - 拉普拉斯是一個**線性運算子 (linear operator)**。 #### 2D Definition - $\nabla^{2}f = \frac{\partial^{2}f}{{\partial x}^{2}} + \frac{\partial^{2}f}{\partial y^{2}}$。 #### 離散形式 (Discrete Form):** - x 方向二階導數:$\frac{\partial^{2}f}{\partial^{2}x^{2}} = f(x+1,y)+f(x-1,y)-2f(x,y)$。 - y 方向二階導數:$\frac{d^{2}f}{d^{2}y^{2}}=f(x,y+1)+f(x,y-1)-2f(x,y)$。 - 將兩者相加,得到 2D 拉普拉斯的離散實作: - $\nabla^{2}f = [f(x+1,y)+f(x-1,y)+f(x,y+1)+f(x,y-1)] - 4f(x,y)$。 #### 拉普拉斯遮罩 (Laplacian Masks):** - 用於實作上述 $\nabla^{2}f$ 的濾波器遮罩: - a. $\begin{bmatrix} 0 & 1 & 0 \\ 1 & -4 & 1 \\ 0 & 1 & 0 \end{bmatrix}$ - b. $\begin{bmatrix} 1 & 1 & 1 \\ 1 & -8 & 1 \\ 1 & 1 & 1 \end{bmatrix}$ (包含對角線) - c. $\begin{bmatrix} 0 & -1 & 0 \\ -1 & 4 & -1 \\ 0 & -1 & 0 \end{bmatrix}$ (另一種常見形式) - d. $\begin{bmatrix} -1 & -1 & -1 \\ -1 & 8 & -1 \\ -1 & -1 & -1 \end{bmatrix}$ (另一種常見形式) #### Enhancement Formula - 拉普拉斯銳利化是將拉普拉斯影像(二階導數的結果)加回(或減去)原始影像。 - $g(x,y)=f(x,y)+c[\nabla^{2}f(x,y)]$ - $c$ 的值(+1 或 -1)取決於所使用的遮罩中心是正或負。 - 如果使用遮罩 a. 或 b.(中心為負),$c$ = -1,使公式變為 $f(x,y) - \nabla^{2}f(x,y)$。 - 如果使用遮罩 c. 或 d.(中心為正),$c$ = +1,使公式變為 $f(x,y) + \nabla^{2}f(x,y)$。 ![image](https://hackmd.io/_uploads/HyChzkA0ex.png) ### 3.6.4 Unsharp Masking and High-Boost Filtering (看圖比較懂) #### Concept - 這是出版業行之有年的銳利化技術。 - 過程是從原始影像 $f(x,y)$ 中減去一個**模糊版本** $\overline{f}(x,y)$。 #### Steps 1. **模糊 (Blur):** 模糊原始影像,得到 $\overline{f}(x,y)$。 2. **計算遮罩 (Mask):** 從原始影像中減去模糊影像,得到「非銳利遮罩」 $g_{mask}(x,y)$。 - $g_{mask}(x,y)=f(x,y)-\overline{f}(x,y)$。 3. **加回 (Add):** 將遮罩(或其加權版本)加回原始影像。 - $g(x,y)=f(x,y)+k \cdot g_{mask}(x,y)$。 ![image](https://hackmd.io/_uploads/H1foQ1R0gg.png) #### Types - 當 $k=1$ 時,此過程稱為**非銳利遮罩 (Unsharp Masking)**。 - 當 $k>1$ 時(例如 $k=4.5$),此過程稱為**高提升濾波 (High-Boost Filtering)**。 ![image](https://hackmd.io/_uploads/S18h7100gl.png) ### 3.6.5 一階導數:The Gradient #### Concept - 梯度 (Gradient) 是一個指向灰階變化率最快方向的 2D 向量。 - $\nabla f=grad(f)=[\begin{matrix}g_{x}\\ g_{y}\end{matrix}]=[\begin{matrix}\frac{\partial f}{\partial x}\\ \frac{\partial f}{\partial y}\end{matrix}]$。 #### Magnitude - 梯度的**大小 (magnitude)**(即向量的長度) $M(x,y)$ 表示了在 (x, y) 點的灰階變化強度,常用於邊緣偵測。 - $M(x,y)=mag(\nabla f)=\sqrt{{g_{x}}^{2}+{g_{y}}^{2}}$。 - 為了簡化計算,常用絕對值總和來近似: - $M(x,y)\approx|g_{x}|+|g_{y}|$。 #### Operators - **Roberts 交叉梯度 (Roberts Operators):** - $g_{x}=(z_{9}-z_{5})$ - $g_{y}=(z_{8}-z_{6})$ - 對應的遮罩為 $\begin{bmatrix} -1 & 0 \\ 0 & 1 \end{bmatrix}$ 和 $\begin{bmatrix} 0 & -1 \\ 1 & 0 \end{bmatrix}$。 - **Sobel 運算子 (Sobel Operators):** - 這是一種常用的 3x3 運算子。 - $g_{x}=(z_{7}+2z_{8}+z_{9})-(z_{1}+2z_{2}+z_{3})$。 - $g_{y}=(z_{3}+2z_{6}+z_{9})-(z_{1}+2z_{4}+z_{7})$。 - 對應的遮罩為: - $g_x$: $\begin{bmatrix} -1 & -2 & -1 \\ 0 & 0 & 0 \\ 1 & 2 & 1 \end{bmatrix}$ - $g_y$: $\begin{bmatrix} -1 & 0 & 1 \\ -2 & 0 & 2 \\ -1 & 0 & 1 \end{bmatrix}$ - Sobel 運算子在計算導數的同時也進行了平滑(例如 $z_8$ 的權重為 2),因此對雜訊的抗干擾能力較強。 ![image](https://hackmd.io/_uploads/BkxeBJRCee.png) - Sobel 梯度影像,它成功地突顯了鏡片的邊緣和缺陷。 ## 3.7 Frequency Domain Filter ### 3.7.1 Filters - **低通濾波器 (Lowpass filter):** - **(Passband):** 允許低頻通過。 - **(Stopband):** 阻擋高頻。 - **(Effect):** 平滑、模糊。 - **高通濾波器 (Highpass filter):** - **(Stopband):** 阻擋低頻。 - **(Passband):** 允許高頻通過。 - **(Effect):** 銳利化、邊緣偵測。 - **帶斥濾波器 (Bandreject filter):** - **(Stopband):** 阻擋特定範圍的中頻。 - **(Passband):** 允許低頻和高頻通過。 - **帶通濾波器 (Bandpass filter):** - **(Passband):** 僅允許特定範圍的中頻通過。 - **(Stopband):** 阻擋低頻和高頻。 ![image](https://hackmd.io/_uploads/S1RZ81A0gl.png) ### 3.7.2 Spatial Domain (計算) - 在空間域(即影像本身),這些濾波器核 (kernel) 之間有著密切的數學關係。 - 我們可以使用**單位脈衝 (unit impulse)** $\delta(x,y)$(一個中心為1、其餘為0的影像,代表原始影像)和**低通核 (lowpass kernel)** $lp(x,y)$(例如均值核或高斯核)來建構其他濾波器。 - **高通核 (Highpass Kernel):** - $hp(x,y) = \delta(x,y) - lp(x,y)$ - **(說明):** 這正是**非銳利遮罩 (Unsharp Masking)** 的核心概念。 - `高通影像 (細節)` = `原始影像` - `低通影像 (模糊版本)`。 - **帶斥核 (Bandreject Kernel):** - $br(x,y) = lp_1(x,y) + hp_2(x,y)$ - (一個帶斥濾波器可以由一個低通核和一個高通核組合而成)。 - **帶通核 (Bandpass Kernel):** - $bp(x,y) = \delta(x,y) - br(x,y)$ - (一個帶通濾波器可以從原始影像中減去一個帶斥濾波器的結果得到)。 - 通常不會只使用單一種增強技術。為了達到最佳的影像效果,往往會將本章中提到的多種方法結合起來使用。 ![image](https://hackmd.io/_uploads/HJj1_k00lx.png) # Chapter 4 *** 挺抽象,可以YT找影片看 *** ## 4.1 Background ### 4.1.1 傅立葉級數 (Fourier Series) - 任何週期性函數 (Periodic functions) 都可以表示為不同頻率的正弦 (sines) 和/或餘弦 (cosines) 的總和。 ### 4.1.2 傅立葉轉換 (Fourier Transform): - 非週期性函數 (Non-periodic functions) 可以表示為正弦和/或餘弦乘以一個權重函數的積分。 - 任何以此方式表示的函數都可以通過反向過程 (inverse process) 重建回來。 ![image](https://hackmd.io/_uploads/BJJ6stlk-l.png) ## 4.2 Preliminary Concepts ### 4.2.1 Complex Numbers #### 定義: $C = R + jI$ - $R$ 是實部 (Real part),$I$ 是虛部 (Imaginary part)。 - $j = \sqrt{-1}$ - **共軛複數 (Conjugate):** $C^* = R - jI$ #### 極座標表示 (Polar Coordinates) - $C = |C|(cos~\theta + j~sin~\theta)$ - **尤拉公式 (Euler's formula):** $e^{j\theta} = cos~\theta + j~sin~\theta$ - 因此,極座標可寫為: $C = |C|e^{j\theta}$ #### 幅度 (Magnitude) - $|C| = \sqrt{R^2 + I^2}$ #### 相位角 (Angle) - $\theta = arctan(I/R)$ ### 4.2.2 傅立葉級數 (Fourier Series) #### 函數 $f(t)$ - $f(t) = \sum_{n=-\infty}^{\infty} c_n e^{j\frac{2\pi n}{T}t}$ #### 係數 $c_n$ - $c_n = \frac{1}{T}\int_{-T/2}^{T/2} f(t) e^{-j\frac{2\pi n}{T}t} dt$ ### 4.2.3 脈衝及其篩選特性 (Impulses and Their Sifting Property) #### 連續脈衝 (Continuous Impulse) / 狄拉克δ函數 (Dirac Delta) - $\delta(t) = \begin{cases} \infty & \text{if } t=0 \\ 0 & \text{if } t \ne 0 \end{cases}$ - 且滿足 $\int_{-\infty}^{\infty} \delta(t) dt = 1$ - **篩選特性 (Sifting Property):** - $\int_{-\infty}^{\infty} f(t)\delta(t) dt = f(0)$ - $\int_{-\infty}^{\infty} f(t)\delta(t-t_0) dt = f(t_0)$ - #### 離散脈衝 (Discrete Impulse) - $\delta(x) = \begin{cases} 1 & \text{if } x=0 \\ 0 & \text{if } x \ne 0 \end{cases}$ - $\sum_{x=-\infty}^{\infty} \delta(x) = 1$ - **篩選特性 (Sifting Property):** - $\sum_{x=-\infty}^{\infty} f(x)\delta(x) = f(0)$ - $\sum_{x=-\infty}^{\infty} f(x)\delta(x-x_0) = f(x_0)$ ![image](https://hackmd.io/_uploads/B11eCKeybx.png) #### 脈衝串 (Impulse Train) - 是一系列相隔 $\Delta T$ 的週期性脈衝的總和。 - $S_{\Delta T}(t) = \sum_{n=-\infty}^{\infty} \delta(t-n\Delta T)$ ![image](https://hackmd.io/_uploads/HJbJCFlkWx.png) ### 4.2.4 單一連續變數的傅立葉轉換 (Fourier Transform of One Continuous Variable) #### 傅立葉轉換 (Forward Transform) - $F(\mu) = \int_{-\infty}^{\infty} f(t) e^{-j2\pi\mu t} dt$ #### 反傅立葉轉換 (Inverse Transform) - $f(t) = \int_{-\infty}^{\infty} F(\mu) e^{j2\pi\mu t} d\mu$ - 轉換後的變數 $\mu$ 是頻率,因此 $F(\mu)$ 被稱為在**頻率域 (frequency domain)**。 #### 範例 (方波轉換) - 一個寬度為 $W$、高度為 $A$ 的方波函數。 - 其傅立葉轉換為一個 **sinc 函數**: $F(\mu) = AW \frac{sin(\pi\mu W)}{(\pi\mu W)}$ - $sinc(m) = \frac{sin(\pi m)}{(\pi m)}$ ![image](https://hackmd.io/_uploads/Sk5kk9g1bx.png) #### 傅立葉頻譜 (Fourier Spectrum) - $|F(\mu)| = AW |\frac{sin(\pi\mu W)}{(\pi\mu W)}|$ ![image](https://hackmd.io/_uploads/HkBdy9gkZe.png) #### 脈衝的轉換 - $\mathfrak{S}\{\delta(t)\} = 1$ (原點的脈衝在頻域中是一個常數) - $\mathfrak{S}\{\delta(t-t_0)\} = e^{-j2\pi\mu t_0}$ #### 脈衝串的轉換 - 脈衝串的傅立葉轉換**也是一個脈衝串**。 - $\mathfrak{S}\{s_{\Delta T}(t)\} = S(\mu) = \frac{1}{\Delta T}\sum_{n=-\infty}^{\infty} \delta(\mu-\frac{n}{\Delta T})$ ### 4.2.5 摺積 (Convolution) #### 定義 - $f(t) * h(t) = \int_{-\infty}^{\infty} f(\tau)h(t-\tau) d\tau$ #### 摺積定理 (Convolution Theorem) - 空間域的摺積 (spatial domain convolution) 等同於頻率域的乘積 (frequency domain multiplication)。 - $f(t) * h(t) \Leftrightarrow H(\mu)F(\mu)$ #### 定理的另一半 - 空間域的乘積等同於頻率域的摺積。 - $f(t)h(t) \Leftrightarrow H(\mu) * F(\mu)$ ## 4.3 取樣與取樣函數的傅立葉轉換 (Sampling and the Fourier Transform of Sampled Functions) ### 4.3.1 取樣 (Sampling) #### 取樣(採樣)過程 - 模型化為一個連續函數 $f(t)$ 與一個脈衝串 $S_{\Delta T}(t)$ 相乘。 #### 取樣後的函數 - - $\tilde{f}(t) = f(t)S_{\Delta T}(t) = \sum_{n=-\infty}^{\infty} f(t)\delta(t-n\Delta T)$ #### 每個取樣點 $f_k$ 的值 - 透過積分和脈衝的篩選特性得到的: - $f_k = \int_{-\infty}^{\infty} f(t)\delta(t-k\Delta T)dt = f(k\Delta T)$ ![image](https://hackmd.io/_uploads/BytXPqxyZe.png) ### 4.3.2 取樣函數的傅立葉轉換 (The Fourier Transform of Sampled Functions) - 取樣後函數 $\tilde{f}(t)$ 的傅立葉轉換為 $\tilde{F}(\mu)$。 #### 摺積定理 (空間域的乘積等於頻率域的摺積) - $\tilde{F}(\mu) = \mathfrak{S}\{f(t)s_{\Delta T}(t)\} = F(\mu) * S(\mu)$ - 其中 $F(\mu)$ 是 $f(t)$ 的轉換,$S(\mu)$ 是脈衝串的轉換。 - $\tilde{F}(\mu) = \frac{1}{\Delta T}\sum_{n=-\infty}^{\infty} F(\mu-\frac{n}{\Delta T})$ - 這個結果表明,一個取樣函數的傅立葉轉換,會是原始函數 $F(\mu)$ 的**無限、週期性重複**,重複的週期為 $1/\Delta T$。 ### 4.3.3 取樣定理 (The Sampling Theorem) #### 頻寬限制函數 (Band-limited function) - 如果一個函數 $f(t)$ 的傅立葉轉換 $F(\mu)$ 在一個有限的頻率範圍 $[-\mu_{max}, \mu_{max}]$ 之外為零,則稱其為頻寬限制函數。 #### 恢復 (Reconstruction) - 為了能從取樣後的數據 $\tilde{F}(\mu)$ 完美地恢復 $F(\mu)$,週期性的 $F(\mu)$ 副本之間必須沒有重疊。 #### 取樣定理 (Nyquist-Shannon) - 恢復的條件是取樣率 $(1/\Delta T)$ 必須**大於**原始訊號最高頻率的兩倍。 - $\frac{1}{\Delta T} > 2\mu_{max}$ #### 過取樣 (over-sampling)** - **臨界取樣 (critically-sampled):** $1/\Delta T = 2\mu_{max}$ - **取樣不足 (under-sampling):** $1/\Delta T < 2\mu_{max}$ ![image](https://hackmd.io/_uploads/B1x6d5gJWe.png) ### 4.3.4 混疊 (Aliasing) - 當發生**取樣不足**時 ($1/\Delta T < 2\mu_{max}$),在頻率域中,$F(\mu)$ 的週期性副本會發生重疊 (如 Fig. 4.9c 所示)。 - 這種重疊會導致高頻訊號被誤認為是低頻訊號,從而**汙染**了原始訊號的頻譜。 - 這種現象稱為**頻率混疊 (frequency aliasing)** 或簡稱**混疊 (aliasing)**。 - 由於混疊,我們無法再使用一個理想的低通濾波器來完美地恢復 $F(\mu)$。 - **重點:** 由於沒有一個**有限持續時間**的函數可以是**頻寬完全受限**的,因此在實際應用中,混疊總是存在的。 ![image](https://hackmd.io/_uploads/S1WMt9xy-x.png) ### 4.3.5 從取樣數據重建函數 (Function Reconstruction) #### 理想低通濾波器 (ideal lowpass filter) - $H(\mu)$ 從 $\tilde{F}(\mu)$ 中提取 $F(\mu)$: - $F(\mu) = H(\mu)\tilde{F}(\mu)$ - 在空間域中,這等同於 $f(t) = h(t) * \tilde{f}(t)$。 - 完整的重建公式(惠特克-香農插值公式)顯示 $f(t)$ 可以由其取樣值 $f(n\Delta T)$ 和 sinc 函數的無窮級數來表示。 ## 4.4 單一變數的離散傅立葉轉換 (The Discrete Fourier Transform (DFT) of One Variable) ### 4.4.1 從連續轉換中獲取 DFT - 連續傅立葉轉換 $\tilde{F}(\mu)$ 是週期的,週期為 $1/\Delta T$。 - 我們在 $\tilde{F}(\mu)$ 的一個週期內(例如 0 到 $1/\Delta T$)取 $M$ 個等距樣本。 - 取樣的頻率點為:$\mu = \frac{m}{M\Delta T}$,其中 $m = 0, 1, 2, ..., M-1$。 - 將此 $\mu$ 代入 $\tilde{F}(\mu)$ 的積分式,可導出: #### 離散傅立葉轉換 (DFT) - $F(u) = \sum_{x=0}^{M-1} f(x) e^{-j2\pi ux/M}$ - 其中 $u = 0, 1, 2, ..., M-1$。 #### 反離散傅立葉轉換 (IDFT) - $f(x) = \frac{1}{M} \sum_{u=0}^{M-1} F(u) e^{j2\pi ux/M}$ - 其中 $x = 0, 1, 2, ..., M-1$。 #### 週期性 (Periodicity) - DFT $F(u)$ 和 IDFT $f(x)$ 都是以 $M$ 為週期的無限週期函數。 - $F(u) = F(u + kM)$ - $f(x) = f(x + kM)$ #### 循環摺積 (Circular Convolution) - 由於 DFT 的週期性,離散域中的摺積是**循環**的。 - $f(x) * h(x) = \sum_{m=0}^{M-1} f(m)h(x-m)$ ### 4.4.2 取樣間隔和頻率間隔的關係 - 假設 $f(x)$ 是由 $M$ 個樣本組成,樣本間隔為 $\Delta T$。 #### 空間域總持續時間 (Duration) - $T = M\Delta T$ #### 頻率域間隔 (Frequency Spacing) - $\Delta u = \frac{1}{M\Delta T} = \frac{1}{T}$ #### 頻率域總範圍 (Frequency Range) - $\Omega = M\Delta u = \frac{1}{\Delta T}$ #### 關係 - 頻率間隔 $\Delta u$ (頻率解析度) 與總持續時間 $T$ 成反比。 - 總頻率範圍 $\Omega$ 與取樣間隔 $\Delta T$ (或取樣率) 成反比。 # Chapter 5 ## 壹、影像復原基礎與降級模型 ### 1. 復原定義與目標 * **影像復原 (Restoration)**:嘗試利用對降級現象的**先驗知識 (a priori knowledge)** 來重建或恢復已被降級的影像。 * **技術方向**:朝向對降級進行建模並應用逆向過程以恢復原始影像。 ### 2. 降級模型 * **空間域降級模型**: $$\mathbf{g(x, y) = h(x, y) \star f(x, y) + \eta(x, y)} \quad \text{(Eq. 5.1-1)}$$ * $f(x, y)$:輸入影像。 * $g(x, y)$:降級影像。 * $\eta(x, y)$:加性噪聲項 (additive noise term)。 * $h(x, y)$:降級函數的空間表示。 * $\star$:空間摺積 (spatial convolution)。 * **頻域降級模型**: $$\mathbf{G(u, v) = H(u, v) F(u, v) + N(u, v)} \quad \text{(Eq. 5.1-2)}$$ ### 3. 系統特性與降級函數 * **線性運算子 (Linear Operator)**:必須同時具備**可加性** (additivity, $H[f_1+f_2] = H[f_1] + H[f_2]$, Eq. 5.5-3) 和**齊次性** (homogeneity, $H[a f_1] = a H[f_1]$, Eq. 5.5-4)。 * **位置/空間不變性 (Position/Space Invariant)**:圖像中任意點的響應只取決於輸入在該點的值,與其位置無關 (Eq. 5.5-5)。 * **脈衝響應/PSF**:如果 $H$ 是線性且位置不變的,則 $h(x, y)$ 稱為脈衝響應 (impulse response),在光學中常稱為**點擴散函數 (PSF)**。 * **降級函數 $H$ 的估計方法**:(1) 觀察 (2) 實驗 (3) 數學建模。 * **盲解摺積 (Blind Deconvolution)**:使用**估計的**降級函數進行影像復原。 * **均勻線性運動模糊**:如果假設恆定運動 $x_0(t)=at/T, y_0(t)=0$,其降級函數 $H(u, v)$ 包含 $\sin(\pi u a / T) / (\pi u a / T)$ 項。 ## 貳、主要噪聲模型與統計特性 噪聲來源於影像採集或傳輸過程。 | 噪聲類型 | PDF 描述 | 平均值 $(\bar{z})$ 與方差 $(\sigma^2)$ | | :--- | :--- | :--- | | **高斯 (Gaussian/Normal)** | 鐘形曲線 (Eq. 5.2-1)。 | 由 $\bar{z}$ 和 $\sigma$ 定義。 | | **瑞利 (Rayleigh)** | PDF (Eq. 5.2-2)。 | $\bar{z} = a + \sqrt{b\pi/4}$ (Eq. 5.2-3), $\sigma^2 = b(4-\pi)/4$ (Eq. 5.2-4)。 | | **愛爾朗/伽馬 (Erlang/Gamma)** | PDF (Eq. 5.2-4)。 | $\bar{z} = b/a$ (Eq. 5.2-6), $\sigma^2 = b/a^2$ (Eq. 5.2-7)。 | | **指數 (Exponential)** | PDF (Eq. 5.2-8)。 | $\bar{z} = 1/a$ (Eq. 5.2-9), $\sigma^2 = 1/a^2$ (Eq. 5.2-10)。 | | **均勻 (Uniform)** | PDF 在 $[a, b]$ 之間恆定 (Eq. 5.2-11)。 | $\bar{z} = (a+b)/2$ (Eq. 5.2-12), $\sigma^2 = (b-a)^2/12$ (Eq. 5.2-13)。 | | **衝激 (Impulse/椒鹽噪聲)** | 在 $z=a$ 或 $z=b$ 處有離散機率 $P(z)$ (Eq. 5.2-14)。 | ## 參、僅噪聲存在的空間域濾波 當只有噪聲時 $g(x, y) = f(x, y) + \eta(x, y)$ (Eq. 5.3-1)。 ### 1. 均值濾波器 * **算術均值**:鄰域像素的平均值 (Eq. 5.3-3)。 * **幾何均值**:模糊程度比算術均值小。 * **諧波均值**:適用於**椒鹽噪聲 (Salt noise)**,但對胡椒噪聲 (Pepper noise) 失敗。 * **逆諧波均值 (Contraharmonic Mean)** (階數 $Q$) (Eq. 5.3-6): * $\mathbf{Q>0}$:消除**胡椒噪聲**。 * $\mathbf{Q<0}$:消除**椒鹽噪聲**。 * 使用前必須知道噪聲是暗 (胡椒) 還是亮 (椒鹽)。 ### 2. 順序統計濾波器 基於對鄰域像素的**排序 (ranking)**。 * **中值濾波器 (Median Filter)**:**非線性濾波**,對**衝激噪聲** (椒鹽) 非常有效 (Eq. 5.3-7)。重複應用會使影像模糊。 * **最大值濾波器 (Max Filter)**:減少**胡椒噪聲**。 * **最小值濾波器 (Min Filter)**:減少**椒鹽噪聲**。 * **中點濾波器 (Midpoint Filter)**:最適用於**隨機分佈噪聲**,如高斯或均勻噪聲 (Eq. 5.3-10)。 * **Alpha-trimmed Mean Filter**:刪除 $d/2$ 個最低和最高值後,對剩餘 $mn-d$ 像素求平均 (Eq. 5.3-11)。 ### 3. 自適應濾波器 * **自適應局部降噪濾波器** (Eq. 5.3-12): * $\hat{f} \approx g - \frac{\sigma_\eta^2}{\sigma_L^2} [g - m_L]$。 * 若噪聲方差 $\mathbf{\sigma_\eta^2 = 0}$,返回 $g(x, y)$。 * 若 $\mathbf{\sigma_L^2 = \sigma_\eta^2}$ (局部方差等於噪聲方差),返回算術均值。 * **自適應中值濾波器**:旨在**移除衝激噪聲**、提供平滑、**減少失真**並**保持銳利度和細節**。它會判斷中心點是否為衝激。 ## 肆、已知降級函數下的頻域復原 ### 1. 逆濾波 (Inverse Filtering) * **公式**:$\hat{F}(u, v) = G(u, v) / H(u, v)$。 * **主要缺陷**:未明確處理噪聲 $N(u, v)$。當 $H(u, v)$ 接近零時,噪聲比 $N(u, v)/H(u, v)$ 會**支配**估計結果,導致不穩定。 * **解決方法**:將濾波限制在接近原點(低頻)的範圍,對結果進行低通濾波。 ### 2. 維納濾波 (Wiener Filtering / 最小均方誤差, MSE) * **目標**:最小化 $\mathbf{f}$ 與 $\mathbf{\hat{f}}$ 之間的均方誤差 (MSE) (Eq. 5.8-4)。 * **實務近似公式** (假設噪聲和原始影像功率譜之比為常數 $K$): $$\mathbf{\hat{F}(u, v) = \left[ \frac{1}{H(u, v)} \frac{|H(u, v)|^2}{|H(u, v)|^2 + K} \right] G(u, v)} \quad \text{(Eq. 5.8-6)}$$ * $K$ 是一個必須**互動調整**才能獲得最佳值的指定常數。 ### 3. 約束最小平方法濾波 (Constrained Least Squares Filtering) * **先決條件**:只需知道噪聲的**均值**和**方差**。 * **原理**:最小化平滑度量 (如使用拉普拉斯函數 $P(u, v)$ 的傅立葉轉換) 並受到噪聲歐幾里德向量範數約束 (Eq. 5.9-3)。 * **頻域解**:涉及參數 $\gamma$,該參數必須調整以滿足約束條件。 ### 4. 幾何均值濾波 (Geometric Mean Filter) * 是維納濾波器的一種推廣。 * $\alpha=1$:**逆濾波器**。 * $\alpha=0, \beta=1$:**維納濾波器**。 * $\alpha=1/2, \beta=1$:**頻譜均衡濾波器 (Spectrum equalization filter)**。 ## 伍、投影影像重建 (Computed Tomography, CT) ### 1. 投影與 Radon 轉換 * **CT 掃描儀世代**:從 G1 發展至 G7 (G5: EBCT 消除機械運動;G6: 螺旋 CT;G7: 多層 CT)。 * **射線和 (Raysum)**:$f(x, y)$ 沿直線 $x \cos \theta + y \sin \theta = \rho$ 的積分 (Eq. 5.11-3)。 * **Radon 轉換**:用 $g(\rho, \theta)$ 表示所有 $\rho$ 和 $\theta$ 值下的射線和。 ### 2. 傅立葉切片定理 (Fourier-Slice Theorem) * **定理**:投影 $g(\rho, \theta)$ 相對於 $\rho$ 的 **1-D 傅立葉轉換** $G(\omega, \theta)$,等於影像 $f(x, y)$ 的 **2-D 傅立葉轉換** $F(u, v)$ 在一條穿過原點、角度為 $\theta$ 的切片上的值。 $$\mathbf{G(\omega, \theta) = F(\omega \cos \theta, \omega \sin \theta)} \quad \text{(Eq. 5.11-11)}$$ ### 3. 濾波反投影 (Filtered Backprojections) * **重建核心**:2-D 逆傅立葉轉換推導中包含一個 $|\omega|$ 項。 * **Ramp 濾波器**:$|\omega|$ 是一個 ramp 濾波器。由於其幅度延伸至 $+\infty$,**不可積分**,因此逆傅立葉轉換未定義。 * **實際處理**:需對 ramp 濾波器進行**窗口化 (windowing)** 處理,使其在定義的頻率區間外變為零 (例如使用 Hamming 或 Hann 窗口)。 * **最終重建**:重建的圖像 $f(x, y)$ 是所有投影角度的反投影結果的總和 (Eq. 5.11-18)。 * **扇形射束 (Fan-Beam Geometry)**:扇形射束投影 $p(\alpha, \beta)$ 可以轉換為對應的平行射束投影 $g(\rho, \theta)$ 進行處理 (Eq. 5.11-31)。 ```

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Google Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully