# <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:
- 
- 
### <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
- 
### <span class="purple">**Implementation**</span>
- Pixel stack: Treat the 8 neighboring pixels as a “byte”
- Look-Up-Table (LUT)
- 
- 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.
- 
### <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>
- 
- 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>
- 
<span class="pink">**Bridge**</span>
- 
<span class="pink">**8-neighbor dilate**</span>
- 
<span class="pink">**Isolated pixel removal**</span>
- 
<span class="pink">**Spur removal**</span>
- 
<span class="pink">**Interior pixel removal**</span>
- 
<span class="pink">**H-break/Eight-neighbor erode**</span>
- 
- 
### <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
- 
- **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).
- 
- **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.
- 
<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
- 
<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
- 
<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
- 
### <span class="purple">**Generalized dilation and erosion**</span>

**Reflection of a binary image**

**Translation of a binary image**

**Dilation**
- 
- H = structuring element
- Can be implemented in several ways
- Minkowski addition definition
- 
- 
**Erosion**
- 
- Can be implemented in several ways
- Dual relationship of Minkowski addition
- 
- Sternberg definition
- Serra definition
## <span class="blue">**Applications**</span>
### <span class="purple">**Boundary Extraction**</span>
- Extract the boundary (or out line) of an object
- 
- 
- 
### <span class="purple">**Hole Filling**</span>
- Given a pixel inside a boundary, hole filling attempts to fill that boundary with object pixels
- 
- 
- 
### <span class="purple">**Connected Component Labeling**</span>
- Scan an image and groups its pixels into components based on pixel connectivity
- 
- 
### <span class="purple">**Open operator**</span>
- With a compact structuring element
- Smoothes contours of objects
- Eliminates small objects
- Breaks narrow strokes
- 
- 
### <span class="purple">**Close operator**</span>
- With a compact structuring element
- Smoothes contours of objects
- Eliminates small holes
- Fuses short gaps between objects
- 
- 
- 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>