CSS Prepare

optional · CSS study notes

Morphological Operators

CSS Optional — Paper-II, Section-B · P-II.V.IX · Syllabus: Morphological operators — erosion, dilation, opening, closing, skeletonization, thinning. CSS 2018 set a zero-padding dilation/erosion numerical on a small binary image — grid numericals are the main scoring route here.

1. What Is Mathematical Morphology?

  • A set of shape-based operations built on set theory. Objects in a binary image are sets of foreground (1) pixels.
  • Used to extract and clean shape — boundaries, skeletons, holes, connected parts — rather than to change brightness.
  • Works on binary images first; extends to grayscale (max/min operations).

Structuring element (SE): a small binary shape with a marked origin (usually its centre) that probes the image.

SE shapeExampleEffect
Square 3 × 3all onesActs equally in all 8 directions
Cross (plus)0 1 0 / 1 1 1 / 0 1 04-connected growth/shrink only
Line[1 1 1] or its transposeActs only horizontally or vertically
Diskroughly circularRounded, direction-independent results

2. The Two Basic Operations

PointErosion A ⊖ BDilation A ⊕ B
RuleOutput = 1 only if the SE fits entirely inside the object ("all")Output = 1 if the SE hits the object anywhere ("any")
Set definition{z : B shifted to z is a subset of A}{z : reflected B shifted to z overlaps A}
Effect on objectsShrinks / thins; removes small objects and thin protrusionsGrows / thickens; fills small holes and gaps, joins close parts
Grayscale equivalentLocal minimumLocal maximum
AnalogySandpaper wearing edges awayPaint brush thickening strokes

Duality: (A ⊖ B)ᶜ = Aᶜ ⊕ B̂ — eroding the object = dilating the background. Erosion and dilation are not inverses: dilating after eroding does not bring back what was lost (that is exactly why opening is useful).

3. Opening and Closing

PointOpening A ∘ BClosing A • B
Formula(A ⊖ B) ⊕ B — erode, then dilate(A ⊕ B) ⊖ B — dilate, then erode
RemovesSmall bright specks, thin protrusions, narrow bridges (isthmuses)Small dark holes, narrow gaps and breaks
ContourSmooths from the insideSmooths from the outside
Relation to AOpening ⊆ A (anti-extensive)A ⊆ Closing (extensive)
Geometric pictureRoll the SE inside the object; keep everywhere it can reachRoll the SE outside; anything it can't enter gets filled

Both are idempotent: applying opening (or closing) twice with the same SE gives the same result as once. Open-close in sequence is a classic binary noise filter (remove specks, then fill holes).

4. Hit-or-Miss Transform

Finds places where a specific shape pattern occurs: A ⊛ B = (A ⊖ B1) ∩ (Aᶜ ⊖ B2) — B1 must fit the object and B2 must fit the background. Basis of thinning, corner detection and template spotting in binary images.

5. Morphological Algorithms

AlgorithmFormula / ideaUse
Boundary extractionβ(A) = A − (A ⊖ B)Object outlines
Hole fillingStart inside the hole: Xk = (Xk−1 ⊕ B) ∩ Aᶜ, repeat until no change; answer = Xk ∪ AFilling cells, coins, characters
Connected componentsXk = (Xk−1 ⊕ B) ∩ A from a seedCounting / labelling objects
Convex hullIterated hit-or-miss until stableShape description
ThinningA ⊗ B = A − (A ⊛ B), with a sequence of rotated SEs, repeated until stableReduce strokes to 1-pixel width while keeping connectivity (fingerprints, handwriting, OCR)
ThickeningA ∪ (A ⊛ B) — dual of thinningGrowing objects without merging them
SkeletonizationS(A) = union over k of [(A ⊖ kB) − (A ⊖ kB) ∘ B] (Lantuéjoul)Medial axis — the "stick figure" of a shape; A can be rebuilt from its skeleton
PruningRemove short spurs left by thinning / skeletonsClean skeletons

Thinning vs Skeletonization (common short question):

PointThinningSkeletonization
Goal1-pixel-wide version, topology (connectivity) preservedMedial axis — centres of maximal inscribed disks
ReconstructionOriginal cannot be rebuiltOriginal can be rebuilt from skeleton + distances
ArtefactsFew spursCan be broken / spurious branches; sensitive to noise
Typical useCharacter and fingerprint recognitionShape analysis, path planning

