Install any skill in seconds. Free to start, no credit card required.
Get Started Free →Opening vs Closing decision guide, structuring element selection, Top-Hat/Bottom-Hat contrast filters, and grayscale morphology
| Test case | Without → With | Effect | Δ tokens | Δ turns |
|---|---|---|---|---|
| case-04 | ✗→✓ | ▲ Improved | -12% | 0% |
| case-02 | ✗→✓ | ▲ Improved | -13% | 0% |
| case-03 | ✗→✓ | ▲ Improved | 9% | 0% |
| case-01 | ✓→✓ | = Same ✓ | -8% | 0% |
| case-05 | ✓→✓ | = Same ✓ | 21% | 0% |
What do you need to clean up?
├── Small white noise specks on black background (external noise)
│ └── ✅ Opening (Erosion → Dilation)
│
├── Small black holes inside white objects (internal gaps)
│ └── ✅ Closing (Dilation → Erosion)
│
├── Objects touching each other (need separation)
│ └── ✅ Erosion (shrinks objects apart)
│
└── Broken lines or gaps in object boundaries
└── ✅ Dilation (expands and connects)| Operation | Formula | Removes | Keeps | Memory Aid | |-----------|---------|---------|-------|------------| | Opening | Erode → Dilate | External noise | Object integrity | "Opens" gaps between noise and object | | Closing | Dilate → Erode | Internal holes | Object shape | "Closes" holes inside object |
> Golden rule: Opening cleans the outside, Closing fills the inside.
Real-world example — Fingerprint processing:
| Shape | OpenCV Constant | Best For | |-------|----------------|----------| | Rectangle/Square | cv2.MORPH_RECT | Angular objects, text characters | | Ellipse/Disk | cv2.MORPH_ELLIPSE | Round objects, cells, coins | | Cross (+) | cv2.MORPH_CROSS | Thin lines, intersections |
Size selection rule: The structuring element must be:
pythonkernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5, 5))
| Filter | Formula | Reveals | Use Case | |--------|---------|---------|----------| | Top-Hat | Original - Opening | Bright details on dark background | Bright spots, text on dark surface | | Bottom-Hat | Closing - Original | Dark details on bright background | Dark spots, stains on light surface |
pythontophat = cv2.morphologyEx(gray, cv2.MORPH_TOPHAT, kernel) blackhat = cv2.morphologyEx(gray, cv2.MORPH_BLACKHAT, kernel)
Swapping the order of erosion and dilation produces opposite results:
In binary images, erosion/dilation work with set theory. In grayscale:
| Operation | Grayscale Behavior | Visual Effect | |-----------|-------------------|---------------| | Dilation | Maximum filter (picks brightest neighbor) | Image brightens, bright regions expand | | Erosion | Minimum filter (picks darkest neighbor) | Image darkens, dark regions expand |
python# Single pass eroded = cv2.erode(binary, kernel, iterations=1) # Multiple passes = stronger effect (equivalent to larger kernel) eroded = cv2.erode(binary, kernel, iterations=3)
python# Dilation - Erosion = object boundary gradient = cv2.morphologyEx(binary, cv2.MORPH_GRADIENT, kernel)
pythonkernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5)) eroded = cv2.erode(binary, kernel, iterations=1) dilated = cv2.dilate(binary, kernel, iterations=1) opened = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel) closed = cv2.morphologyEx(binary, cv2.MORPH_CLOSE, kernel)
pythongradient = cv2.morphologyEx(img, cv2.MORPH_GRADIENT, kernel) # Boundary tophat = cv2.morphologyEx(img, cv2.MORPH_TOPHAT, kernel) # Bright details blackhat = cv2.morphologyEx(img, cv2.MORPH_BLACKHAT, kernel) # Dark details
python# Typical binary cleanup pipeline kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3)) cleaned = cv2.morphologyEx(binary, cv2.MORPH_OPEN, kernel) # Remove noise cleaned = cv2.morphologyEx(cleaned, cv2.MORPH_CLOSE, kernel) # Fill holes
Other measured skills in the registry, with their headline benchmark lift.