Blog
Digital Image Processing Past Paper 2014 – MS CS 2nd Term UOS (CS-5813)
University of Sargodha MS Computer Science 2nd term Digital Image Processing (CS-5813) paper, 2014 exam, session 2013-15 (80 marks, 3 hours). The paper has been retyped, and solved answers are given below for all short questions and all six long questions, including contrast stretching, histogram equalisation, finding α and β, 4/8/m-paths, linear filter design, HSI conversion and compass operators. All numbers were computed in Python.
Solved answers
Q1 – Short answers (8 × 2)
i. Size of a 100 × 200 image with 100 gray levels: 100 gray levels need k = ⌈log2100⌉ = 7 bits per pixel. Size = 100 × 200 × 7 = 140,000 bits = 17,500 bytes (≈ 17.1 KB).
ii. Analog to digital image: By sampling (digitising the spatial coordinates on a grid of M × N points) and quantisation (digitising the amplitude/intensity at each sample into one of L discrete gray levels).
iii. Gray-level (intensity) resolution: The smallest discernible change in intensity, set by the number of bits per pixel: k bits give L = 2k levels (8 bits → 256 levels). Too few levels cause false contouring.
iv. Nearest-neighbour and bilinear interpolation: Used in zooming/rotation. Nearest neighbour assigns to a new location the value of the closest original pixel (fast but blocky). Bilinear uses the 4 nearest neighbours: v(x, y) = ax + by + cxy + d, giving smoother results.
v. Linear vs non-linear operations: An operator H is linear if H[a f1 + b f2] = a H[f1] + b H[f2] (additivity and homogeneity), e.g. sum, averaging filter. Otherwise it is non-linear, e.g. max or median: max{f1 + f2} ≠ max{f1} + max{f2} in general.
vi. Cones and rods: Cones (6–7 million, concentrated in the fovea) are sensitive to colour and give fine detail in bright light (photopic vision). Rods (75–150 million, spread over the retina) give an overall picture, are sensitive to low light and not to colour (scotopic vision).
vii. Power-law (gamma) operator: s = c rγ. γ < 1 expands dark values (brightens the image), γ > 1 compresses dark values (darkens it); used for gamma correction of displays and contrast enhancement.
viii. RGB to CMY: With values normalised to [0, 1]: C = 1 − R, M = 1 − G, Y = 1 − B (for 8-bit values, C = 255 − R, etc.).
Q2 – Contrast stretching and histogram equalisation
(a) Full-scale contrast stretch (5-bit, L = 32): rmin = 9, rmax = 14, so s = round[(r − 9) × 31 / (14 − 9)] = round[6.2 (r − 9)].
| r | 9 | 10 | 12 | 13 | 14 |
|---|---|---|---|---|---|
| s | 0 | 6 | 19 | 25 | 31 |
| 6 | 19 | 6 | 0 |
| 19 | 19 | 19 | 31 |
| 25 | 25 | 6 | 0 |
| 31 | 19 | 25 | 19 |
(b) Histogram equalisation (treated as 4-bit, L = 16, 16 pixels): sk = round[15 × Σ nj/16].
| rk | nk | p(rk) | CDF | 15 × CDF | sk |
|---|---|---|---|---|---|
| 9 | 2 | 0.125 | 0.125 | 1.875 | 2 |
| 10 | 3 | 0.1875 | 0.3125 | 4.688 | 5 |
| 12 | 6 | 0.375 | 0.6875 | 10.31 | 10 |
| 13 | 3 | 0.1875 | 0.875 | 13.13 | 13 |
| 14 | 2 | 0.125 | 1.000 | 15.00 | 15 |
| 5 | 10 | 5 | 2 |
| 10 | 10 | 10 | 15 |
| 13 | 13 | 5 | 2 |
| 15 | 10 | 13 | 10 |
(c) Histograms: original – counts 2, 3, 6, 3, 2 at levels 9, 10, 12, 13, 14 (crowded in a narrow band 9–14). Equalised – the same counts 2, 3, 6, 3, 2 now at levels 2, 5, 10, 13, 15, spread over almost the full range 0–15, so contrast is higher. (Bar heights are unchanged because discrete equalisation only moves levels.)
Q3 – Finding α and β
From the known pixel pairs: r = 3 → s = 5 and r = 15 → s = 11 (also r = 3 → s = 5 at the bottom-right).
α·log2(4) + β = 5 → 2α + β = 5
α·log2(16) + β = 11 → 4α + β = 11
Subtract: 2α = 6 → α = 3, β = −1 so s = 3·log2(1 + r) − 1(b) Output “?” corresponds to input r = 1: s = 3 log22 − 1 = 2.
(c) Input “?” gives output 8: 3 log2(1 + r) − 1 = 8 → log2(1 + r) = 3 → 1 + r = 8 → r = 7.
Q4 – Shortest paths and 1-D derivatives
(a) V = {0, 1}; p is the bottom-left pixel (value 1), q the top-right pixel (value 1). Label rows 0 (top) to 3 (bottom).
- 4-path: does not exist. Starting from p and moving only up/down/left/right through pixels with values 0 or 1, we can reach (3,1), (3,2), (2,2), (2,3), (1,2), (1,1), (0,1) and (2,0), but never q: q’s only 4-neighbours, (0,2) and (1,3), both have value 2, which is not in V, so q is 4-isolated.
- 8-path: length 4, e.g. (3,0) → (2,0) → (1,1) → (1,2) → (0,3).
- m-path: length 4, the same path. Each diagonal step is allowed under m-adjacency because the shared 4-neighbours are not in V: for (2,0)→(1,1) they are (1,0) = 2 and (2,1) = 2; for (1,2)→(0,3) they are (0,2) = 2 and (1,3) = 2.
(Lengths found by breadth-first search in Python.)
(b) Using the first derivative ∂f/∂x = f(x + 1) − f(x) and the second derivative ∂²f/∂x² = f(x + 1) + f(x − 1) − 2f(x):
| Signal f | 5 | 5 | 5 | 9 | 9 | 0 | 0 | 5 | 5 | 8 | 1 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Gradient | 0 | 0 | 4 | 0 | -9 | 0 | 5 | 0 | 3 | -7 | 4 | 0 |
| Laplacian | – | 0 | 4 | -4 | -9 | 9 | 5 | -5 | 3 | -10 | 11 | -4 |
| Signal f | 5 | 6 | 8 | 6 | 5 | 3 | 1 | 3 | 4 | 5 | 5 |
|---|---|---|---|---|---|---|---|---|---|---|---|
| Gradient | 1 | 2 | -2 | -1 | -2 | -2 | 2 | 1 | 1 | 0 | – |
| Laplacian | 1 | 1 | -4 | 1 | -1 | 0 | 4 | -1 | 0 | -1 | – |
Observations: the gradient is zero in flat regions, has a single spike at each step edge (+4 at 5→9, −9 at 9→0, +5 at 0→5) and is non-zero along ramps. The Laplacian gives a double response with a zero crossing at each step (+4/−4, −9/+9, +5/−5), a strong response at the isolated point/thin line (8 between 5 and 1 → −10, then +11), and is small along the smooth sinusoidal part.
Q5 – Linear filtering (zero padding)
Filters are applied by correlation with zero padding (the masks are symmetric, so convolution gives the same result).
| 5 | 4 | 3 | 2 |
| 10 | 8 | 6 | 4 |
| 8 | 6 | 4 | 2 |
| 4 | 3 | 2 | 1 |
| 12 | 4 | 3 | 4 |
| 16 | 8 | 6 | 5 |
| 13 | 6 | 4 | 2 |
| 8 | 3 | 2 | 0 |
| 7 | 0 | 0 | 2 |
| 6 | 0 | 0 | 1 |
| 5 | 0 | 0 | 0 |
| 4 | 0 | 0 | -1 |
| 0 | 0 | 0 |
| -1 | 2 | -1 |
| 0 | 0 | 0 |
Q6 – HSI colour model
(a) HSI describes colour the way humans do: Hue (dominant colour, an angle 0°–360°), Saturation (purity – how much white is mixed in) and Intensity (brightness). It decouples intensity from colour information, so image-processing algorithms (e.g. histogram equalisation on I only) can be applied without distorting colours. It is drawn as a double cone/triangle with I on the vertical axis.
RGB → HSI (R, G, B normalised to [0,1])
θ = cos⁻¹{ ½[(R−G) + (R−B)] / √[(R−G)² + (R−B)(G−B)] }
H = θ if B ≤ G, H = 360° − θ if B > G
S = 1 − 3·min(R, G, B) / (R + G + B)
I = (R + G + B) / 3
HSI → RGB
RG sector (0° ≤ H < 120°): B = I(1 − S), R = I[1 + S cos H / cos(60° − H)], G = 3I − (R + B)
GB sector (120° ≤ H < 240°): H = H − 120°; R = I(1 − S), G = I[1 + S cos H / cos(60° − H)], B = 3I − (R + G)
BR sector (240° ≤ H ≤ 360°): H = H − 240°; G = I(1 − S), B = I[1 + S cos H / cos(60° − H)], R = 3I − (G + B)(b) A gray shade has R = G = B, and each component has 28 values, so there are 256 different shades of gray (out of 224 ≈ 16.7 million colours).
Q7 – Compression and compass gradient operators
| Lossless compression | Lossy compression |
|---|---|
| Original data recovered exactly after decompression | Some information is permanently discarded; only an approximation is recovered |
| Lower compression ratio (≈ 2:1 – 3:1) | Much higher compression ratio (10:1 – 50:1 or more) |
| Removes coding and inter-pixel redundancy | Also removes psycho-visual redundancy (details the eye does not notice) |
| Huffman, LZW, run-length, PNG, GIF | JPEG, transform coding with quantisation, MPEG |
| Used for medical images, text, archives | Used for photos, video, web images |
(b) Compass gradient operators (Prewitt-type, coefficients 0, ±1). Each mask responds most strongly when intensity increases in its named direction; the edge it detects is perpendicular to that direction:
| -1 | 0 | 1 |
| -1 | 0 | 1 |
| -1 | 0 | 1 |
| 0 | 1 | 1 |
| -1 | 0 | 1 |
| -1 | -1 | 0 |
| 1 | 1 | 1 |
| 0 | 0 | 0 |
| -1 | -1 | -1 |
| 1 | 1 | 0 |
| 1 | 0 | -1 |
| 0 | -1 | -1 |
| 1 | 0 | -1 |
| 1 | 0 | -1 |
| 1 | 0 | -1 |
| 0 | -1 | -1 |
| 1 | 0 | -1 |
| 1 | 1 | 0 |
| -1 | -1 | -1 |
| 0 | 0 | 0 |
| 1 | 1 | 1 |
| -1 | -1 | 0 |
| -1 | 0 | 1 |
| 0 | 1 | 1 |
E/W masks detect vertical edges (gradient horizontal), N/S masks detect horizontal edges (gradient vertical), NE/SW detect edges along the NW–SE diagonal and NW/SE detect edges along the NE–SW diagonal. Each mask is the previous one rotated by 45°, and the mask with the largest response gives the edge direction at that pixel.