6. Grayscale Morphology

  • Dilation = local maximum → brightens, expands bright regions.
  • Erosion = local minimum → darkens, shrinks bright regions.
  • Morphological gradient = dilation − erosion → highlights edges.
  • Top-hat = f − opening(f) → extracts small bright objects on an uneven background (corrects shading).
  • Bottom-hat (black-hat) = closing(f) − f → extracts small dark objects.
  • Granulometry: open with growing SEs to measure the size distribution of particles.

7. Applications

Cleaning binary masks after thresholding · counting rice grains / blood cells · fingerprint ridge thinning (NADRA-style biometrics) · OCR character cleanup and thinning · PCB inspection for broken tracks · road and river extraction from satellite images · separating touching objects (with watershed).


📐 Worked Numericals

Convention: 3 × 3 square SE of ones, origin at centre, zero padding at borders (as in CSS 2018).

N1 — Opening removes a noise speck. Image A (5 rows × 7 cols):

0 0 0 0 0 0 0
0 1 1 1 0 0 0
0 1 1 1 0 0 0
0 1 1 1 0 1 0
0 0 0 0 0 0 0
  1. Erosion: a pixel stays 1 only if its whole 3 × 3 window is 1. Only the block centre (row 2, col 2) qualifies; the isolated speck at (3, 5) fails.
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 1 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
  1. Dilation of that result: every pixel within the 3 × 3 window of (2, 2) becomes 1 → the 3 × 3 block is rebuilt.
0 0 0 0 0 0 0
0 1 1 1 0 0 0
0 1 1 1 0 0 0
0 1 1 1 0 0 0
0 0 0 0 0 0 0

Result: the object survives unchanged, the speck is gone → opening removes bright noise smaller than the SE.

N2 — Closing fills a hole. Image A (5 × 5), a ring with a hole at the centre:

0 0 0 0 0
0 1 1 1 0
0 1 0 1 0
0 1 1 1 0
0 0 0 0 0
  1. Dilation: every pixel whose 3 × 3 window touches a 1 becomes 1 → the whole 5 × 5 grid becomes 1 (even corners touch (1,1), (1,3), etc.).
  2. Erosion with zero padding: border pixels see padded zeros → 0; the nine interior pixels have all-ones windows → 1.
0 0 0 0 0
0 1 1 1 0
0 1 1 1 0
0 1 1 1 0
0 0 0 0 0

Result: same outline, hole filled → closing fills dark gaps smaller than the SE.

N3 — Boundary extraction. A = solid 3 × 3 block (9 pixels). A ⊖ B = the centre pixel only. β(A) = A − centre = 8-pixel ring.

N4 — Effect of SE shape. Dilate a single 1-pixel with a 3 × 3 square → 9 pixels; with a cross → 5 pixels; with [1 1 1] → 3 pixels in a row.

N5 — Grayscale (1-D). f = [10, 50, 20, 30, 80], flat SE of length 3, interior pixels only (positions 1–3):

  • Dilation (max): [50, 50, 80]
  • Erosion (min): [10, 20, 20]
  • Morphological gradient = dilation − erosion: [40, 30, 60] → the largest value marks the sharpest change (the jump to 80).

N6 — CSS 2018 style (horizontal / vertical line SEs and 3 × 3 dilation) is fully solved on DIP — Solved Numericals & Formula Sheet. Method to remember: a horizontal SE [1 1 1] only spreads left-right; a vertical SE only up-down.


🎯 CSS Exam Angle

  • Grid numericals: copy the grid, state the SE and its origin, state the padding rule, then show the intermediate result for opening/closing. Write one line of interpretation at the end ("speck removed", "hole filled").
  • Theory: erosion vs dilation table → opening vs closing table → one algorithm (boundary extraction formula) → applications. Draw a small blob before/after each operation.
  • "Explain thinning and skeletonization": definitions + the comparison table + fingerprint / OCR example.

📌 Past CSS Paper Frequency

YearQuestion
2018Apply dilation and erosion (with zero padding) using given structuring elements on a small binary image
SyllabusErosion, dilation, opening, closing, skeletonization and thinning named explicitly

⚠️ MCQ Traps

  • Opening = erode → dilate; Closing = dilate → erode. "Open the door, Enter" is one memory hook (E first).
  • Opening removes bright specks; closing fills dark holes.
  • Erosion and dilation are duals, not inverses.
  • Opening and closing are idempotent.
  • Boundary = A − (A ⊖ B), not (A ⊕ B) − A (that gives the outer boundary).
  • Grayscale dilation = max, erosion = min.
  • Thinning preserves connectivity; a skeleton allows reconstruction.

Self-Check

  1. Which operation would remove thin wires connecting two blobs? Which would join two nearly touching blobs?
  2. Why does opening not change an object that is larger than the SE?
  3. Write the formulas for top-hat and morphological gradient and say what each highlights.

