Image Practice Problems
Image Practice Problems
Contents
Problem 1 — Shortest 4-, 8-, and m-adjacency paths
Problem 2 — Contrast stretching (dynamic-range, piecewise linear)
Problem 3 — Histogram equalization for a 4-bit (L = 16) image
Problem 4 — Gray-level slicing — highlight a range (no background)
Problem 5 — Standard 3×3 averaging filter with replicated padding
Problem 6 — Weighted-average (Gaussian-approximation) 3×3 filter
Problem 7 — Median filter on salt-and-pepper noise
Problem 8 — Laplacian filtering (4-neighbour kernel)
Problem 9 — Composite (8-neighbour) Laplacian — single pixel
Problem 10 — Unsharp masking / high-boost (k = 2)
Problem 11 — Sobel edge detection — gradient magnitude
Problem 12 — Point detection with the Laplacian-style mask
Problem 13 — Multilevel thresholding
Problem 14 — Generating a Region of Interest with masking
Problem 15 — Comparing three intensity transformations (negative, log, gamma)
Page 1 of 31
CET352 — Practice Problems
QUESTION
Given the following 5×5 image with V = {1, 2, 3}, find the shortest 4-, 8-, and m-paths from p (bottom-left)
to q (top-right).
Image (top-left is row 0, col 0)
1 0 2 3 q=2
0 3 0 1 2
1 0 1 2 0
3 2 0 1 3
p=2 1 0 0 3
APPROACH
This problem tests m-adjacency carefully — the diagonal moves are only legal when the two diagonal pixels
do NOT share a 4-neighbour that lies in V.
STEP-BY-STEP SOLUTION
Step 1 — Identify pixels in V
Mark every cell whose value ∈ {1, 2, 3}. Cells with 0 are blocked.
Step 2 — 4-path
Try every horizontal/vertical route from p(4,0)=2 to q(0,4)=2:
• From (4,0): right (4,1)=1 ✓, but (4,2)=0 blocks.
• Going up: (3,0)=3 ✓→ (2,0)=1 ✓→ (1,0)=0 blocks.
• Try (3,0)=3 → (3,1)=2 ✓→ (3,2)=0 blocks.
• Routing along column 3 also fails because rows/columns of zeros block the way.
• Conclusion: NO 4-PATH EXISTS.
Step 3 — 8-path (diagonals allowed)
p(4,0)=2 → (3,1)=2 → (2,2)=1 → (1,3)=1 → (0,4)=2. All five pixels are in V.
→ Length = 4 edges
Step 4 — m-path
Re-examine every diagonal step in the 8-path:
• (3,1)→(2,2): common 4-neighbours are (3,2)=0 and (2,1)=0 — neither in V → diagonal is LEGAL.
• (2,2)→(1,3): common 4-neighbours are (2,3)=2 ∈ V → diagonal is BLOCKED.
• Must reroute via (2,3): (2,2)=1 → (2,3)=2 → (1,3)=1 → (0,3)=3 → (0,4)=2.
Page 2 of 31
CET352 — Practice Problems
FINAL ANSWER
✓4-path: no solution (blocked by zeros)
✓8-path: length 4
✓m-path: length 6
Page 3 of 31
CET352 — Practice Problems
QUESTION
Apply dynamic-range contrast stretching with r₁ = 60, r₂ = 170, s₁ = 20, s₂ = 230 to the following 2×2 image.
Image
40 120
165 220
APPROACH
Three linear segments with slopes α, β, γ. For each pixel, identify which region it falls in, then apply the
matching segment formula.
STEP-BY-STEP SOLUTION
Step 1 — Compute the slopes
From the three breakpoints (0,0), (r₁,s₁), (r₂,s₂), (255,255):
• α = s₁/r₁ = 20/60 ≈ 0.333
• β = (s₂ − s₁)/(r₂ − r₁) = 210/110 ≈ 1.909
• γ = (255 − s₂)/(255 − r₂) = 25/85 ≈ 0.294
Step 2 — Apply to each pixel
Pixel r Region Calculation s (rounded)
40 r ≤ 60 α × r = 0.333 × 13
40 = 13.3
120 60 < r ≤ 170 β × (120 − 60) + 135
20 = 1.909 × 60 +
20 = 134.5
Page 4 of 31
CET352 — Practice Problems
FINAL ANSWER
Resulting image
13 135
221 245
Page 5 of 31
CET352 — Practice Problems
QUESTION
Given a 4×4 image with intensities in [0, 15] (4-bit depth, L = 16), (a) compute the histogram and (b) apply
histogram equalization.
Image
3 3 5 6
5 7 7 8
8 9 10 10
10 11 11 13
APPROACH
Histogram → PDF → CDF → multiply CDF by (L − 1) = 15 → round to the nearest integer.
Page 6 of 31
CET352 — Practice Problems
STEP-BY-STEP SOLUTION
Step 1 — Total pixels
N = 4 × 4 = 16.
Step 2 — Build the histogram
Count occurrences of each level. Levels 0, 1, 2, 4, 12, 14, 15 have zero count.
Level r Count n
3 2
5 2
6 1
7 2
8 2
9 1
10 3
11 2
13 1
Page 7 of 31
CET352 — Practice Problems
FINAL ANSWER
Equalized image
2 2 4 5
4 7 7 8
8 9 12 12
12 14 14 15
Page 8 of 31
CET352 — Practice Problems
QUESTION
Apply gray-level slicing to highlight the range [80, 160] WITHOUT preserving background. Pixels inside the
range → 255; pixels outside → 0.
Image
30 100 150 200
50 120 160 75
180 90 110 220
APPROACH
Apply the rule: s = 255 if 80 ≤ r ≤ 160, else s = 0. Process each pixel independently (point operation).
STEP-BY-STEP SOLUTION
Pixel-by-pixel evaluation
Each pixel is mapped independently.
• 30 → outside → 0
• 100 → inside → 255
• 150 → inside → 255
• 200 → outside → 0
• 50 → outside → 0
• 120 → inside → 255
• 160 → inside (boundary) → 255
• 75 → outside → 0
• 180 → outside → 0
• 90 → inside → 255
• 110 → inside → 255
• 220 → outside → 0
Page 9 of 31
CET352 — Practice Problems
FINAL ANSWER
Sliced image
0 255 255 0
0 255 255 0
0 255 255 0
Page 10 of 31
CET352 — Practice Problems
QUESTION
Apply a 3×3 standard average filter to the image using REPLICATED padding.
Image
20 20 60 60
20 30 60 60
20 20 60 60
20 20 60 60
APPROACH
Pad size = 1 on each side (kernel is 3×3). Replicate boundary pixels outward. Output(i,j) = mean of 3×3
window centered at (i,j).
STEP-BY-STEP SOLUTION
Step 1 — Build the padded image
Each edge replicates the boundary pixel outward.
Padded image (6×6)
20 20 20 60 60 60
20 20 20 60 60 60
20 20 30 60 60 60
20 20 20 60 60 60
20 20 20 60 60 60
20 20 20 60 60 60
Page 11 of 31
CET352 — Practice Problems
FINAL ANSWER
Filtered output (rounded)
21 32 48 60
21 32 48 60
21 32 48 60
20 32 48 60
Page 12 of 31
CET352 — Practice Problems
QUESTION
Apply a weighted-average filter with kernel
Kernel = (1/16) ×
1 2 1
2 4 2
1 2 1
to the image with replicated padding. Compute the four interior output pixels.
Image (a bright square on a dark background)
10 10 10 10
10 100 100 10
10 100 100 10
10 10 10 10
APPROACH
Convolve the kernel with the image. For each output pixel, multiply each neighbour by the corresponding
kernel weight, sum, divide by 16.
STEP-BY-STEP SOLUTION
Output at (1,1)
Window rows 0–2, cols 0–2:
• Row sum 1: (10·1)+(10·2)+(10·1) = 40
• Row sum 2: (10·2)+(100·4)+(100·2) = 620
• Row sum 3: (10·1)+(100·2)+(100·1) = 310
• Total = 970. 970 / 16 = 60.6 → 61
Output at (1,2)
By the symmetry of kernel and image: also 61.
Outputs at (2,1) and (2,2)
Same value by symmetry: 61.
Page 13 of 31
CET352 — Practice Problems
FINAL ANSWER
✓All four interior pixels: 61
✓The bright 100-pixels were reduced to ~61 by the low-pass filter.
Page 14 of 31
CET352 — Practice Problems
QUESTION
Apply a 3×3 median filter (with replicated padding) to the noisy image below.
Image with salt (255) and pepper (0) noise
100 102 100 99 101
98 255 102 100 99
101 99 103 0 102
100 101 99 100 98
99 100 98 255 99
APPROACH
Sort each 3×3 window; replace the center pixel with the median (the 5th element of 9 sorted values).
STEP-BY-STEP SOLUTION
Pixel (1,1) — currently 255 (salt)
Window: 100, 102, 100, 98, 255, 102, 101, 99, 103.
• Sorted: 98, 99, 100, 100, 101, 102, 102, 103, 255
• Median (5th value) = 101
Pixel (2,3) — currently 0 (pepper)
Window: 102, 100, 99, 103, 0, 102, 99, 100, 98.
• Sorted: 0, 98, 99, 99, 100, 100, 102, 102, 103
• Median = 100
Pixel (4,3) — currently 255
Window includes row 3 and replicated row 5.
• Sorted: 98, 98, 98, 99, 100, 100, 100, 255, 255
• Median = 100
All other pixels
Their median is near their own value (or a neighbour); flat-region pixels shift by at most 1–2 levels.
Page 15 of 31
CET352 — Practice Problems
FINAL ANSWER
✓(1,1): 255 → 101 (salt removed)
✓(2,3): 0 → 100 (pepper removed)
✓(4,3): 255 → 100 (salt removed)
Page 16 of 31
CET352 — Practice Problems
QUESTION
Apply the Laplacian for g(x,y) = f(x,y) − ∇²f using kernel
0 1 0
1 -4 1
0 1 0
with replicated padding. Compute both ∇²f and the sharpened image g.
Image (bright spot at (1,2), bright row at the bottom)
20 20 20 20
20 20 40 20
20 20 20 20
80 80 80 80
APPROACH
Compute ∇²f using the kernel, then subtract from the original.
STEP-BY-STEP SOLUTION
Laplacian at (0,0) — replicated padding
Center = 20; top, bottom, left, right all = 20. ∇²f = 20 + 20 + 20 + 20 + (−4·20) = 80 − 80 = 0.
Laplacian at (1,1)
Center = 20, neighbours: top=20, bottom=20, left=20, right=40. ∇²f = 20 + 20 + 20 + 40 + (−4·20) = 100 − 80
= 20.
Laplacian at (1,2) — the bright spot
Center = 40, all four neighbours = 20. ∇²f = 20·4 + (−4·40) = 80 − 160 = −80.
Laplacian at (2,0)
Center = 20, bottom = 80 (the bright row), others = 20. ∇²f = 20 + 20 + 20 + 80 + (−4·20) = 140 − 80 = 60.
Page 17 of 31
CET352 — Practice Problems
FINAL ANSWER
∇²f over the full 4×4 image
0 0 20 0
0 0 -80 0
60 60 40 60
-60 -60 -60 -60
✓Sharpened g = f − ∇²f:
✓(0,2): 20 − 20 = 0
✓(1,2): 40 − (−80) = 120 (the bright spot becomes much brighter)
✓(2,0): 20 − 60 = −40 → clipped to 0
✓(3,0): 80 − (−60) = 140 (the bottom row is enhanced)
Page 18 of 31
CET352 — Practice Problems
QUESTION
Apply the composite Laplacian g = f − ∇²f using kernel
1 1 1
1 -8 1
1 1 1
APPROACH
Apply the kernel at the chosen location, then subtract the Laplacian from the original.
STEP-BY-STEP SOLUTION
Step 1 — Window centered at (2,2)
• Top row: 40, 40, 40
• Middle row: 40, 100, 100
• Bottom row: 40, 100, 100
Step 2 — Apply the kernel
Multiply each pixel by its corresponding weight:
• Sum of the eight neighbours (each ×1): 40 + 40 + 40 + 40 + 100 + 40 + 100 + 100 = 500
• Center contribution: −8 × 100 = −800
• ∇²f = 500 + (−800) = −300
Step 3 — Sharpened pixel
g(2,2) = f(2,2) − ∇²f(2,2) = 100 − (−300) = 400 (saturates to 255).
Page 19 of 31
CET352 — Practice Problems
FINAL ANSWER
✓g(2,2) = 400 (raw) → clipped to 255
Page 20 of 31
CET352 — Practice Problems
QUESTION
Compute the high-boost output g = f + k·(f − f̄) with k = 2 at position (1,1), where f̄ is the 3×3 standard-
average smoothing with replicated padding.
Image (linear horizontal ramp)
40 60 80 100
40 60 80 100
40 60 80 100
40 60 80 100
APPROACH
(1) Compute f̄ via averaging. (2) Compute mask = f − f̄. (3) Apply g = f + k · mask.
STEP-BY-STEP SOLUTION
Step 1 — Compute average at (1,1)
Window rows 0–2, cols 0–2 = (40+60+80) repeated 3 times = 180·3 = 540. Mean = 540/9 = 60.
Step 2 — Compute the mask at (1,1)
Mask = f(1,1) − f̄(1,1) = 60 − 60 = 0.
Step 3 — Apply high-boost
g(1,1) = 60 + 2 × 0 = 60.
FINAL ANSWER
✓g(1,1) = 60 (unchanged because the local region is a perfectly linear ramp — no edge to sharpen)
Page 21 of 31
CET352 — Practice Problems
QUESTION
Compute the gradient magnitude |Gx| + |Gy| at position (1,1) with replicated padding using the standard
Sobel kernels.
Image (vertical edge between cols 1 and 2 of the lower three rows)
10 10 10 10
10 10 200 200
10 10 200 200
10 10 200 200
APPROACH
Apply Gx and Gy separately. Magnitude ≈ |Gx| + |Gy|. Recall Gx = [[-1,0,1],[-2,0,2],[-1,0,1]] and Gy = [[-1,-
2,-1],[0,0,0],[1,2,1]].
STEP-BY-STEP SOLUTION
Step 1 — Window centered at (1,1)
• Top: 10, 10, 10
• Middle: 10, 10, 200
• Bottom: 10, 10, 200
Step 2 — Compute Gx at (1,1)
Sum of (kernel × pixel):
• (−1)·10 + 0·10 + 1·10 = 0
• (−2)·10 + 0·10 + 2·200 = 380
• (−1)·10 + 0·10 + 1·200 = 190
• Gx = 0 + 380 + 190 = 570
Step 3 — Compute Gy at (1,1)
• (−1)·10 + (−2)·10 + (−1)·10 = −40
• 0·10 + 0·10 + 0·200 = 0
• 1·10 + 2·10 + 1·200 = 230
• Gy = −40 + 0 + 230 = 190
Step 4 — Magnitude
|Gx| + |Gy| = 570 + 190 = 760.
Page 22 of 31
CET352 — Practice Problems
FINAL ANSWER
✓Gradient magnitude at (1,1) = 760 — a strong edge response
Page 23 of 31
CET352 — Practice Problems
QUESTION
Apply the point-detection mask
-1 -1 -1
-1 8 -1
-1 -1 -1
APPROACH
Apply the mask at (2,1); the pixel is flagged when |R| > T.
STEP-BY-STEP SOLUTION
Step 1 — Window centered at (2,1)
• Top: 11, 13, 12
• Middle: 12, 250, 11
• Bottom: 13, 12, 11
Step 2 — Compute R
• Sum of the eight neighbours: 11+13+12+12+11+13+12+11 = 95
• Each neighbour multiplied by −1 contributes −95
• Center contribution: 8 × 250 = 2000
• R = −95 + 2000 = 1905
Step 3 — Threshold check
|R| = 1905 > T = 100 → pixel detected.
Page 24 of 31
CET352 — Practice Problems
FINAL ANSWER
✓Pixel (2,1) IS detected as an isolated point (R = 1905, far above the threshold)
Page 25 of 31
CET352 — Practice Problems
QUESTION
Apply two-level thresholding with T₁ = 60 and T₂ = 150. Convention: background = 0, first object = 128,
second object = 255.
Image
40 80 120 180
55 90 150 200
30 60 110 170
APPROACH
Compare each pixel to T₁ and T₂. Rules: f ≤ T₁ → 0; T₁ < f ≤ T₂ → 128; f > T₂ → 255.
STEP-BY-STEP SOLUTION
Pixel-by-pixel evaluation
• 40 → ≤ 60 → 0
• 80 → 60 < 80 ≤ 150 → 128
• 120 → 128
• 180 → > 150 → 255
• 55 → 0
• 90 → 128
• 150 → boundary, included in first object → 128
• 200 → 255
• 30 → 0
• 60 → boundary, ≤ T₁ → 0
• 110 → 128
• 170 → 255
FINAL ANSWER
Thresholded image
0 128 128 255
0 128 128 255
0 0 128 255
Page 26 of 31
CET352 — Practice Problems
Page 27 of 31
CET352 — Practice Problems
QUESTION
Given image a and binary mask b, compute the ROI c = a · b (pointwise multiplication).
Image a (5×5)
20 30 40 50 60
25 35 45 55 65
30 40 50 60 70
35 45 55 65 75
40 50 60 70 80
Mask b (5×5)
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
APPROACH
Pixel-wise multiply: c(i,j) = a(i,j) × b(i,j). Wherever b = 0, c = 0; wherever b = 1, c equals a.
STEP-BY-STEP SOLUTION
Apply the multiplication
The 3×3 inner region (b = 1) preserves a; everywhere else b = 0 zeroes out a.
Page 28 of 31
CET352 — Practice Problems
FINAL ANSWER
ROI image c
0 0 0 0 0
0 35 45 55 0
0 40 50 60 0
0 45 55 65 0
0 0 0 0 0
Page 29 of 31
CET352 — Practice Problems
QUESTION
Given the 8-bit image, apply: (a) Negative s = 255 − r; (b) Log s = c · log(1 + r) with c = 46.04; (c) Gamma s =
r^0.5.
Image
20 100
180 250
APPROACH
Apply each formula pixel-by-pixel; round to integer; observe the different effects.
STEP-BY-STEP SOLUTION
(a) Negative — s = 255 − r
• 20 → 235
• 100 → 155
• 180 → 75
• 250 → 5
(b) Log — s = 46.04 · log(1 + r)
(c was chosen so that r=250 maps to ~110)
• 20 → 46.04 · log(21) = 46.04 · 1.322 = 60.9 → 61
• 100 → 46.04 · log(101) = 46.04 · 2.004 = 92.3 → 92
• 180 → 46.04 · log(181) = 46.04 · 2.258 = 104.0 → 104
• 250 → 46.04 · log(251) = 46.04 · 2.400 = 110.5 → 111
(c) Gamma (γ = 0.5, c = 1) — s = √r
• 20 → √20 = 4.47 → 4
• 100 → 10
• 180 → √180 = 13.4 → 13
• 250 → √250 = 15.8 → 16
FINAL ANSWER
✓Negative inverts the image: bright becomes dark, dark becomes bright.
✓Log brightens dark detail; bright pixels are compressed (250 only reaches 111).
✓Gamma 0.5 (square root) brightens overall but compresses to a narrow output range — usually
rescaled to [0,255] in practice.
Page 30 of 31
CET352 — Practice Problems
Page 31 of 31