CSS Prepare

optional · CSS study notes

P-II.V. Digital Image Processing

CSS Computer Science (Optional) — Paper-II, Section-B, Topic V. Index for Digital Image Processing — each subtopic has its own page, in syllabus order. Every page follows the same layout: notes → worked numericals → CSS exam angle → past-paper frequency → MCQ traps.

📚 Topic Pages (in order)

🧮 Revision

📖 Book Chapter 11 → Page Map

Book headingWhere it's covered
What is Digital ImagingRelation to Related Fields (§1, §5 fundamental steps) + Image Sensing, Acquisition, and Representation
Relation between Image Processing, Computer Graphics, Computer Vision & GraphicsRelation to Related Fields (Graphics, Vision, AI)
Image Sampling & QuantizationImage Sampling and Quantization
Image TransformationsImage Transformations
Image Histogram (2017)Image Histogram and Enhancement (§1, §4)
Optical Character RecognitionOptical Character Recognition (OCR) — new page
Image EnhancementImage Histogram and Enhancement (§2–§8)
Gray Level TransformationImage Histogram and Enhancement (§3 — negative, log, gamma, stretching, slicing)
Color Codes ConversionGrayscale, Color Images, and Color Models (RGB ↔ CMY/CMYK/HSI, grayscale conversion)
Image RestorationImage Restoration and Noise Models
MorphologyMorphological Operators
Line DetectionPoint, Line, Edge and Boundary Detection (§3)
Boundary DetectionPoint, Line, Edge and Boundary Detection (§5)
Image CompressionImage Compression

📌 Past-Paper Snapshot (reviewed papers)

  • 2017: image histogram + equalization + applications; geometric transformations; storage of a 256 × 200 RGB image
  • 2018: sampling vs quantization; histogram equalization numerical; mean vs median; Sobel numerical; dilation/erosion with zero padding
  • 2019: RGB vs HSI
  • 2020: histogram equalization (process and uses); types of colour models, hardware-oriented models in detail; translation and scaling + bits to store a 256 × 256 image with 32 gray levels (k = 5 → 327,680 bits = 40 KB)
  • 2021: need for multiple colour models; CMY vs CMYK

✅ Self-Test Questions (with Answers)

Q1. Distinguish Image Processing, Computer Graphics, and Computer Vision by their input→output direction.

Ans: The three fields are separated by what goes in and what comes out:

FieldInput → OutputTypical task
Digital Image ProcessingImage → ImageEnhance, restore, compress a photograph
Computer GraphicsDescription/model → ImageRender a 3-D scene from geometry
Computer VisionImage → Description/decisionRecognize a face, read a number plate

Graphics and Vision are therefore inverse of each other, with Image Processing sitting between them as the pre-processing stage. Artificial Intelligence is the umbrella that supplies the learning and inference machinery Vision uses; the boundary is blurred (e.g. a CNN that both denoises and classifies), which is why modern texts call the whole area a continuum rather than three sealed disciplines.

Q2. What is the difference between sampling and quantization in digitizing an image?

Ans: Digitization has two independent axes:

  • Sampling discretizes the spatial coordinates (x, y) — how many pixels the image has. It fixes spatial resolution (M × N). Too few samples produce checkerboard/pixelation and aliasing (Moiré patterns); the Nyquist criterion requires a sampling rate of at least twice the highest spatial frequency.
  • Quantization discretizes the amplitude (intensity) of each sample — how many gray levels are allowed, L = 2^k for k bits. It fixes intensity/gray-level resolution. Too few levels produce false contouring (visible bands in smooth gradients).

Storage follows directly: bits = M × N × k. For example a 256 × 256 image with 32 gray levels (k = 5) needs 256 × 256 × 5 = 327,680 bits = 40 KB — the exact CSS-2020 numerical.

Q3. Perform histogram equalization on a small 3-bit histogram and explain why the result isn't flat.

Ans: Take a 64-pixel, 3-bit image (L = 8) with counts r₀…r₇ = 8, 10, 10, 2, 12, 16, 4, 2.