✅ Self-Check (with Answers)

Q1. Define erosion and dilation in set notation.

Ans: Erosion A ⊖ B = {z | (B)z ⊆ A} — positions where the SE fits entirely inside A. Dilation A ⊕ B = {z | (B̂)z ∩ A ≠ ∅} — positions where the reflected SE overlaps A at all.

Q2. You need to remove small white specks from a binary image without shrinking the main objects. Which operation?

Ans: Opening with an SE larger than the specks: erosion removes the specks (and slightly shrinks objects), then dilation restores the objects to their original size.

Q3. You need to fill small black holes inside objects. Which operation?

Ans: Closing: dilation fills the holes (and slightly enlarges objects), then erosion restores the object size.

Q4. Erode the 1-D row [0 1 1 1 0 1 0] with SE [1 1 1] (zero padding).

Ans: A pixel stays 1 only if it and both neighbours are 1 → only the middle of the 3-run survives: [0 0 1 0 0 0 0]. The isolated 1 disappears.

Q5. Dilate the same row with [1 1 1].

Ans: Any 1 in the 3-window → 1: [1 1 1 1 1 1 1] — the gap at position 4 is bridged.

Q6. Write the boundary-extraction formula and explain it.

Ans: β(A) = A − (A ⊖ B). Erosion strips a one-pixel layer from the object; subtracting it from the original leaves exactly that outer layer — the boundary.

Q7. Give two differences between thinning and skeletonization.

Ans: Thinning (iterative hit-or-miss) preserves connectivity but cannot reconstruct the object; the skeleton (medial axis, via erosions minus openings) allows exact reconstruction but may be disconnected and is sensitive to boundary noise.

📜 Past-Paper Questions & MCQs (CSS 2016–2026)

2016 · Paper-II · Q7(c) · 8 marks

Q. Explain the boundary extraction algorithm used for basic morphology.

Ans.

  • Idea: the boundary of a set A is the set of pixels that belong to A but are removed when A is eroded.
  • Formula: β(A) = A − (A ⊖ B), where B is a small structuring element (for example 3 × 3 of ones) and ⊖ is erosion.
  • Steps:
    • Step 1: Choose a structuring element B.
    • Step 2: Erode A by B: A ⊖ B shrinks the object by one pixel at the border.
    • Step 3: Subtract the eroded image from A, so only the boundary layer remains.
  • Result: a 1-pixel-thick boundary (8-connected for a 3 × 3 B). A larger B gives a thicker boundary.

2018 · Paper-II · Q6(C) · 6 marks

Q. For the 7 × 8 binary image X (ones in row 1 at columns 1, 2, 5, 6; row 2 at columns 1–6; row 5 at columns 2–5; zero padding at the borders), show the result of (i) dilation with [1 1 1] (ii) erosion with [1 1 1]ᵀ (iii) dilation with a 3 × 3 element of ones.

Ans.

  • (i) Dilation with [1 1 1] spreads each 1 one pixel to the left and right.
row0: 0 0 0 0 0 0 0 0
row1: 1 1 1 1 1 1 1 1
row2: 1 1 1 1 1 1 1 1
row3: 0 0 0 0 0 0 0 0
row4: 0 0 0 0 0 0 0 0
row5: 0 1 1 1 1 1 1 0
row6: 0 0 0 0 0 0 0 0
  • (ii) Erosion with [1 1 1]ᵀ (vertical, 3 × 1) keeps a pixel only if it and the pixels above and below it are all 1. No column has three consecutive ones (the longest vertical runs are 2), so the result is all zeros.
  • (iii) Dilation with 3 × 3 ones spreads each 1 by one pixel in every direction.
row0: 1 1 1 1 1 1 1 1
row1: 1 1 1 1 1 1 1 1
row2: 1 1 1 1 1 1 1 1
row3: 1 1 1 1 1 1 1 1
row4: 0 1 1 1 1 1 1 0
row5: 0 1 1 1 1 1 1 0
row6: 0 1 1 1 1 1 1 0

2021 · Paper-II · Q7(a) · 8 marks

Q. Write code or pseudocode to locate all of the large boxes in a noisy image (white boxes with small speckles on a black background) using morphological image processing.

Ans.

1. Read the image; convert to grey scale; threshold to a binary image B (boxes = 1)
2. Remove small white specks:   B1 = open(B, SE_big)        // SE larger than the noise, smaller than a box (e.g. 5x5)
3. Fill small black holes inside boxes:   B2 = close(B1, SE_small)    // e.g. 3x3
4. Label the connected components of B2      // 8-connectivity
5. For each component:
       area = number of pixels
       if area > MIN_BOX_AREA:  record its bounding box
