EP08_08 — 🟡 Grayscale Image and Dynamic Thresholding
8.14.8 EP08_08 🟡 Grayscale Image and Dynamic Thresholding
In this exercise, the input image is no longer strictly binary (0/1) but becomes a grayscale image (\(8\) bits, \(0\dots255\)), where objects have an intermediate average intensity over a dark background (\(0\)), in addition to salt-and-pepper noise scattered throughout the entire image.
8.14.8.1 📋 Implementation Guidelines
Input: read \(H\) and \(W\) on the first line, followed by the \(H\) lines with integer values from \(0\) to \(255\) in an \(H \times W\) matrix.
Preprocessing:
Apply a Median filter (\(3 \times 3\)) to remove salt-and-pepper noise while keeping edges sharp.
Apply Otsu’s thresholding (or a fixed threshold \(T = 60\)) to binarize the clean image.
Measurement and Output: extract the contour of the objects, compute the geometric metrics (area, perimeter, cx, cy, x, y, w, h, circularity, solidity), and sort the objects by bbox[0] (with bbox[1] as a tiebreaker). Reassign id from \(1\) to \(N\) and print the table.
For sorting, use medidas.sort(key=lambda m: (m['bbox'][1], m['bbox'][0])), with medidas = mm.measure(img).