rnp = n/64CDFs = round(7 × CDF)
080.1250.1251
1100.1560.2812
2100.1560.4373
320.0310.4693
4120.1880.6565
5160.2500.9066
640.0630.9697
720.0311.0007

New histogram: level 1 → 8, level 2 → 10, level 3 → 12 (2 and 3 merged), level 5 → 12, level 6 → 16, level 7 → 6 (6 and 7 merged); levels 0 and 4 become empty.

Why it is not flat: the transformation s = T(r) = (L−1)·CDF(r) is exactly flattening only for a continuous intensity variable. In a digital image the intensities are discrete and the mapping must be rounded, so (i) several input levels collapse onto one output level and (ii) some output levels receive no pixels. Equalization can only spread and stretch the histogram — it cannot split the pixels of a single input level. The result is a more uniform, wider-spread histogram with improved global contrast, not a mathematically flat one. (Side effects: it can over-amplify noise in near-uniform regions, which is why CLAHE / adaptive HE is preferred in medical imaging.)

Q4. Compare RGB, CMYK, and HSI color models and their typical use cases.

Ans:

FeatureRGBCMY / CMYKHSI (HSV/HSL)
NatureAdditive — light added to blackSubtractive — ink subtracts from whitePerceptual — how humans describe colour
PrimariesRed, Green, BlueCyan, Magenta, Yellow (+ blacK)Hue, Saturation, Intensity
All primaries at maxWhiteBlack (muddy brown in practice)n/a
OrientationHardware — monitors, cameras, scannersHardware — printers, pressesHuman/algorithm oriented
Typical useDisplay, capture, storage (JPEG/PNG)Printing, publishingImage analysis, segmentation by colour, enhancement
  • Conversion: C = 1 − R, M = 1 − G, Y = 1 − B; K = min(C, M, Y) is then factored out — the K plate exists because mixing three inks gives a poor, expensive, slow-drying black, and black text is the most printed element (CSS 2021).
  • Why several models are needed: no single model serves capture, display, print and analysis at once. RGB and CMY are device-dependent and couple colour with brightness, so changing brightness in RGB means editing all three channels. HSI decouples intensity (I) from chromaticity (H, S), so grayscale techniques — histogram equalization, edge detection, smoothing — can be applied to I alone without shifting the colours. That decoupling is the standard exam justification for HSI.
  • Grayscale conversion uses luminance weights: Y = 0.299R + 0.587G + 0.114B (green dominates because the eye is most sensitive to it).

Q5. Differentiate enhancement and restoration; write the degradation model.

Ans:

  • Enhancement is subjective and heuristic — it manipulates an image so it looks better or is more suitable for a specific task, with no model of how it was spoiled. Success is judged by the human eye. Examples: histogram equalization, contrast stretching, sharpening, gamma correction.
  • Restoration is objective and model-based — it uses a priori knowledge of the degradation (blur kernel, noise statistics) to recover an estimate that is closest to the original by a mathematical criterion such as minimum mean-square error. Examples: inverse filtering, Wiener filtering, constrained least squares.

In one line: enhancement makes an image look good; restoration tries to make it correct.

Degradation model (spatial domain):

g(x, y) = h(x, y) * f(x, y) + η(x, y)

and in the frequency domain:

G(u, v) = H(u, v) · F(u, v) + N(u, v)

where f = original image, h = degradation (point-spread) function, * = convolution, η = additive noise, g = observed image. Restoration applies a restoration filter to g to produce an estimate f̂. Direct inverse filtering, F̂ = G/H, fails wherever H(u, v) is small because the noise term N/H explodes — hence the Wiener filter, which balances the inverse against the signal-to-noise ratio.

Q6. Explain erosion, dilation, opening, and closing, and how opening/closing differ in operation order.

Ans: All four use a structuring element (SE) B moved over a binary image A.

  • Erosion (A ⊖ B): keeps a pixel only if the SE fits entirely inside the object. It shrinks objects, removes isolated noise pixels and thin protrusions, and breaks narrow connections.
  • Dilation (A ⊕ B): keeps a pixel if the SE touches the object at all. It grows objects, fills small holes and gaps, and joins broken strokes.
  • Opening (A ∘ B = (A ⊖ B) ⊕ B): erosion first, then dilation. It removes small objects, thin spurs and salt noise while restoring the size and shape of surviving objects. It smooths the contour from the outside.
  • Closing (A • B = (A ⊕ B) ⊖ B): dilation first, then erosion. It fills small holes, narrow gaps and pepper noise while keeping the original size. It smooths the contour from the inside.