6. Draw or return the bounding boxes (the large boxes)
  • Why it works: opening (erosion then dilation) deletes objects smaller than the structuring element (noise) but restores the size of larger boxes. Closing fills small gaps inside them.

2022 · Paper-II · Q6(a) · 8 marks

Q. Find the reflection B̂ of each of the given structuring elements (the dot is the origin).

Ans. (The figure cannot be read reliably from the scan, so the rule is given.)

  • Reflection is B̂ = {w | w = −b, for b in B}. Every element at coordinates (x, y) relative to the origin moves to (−x, −y), which is the same as rotating the structuring element by 180° about its origin.
  • How to draw it: for each shaded cell, find its offset from the dot, negate both coordinates, and shade that new cell. A cell at the right of the origin moves to the left, one above moves below, and so on. The origin stays in place. A symmetric element is its own reflection.

2024 · Paper-II · Q7(b) · 7 marks

Q. Provide detailed explanations and applications of morphological operators such as erosion, dilation, opening, closing, skeletonisation and thinning.

Ans.

  • Erosion (A ⊖ B): shrinks objects. A pixel stays only if the structuring element fits inside the object. Removes small bright noise and separates touching objects.
  • Dilation (A ⊕ B): grows objects. Fills small holes and joins broken parts (such as broken characters).
  • Opening (A ∘ B = (A ⊖ B) ⊕ B): smooths contours, removes thin protrusions and small objects.
  • Closing (A • B = (A ⊕ B) ⊖ B): fills narrow gaps and small holes and joins nearby objects.
  • Skeletonisation: reduces an object to a thin line (the medial axis) that keeps its shape and connectivity. It is used for character recognition, fingerprint analysis and shape description.
  • Thinning: repeatedly removes boundary pixels until the object is one pixel wide, without breaking it. It is used for document and fingerprint processing.
  • Applications: document cleaning, medical image preprocessing, boundary extraction, object counting, and inspection in industry.

2026 · Paper-II · Q7 · 20 marks

Q. Morphological operations are powerful tools to manipulate shape-based features. Discuss the mathematical foundation of dilation, erosion, opening and closing, and evaluate their use in tasks such as document analysis or medical image preprocessing.

Ans.

  • Foundation: images are sets of pixel coordinates (binary: the foreground set A). A structuring element B is a small probe shape. Operations use set theory (union, intersection, translation, reflection). (Bz is B translated by z, and B̂ is B reflected about its origin.)
  • Dilation: A ⊕ B = { z | (B̂)z ∩ A ≠ ∅ }. The set of positions where the reflected B touches A. It enlarges A and fills gaps.
  • Erosion: A ⊖ B = { z | (B)z ⊆ A }. The set of positions where B fits entirely inside A. It shrinks A and removes small details.
  • Duality: (A ⊖ B)ᶜ = Aᶜ ⊕ B̂. Erosion of the object is dilation of the background.
  • Opening: A ∘ B = (A ⊖ B) ⊕ B. It is the union of all translates of B that fit inside A. It removes small objects and thin protrusions, smooths contours, and is idempotent (repeating it changes nothing).
  • Closing: A • B = (A ⊕ B) ⊖ B. It fills small holes, narrow gaps and bays.
  • Properties: dilation and erosion are increasing (monotonic), and opening is anti-extensive (A ∘ B ⊆ A) while closing is extensive (A ⊆ A • B).
  • Document analysis:
    • opening removes speckle noise, and closing joins broken strokes in characters
    • dilation with a horizontal element joins letters into words and text lines (layout analysis)
    • thinning and skeletons help character recognition, and boundary extraction finds outlines
  • Medical image preprocessing:
    • opening removes small bright artefacts and noise, and closing fills holes in segmented organs or vessels
    • erosion separates touching cells for counting, and top-hat transforms correct uneven illumination
    • skeletons analyse vessels and bones
  • Limitations: the result depends on the choice of B (shape and size). Aggressive operations can erase real fine structures (small lesions, thin strokes), and grey-scale and noisy data need careful design. A balanced choice of B and a combination of operations (open then close) works best.

MCQs

2026 P-II · Q6. In morphological image operations, the key property that distinguishes opening from closing is that opening tends to:

  • (A) Fill narrow gaps
  • (B) Remove thin protrusions ✅
  • (C) Produce higher contrast
  • (D) None of these
  • Answer: (B).
  • Why: opening is erosion followed by dilation, which deletes thin protrusions and small bright objects but restores the size of larger shapes. Filling narrow gaps is what closing does.