---
# System prepended metadata

title: 'Lec 4 Morphological Processing '
tags: [dip]

---

# <span class="orange">Morphological Processing</span>


##  <span class="blue">**Binary image connectivity**</span>


###  <span class="purple">**Pixel bond**</span>

  - Specify the connectivity of a pixel with its neighbors
- Four-connected neighbor: bond = 2 (上下左右)
-  Eight-connected neighbor: bond = 1 (上下左右＋右上右下左上左下)

Examples:
- ![截圖 2024-04-09 下午6.52.27](https://hackmd.io/_uploads/rkwgA5fg0.png)
 - ![截圖 2024-04-09 下午6.53.02](https://hackmd.io/_uploads/Hy5GA5Mg0.png =350x)

###  <span class="purple">**Minimally connected**</span>
- Elimination of any black (object) pixel (except boundary pixels) results in **disconnection** of the remaining black (object) pixels


##  <span class="blue">**Binary hit or miss transformations**</span>

1. Select a nxn hit pattern (odd-sized mask)
2. Compare with a nxn image window
    - Match (hit) -> change the central pixel value 
    - Otherwise (miss) -> do nothing

- ![截圖 2024-04-09 下午6.58.16](https://hackmd.io/_uploads/SyN8JiMeA.png =350x)

###  <span class="purple">**Implementation**</span>
- Pixel stack: Treat the 8 neighboring pixels as a “byte”
- Look-Up-Table (LUT) 
- ![截圖 2024-04-09 下午7.28.12](https://hackmd.io/_uploads/Hyu8LsfeC.png =250x)
    - A lookup table (LUT) method is a technique used to efficiently perform the hit-or-miss operation by utilizing predefined patterns to match against the input image.
- ![截圖 2024-04-09 下午7.28.49](https://hackmd.io/_uploads/HkT_IoGg0.png =250x)


###  <span class="purple">**Simple morphological processing based on binary hit or miss rules**</span>

#### Additive operators (0->1) 
- Interior fill
-  Diagonal fill
- Bridge
- 8-neighbordilate

#### Subtractive operators (1->0) 
- Isolated pixel removal
- Spur removal
- Interior pixel removal
- H-break/Eight-neighbor erode

<span class="pink">**Interior fill**</span>
- ![截圖 2024-04-09 下午7.54.58](https://hackmd.io/_uploads/BJyinizgR.png)
- kernel: 十字
- perform dilation with the 十字 kernel

```  python=
def dilation(image, kernel):
    result = np.zeros_like(image)
    height, width = image.shape
    kh, kw = kernel.shape
    kh_half, kw_half = kh // 2, kw // 2
    for i in range(kh_half, height - kh_half):
        for j in range(kw_half, width - kw_half):
            result[i, j] = np.max(image[i - kh_half:i + kh_half + 1, j - kw_half:j + kw_half + 1] * kernel)
    return result
``` 

<span class="pink">**Diagonal fill**</span>
- ![截圖 2024-04-09 下午7.55.24](https://hackmd.io/_uploads/SyK23ifxA.png)

<span class="pink">**Bridge**</span>
- ![截圖 2024-04-09 下午7.55.48](https://hackmd.io/_uploads/H1-CnsMlC.png)


<span class="pink">**8-neighbor dilate**</span>
- ![截圖 2024-04-09 下午7.56.06](https://hackmd.io/_uploads/BJXkpjGlA.png)

<span class="pink">**Isolated pixel removal**</span>
- ![截圖 2024-04-09 下午7.56.24](https://hackmd.io/_uploads/B1Hg6oGe0.png)

<span class="pink">**Spur removal**</span>
- ![截圖 2024-04-09 下午7.56.40](https://hackmd.io/_uploads/SySZpjGeA.png)

<span class="pink">**Interior pixel removal**</span>
- ![截圖 2024-04-09 下午7.56.55](https://hackmd.io/_uploads/HkQzasflC.png)

<span class="pink">**H-break/Eight-neighbor erode**</span>
- ![截圖 2024-04-09 下午7.57.08](https://hackmd.io/_uploads/B1fmasMlA.png =450x)
- ![截圖 2024-04-09 下午7.57.40](https://hackmd.io/_uploads/HJZHTsfxR.png)


###  <span class="purple">**Advanced morphological processing**</span>
Subtractive operator doesn’t prevent total erasure and ensure connectivity


<span class="pink">**Shrinking/Thinning/Skeletonizing**</span>

 - Conditional erosion
  - ![截圖 2024-04-09 下午8.11.27](https://hackmd.io/_uploads/rJhOg3zxC.png)
- **Stage 1: input pixel X 與  pattern table 比對, M=1 if Hit**
- The conditional array, often denoted as M(j,k), is generated based on certain criteria. Each pixel in the image is examined, and if it meets certain conditions, it is marked as a candidate for erasure (M(j,k)=1).
- Conversely, if a pixel does not meet the criteria, it is marked as not needing further operation (M(j,k)=0).
    - ![截圖 2024-04-09 下午8.13.16](https://hackmd.io/_uploads/Hk_yb3fgA.png =480x)
- **Stage 2:** 
- In step 2 of the thinning algorithm, when there is a "hit" between the center pixel and the pattern in the conditional array, it means that the conditions for erasure are not met. In other words, the **center pixel satisfies the criteria for preserving the shape** of the object in the binary image.
- when there is a "hit," indicating that the current configuration of pixels preserves the desired shape or connectivity, there is no need to take any action (i.e., no need to erase the center pixel). The algorithm moves on to the next pixel without modifying the image.
    - ![截圖 2024-04-09 下午8.13.34](https://hackmd.io/_uploads/Sk5lW3MxR.png =450x)


<span class="pink">**Shrinking**</span>
Erase black pixels such that an object without holes erodes to a single pixel at its center of mass, an object with holes erodes to a connected ring lying midway between each hole and its nearest outer boundary
- ![截圖 2024-04-10 下午4.42.58](https://hackmd.io/_uploads/HkRfWCXlA.png =400x)

<span class="pink">**Thinning**</span>
Erase black pixels such that an object without holes erodes to a minimally connected stroke located equidistant from its **nearest outer boundaries**, and an object with holes erodes to a minimally connected ring midway between each hole and its nearest outer boundary
- ![截圖 2024-04-10 下午4.43.27](https://hackmd.io/_uploads/HysEZRXxR.png =400x)

<span class="pink">**Skeletonizing**</span>
The medial axis skeleton consists of the set of points that are equally distant from **two closest points** of an object boundary

- ![截圖 2024-04-10 下午4.44.07](https://hackmd.io/_uploads/H1mDZRXxC.png =500x)



###  <span class="purple">**Generalized dilation and erosion**</span>

![截圖 2024-04-10 下午4.49.45](https://hackmd.io/_uploads/SJU3MR7l0.png =200x)

**Reflection of a binary image**
![截圖 2024-04-10 下午4.50.09](https://hackmd.io/_uploads/HJMCGCQx0.png =200x)

**Translation of a binary image**
![截圖 2024-04-10 下午4.50.43](https://hackmd.io/_uploads/B1ge7CQgA.png =200x)

**Dilation**
- ![截圖 2024-04-10 下午4.51.24](https://hackmd.io/_uploads/rJdzXCXxC.png =200x)
- H = structuring element
- Can be implemented in several ways
    - Minkowski addition definition
    - ![截圖 2024-04-10 下午4.56.49](https://hackmd.io/_uploads/HkTI40Qx0.png =230x)
    - ![截圖 2024-04-10 下午4.57.05](https://hackmd.io/_uploads/SJJdNRXeC.png =400x)



**Erosion**
- ![截圖 2024-04-10 下午4.51.42](https://hackmd.io/_uploads/rk9QX07e0.png =200x)
- Can be implemented in several ways
- Dual relationship of Minkowski addition
- ![截圖 2024-04-10 下午5.16.47](https://hackmd.io/_uploads/BkTWYRXeC.png =230x)
    - Sternberg definition
    - Serra definition


##  <span class="blue">**Applications**</span>

###  <span class="purple">**Boundary Extraction**</span>
- Extract the boundary (or out line) of an object
- ![截圖 2024-04-10 下午5.29.45](https://hackmd.io/_uploads/SyDz3AmxR.png =400x)
- ![截圖 2024-04-10 下午5.30.07](https://hackmd.io/_uploads/H1jmn0Xg0.png =400x)
- ![截圖 2024-04-10 下午5.30.28](https://hackmd.io/_uploads/Sy-Hn0Xl0.png =300x)

###  <span class="purple">**Hole Filling**</span>
- Given a pixel inside a boundary, hole filling attempts to fill that boundary with object pixels
- ![截圖 2024-04-10 下午5.31.10](https://hackmd.io/_uploads/r1swhAQxA.png =500x)
- ![截圖 2024-04-10 下午5.31.39](https://hackmd.io/_uploads/HkKFh07g0.png =500x)
- ![截圖 2024-04-10 下午5.32.09](https://hackmd.io/_uploads/BkIo30QlR.png =400x)


###  <span class="purple">**Connected Component Labeling**</span>
- Scan an image and groups its pixels into components based on pixel connectivity
- ![截圖 2024-04-10 下午5.33.28](https://hackmd.io/_uploads/HJPlpRQeC.png =420x)
- ![截圖 2024-04-11 上午2.43.06](https://hackmd.io/_uploads/S1U6TLExC.png =300x)



###  <span class="purple">**Open operator**</span>
- With a compact structuring element 
    -  Smoothes contours of objects
    - Eliminates small objects
    - Breaks narrow strokes
- ![截圖 2024-04-11 下午4.34.01](https://hackmd.io/_uploads/H18YlQHlA.png =400x)
- ![截圖 2024-04-11 下午4.39.30](https://hackmd.io/_uploads/SJbCbmrxR.png =500x)


###  <span class="purple">**Close operator**</span>
- With a compact structuring element 
    -  Smoothes contours of objects
    - Eliminates small holes
    - Fuses short gaps between objects
- ![截圖 2024-04-11 下午4.40.14](https://hackmd.io/_uploads/H1nxMQBeC.png =400x)
- ![截圖 2024-04-11 下午4.41.03](https://hackmd.io/_uploads/SJoQf7BgA.png =500x)
- Q: repeated openings/closings?


<style>
.blue {
  color:#79CCEE;
}
.orange {
  color:#F68F02;
}
.pink {
  color:#FE9FA1;

}
.yellow {
  color:#FDB515;
}

.purple {
    color:#BAA2ED;
}
.block {
  margin-left : 1em; 
}

</style>