The difference is purely the order of the two primitives, and that order decides the effect: opening is destructive-then-restorative (removes what shouldn't be there), closing is constructive-then-restorative (fills what shouldn't be missing). Both are idempotent — applying them twice gives the same result as once — and both are duals: opening the object equals closing the background. Applied in sequence (opening then closing) they give the classic morphological noise filter.

Q7. Apply Sobel masks to a 3 × 3 patch and interpret the gradient magnitude.

Ans: Sobel masks:

Gx = [-1  0  +1]        Gy = [-1 -2 -1]
     [-2  0  +2]             [ 0  0  0]
     [-1  0  +1]             [+1 +2 +1]

Take the patch (a clean vertical edge, dark left / bright right):

10  10  100
10  10  100
10  10  100

Gx = (−1)(10) + (0)(10) + (1)(100) + (−2)(10) + (0)(10) + (2)(100) + (−1)(10) + (0)(10) + (1)(100)

= (−10 + 100) + (−20 + 200) + (−10 + 100) = 90 + 180 + 90 = +360

Gy = (−1)(10) + (−2)(10) + (−1)(100) + 0 + (1)(10) + (2)(10) + (1)(100) = (−10 −20 −100) + (10 + 20 + 100) = 0

Magnitude |∇f| = √(Gx² + Gy²) = √(360²) = 360 (the CSS-friendly approximation |Gx| + |Gy| gives the same 360 here).

Direction θ = tan⁻¹(Gy/Gx) = tan⁻¹(0/360) = 0°.

Interpretation: a large magnitude means a strong intensity discontinuity — an edge — is present at this pixel. The gradient vector points across the edge, in the direction of fastest intensity increase (here, horizontally to the right), so the edge itself runs perpendicular to it — a vertical edge. Gy = 0 confirms there is no vertical intensity change. In practice the magnitude image is thresholded to produce a binary edge map; Sobel's 2 weight gives it built-in smoothing, making it less noise-sensitive than the Prewitt or Roberts operators.

Q8. Compare region growing and split-and-merge segmentation.

Ans: Both are region-based (they work on similarity, not discontinuity), but they approach the image from opposite directions:

AspectRegion GrowingSplit-and-Merge
DirectionBottom-up — starts from seed pixels and growsTop-down then bottom-up — starts from the whole image, splits, then merges
MechanismAppend neighbouring pixels that satisfy a similarity predicate P until no more qualifyRecursively quarter any region where P is false (quadtree); then merge adjacent regions whose union satisfies P
SeedsRequired — and results are sensitive to their choiceNot required — fully automatic
Shape biasNone — regions take natural shapesBlocky quadtree artefacts along region boundaries
Main weakness"Leaking" through weak boundaries; order-dependent; slow on large regionsRigid square partitioning; merging criterion is hard to tune
Best forKnown objects of interest — e.g. a tumour marked by a radiologistUnsupervised partitioning of a whole scene

Both must satisfy the formal segmentation conditions: the regions are complete (their union is the image), disjoint, connected, each satisfies P, and the union of any two adjacent regions does not.

Q9. Build a Huffman code for given probabilities; compute Lavg, entropy and efficiency.

Ans: Symbols and probabilities: a₁ = 0.4, a₂ = 0.3, a₃ = 0.1, a₄ = 0.1, a₅ = 0.06, a₆ = 0.04.

Reduction (repeatedly add the two smallest):

0.06 + 0.04 = 0.10 → {0.4, 0.3, 0.1, 0.1, 0.1}

0.1 + 0.1 = 0.20 → {0.4, 0.3, 0.2, 0.1} … continuing: 0.2 + 0.1 = 0.3 → {0.4, 0.3, 0.3}; 0.3 + 0.3 = 0.6 → {0.6, 0.4} → 1.0.

Resulting code (one valid assignment):

SymbolpCodeLength lp × l
a10.4110.40
a20.30020.60
a30.101130.30
a40.1010040.40
a50.060101050.30
a60.040101150.20

Average length Lavg = Σ p·l = 0.40 + 0.60 + 0.30 + 0.40 + 0.30 + 0.20 = 2.20 bits/symbol

Entropy H = −Σ p log₂ p

= 0.4(1.322) + 0.3(1.737) + 0.1(3.322) + 0.1(3.322) + 0.06(4.059) + 0.04(4.644)

= 0.529 + 0.521 + 0.332 + 0.332 + 0.244 + 0.186 = 2.144 bits/symbol

Efficiency η = H / Lavg = 2.144 / 2.20 = 0.975 ≈ 97.5 %

Compression ratio against a fixed 3-bit code (6 symbols need ⌈log₂6⌉ = 3 bits): CR = 3 / 2.20 = 1.36 : 1, i.e. redundancy R = 1 − 1/1.36 = 26.4 % removed.

Huffman is optimal among per-symbol prefix codes and its redundancy (Lavg − H) is always less than 1 bit; it is lossless and its codes are instantaneously decodable because no codeword is a prefix of another.

Q10. What is the key difference between lossless and lossy image compression?

Ans: Lossless compression is perfectly reversible — the decompressed image is bit-for-bit identical to the original. It removes only coding redundancy and interpixel (spatial) redundancy, so compression ratios are modest (typically 2:1 to 4:1). Techniques: Huffman, Arithmetic, LZW, Run-Length Encoding, predictive DPCM. Formats: PNG, GIF, TIFF, lossless JPEG-2000.

Lossy compression is irreversible — information is permanently discarded and the decompressed image is only an approximation. It additionally exploits psychovisual redundancy (detail the human eye cannot perceive, chiefly high frequencies and fine chrominance), achieving far higher ratios (10:1 to 100:1) at a controllable quality cost. Techniques: DCT-based transform coding with quantization (JPEG), wavelet coding (JPEG-2000), chroma subsampling. Quality is measured with MSE / PSNR.

Decision rule: use lossless wherever loss is unacceptable — medical imaging, satellite and forensic imagery, archival masters, text/line-art scans and images to be re-edited; use lossy wherever bandwidth or storage dominates and the viewer is human — web photos, streaming, social media, consumer cameras. Note that repeated lossy save cycles cause generation loss, which is why originals are archived losslessly.

Q11. Describe the OCR pipeline, naming the DIP technique used at each stage.

Ans:

  1. Image acquisition — scanner or camera captures the document; sampling and quantization fix resolution (≥ 300 dpi is the practical minimum for reliable OCR).
  2. Pre-processing — binarization by global (Otsu) or adaptive thresholding; noise removal by median filtering and morphological opening; skew detection and correction using the Hough transform or projection profiles; contrast enhancement by histogram equalization.
  3. Segmentation — page layout analysis separates text from graphics; horizontal projection profiles cut the page into lines, vertical profiles and connected-component labelling cut lines into words and then characters.
  4. Thinning / normalization — morphological skeletonization reduces strokes to one-pixel width; each character is scaled to a standard box so size and font weight stop mattering.
  5. Feature extraction — structural features (loops, endpoints, junctions, strokes, aspect ratio), statistical features (zoning densities, projection histograms, crossings), or transform features (moment invariants, Fourier descriptors). Modern systems replace this stage with learned CNN features.
  6. Classification / recognition — the feature vector is matched against trained models: template matching, k-NN, SVM, neural networks, and today CNN + RNN/LSTM with CTC for whole-line recognition.
  7. Post-processing — a dictionary/lexicon and language model corrects implausible outputs (e.g. "l" ↔ "1", "O" ↔ "0", "rn" ↔ "m"), and the text is written out as searchable PDF, TXT or XML.

CSS angle: OCR underpins NADRA's document digitization, number-plate recognition in Safe City projects, automated cheque clearing, and digitizing Pakistan's land records — while Urdu's cursive, context-sensitive Nastaliq script makes segmentation far harder than for Latin text, which is why Urdu OCR lags and is an active research area at NUST/UET.

Continue studying