EP06_04 — 🟡 Median Angular Slope Estimator ( Deskew )
6.14.4 EP06_04 🟡 Median Angular Slope Estimator (Deskew)
After edge detection and the application of the Hough Transform, a set of candidate lines for the predominant orientation of the document is obtained. Each line provides an estimate of the skew angle, calculated by
However, not all lines correspond to document lines: some result from noise, shadows, or other image elements. In this exercise, you will implement the robust estimation step of the skew angle, filtering plausible values and computing their median.
6.14.4.1 📋 Implementation Guidelines
Quantity: Read the integer \(M\), corresponding to the number of estimated angles.
Angles: Read the \(M\) real values, in degrees.
Filtering: Keep only the angles that strictly satisfy \(-45 < \text{angle} < 45\).
No candidates: If no angle remains after filtering, print exactly SEM_CORRECAO.
Median: If valid angles exist:
if the quantity is odd, the median is the central element of the ordered sequence;
if it is even, the median is the arithmetic mean of the two central elements.
Output: Print the median rounded to two decimal places (standard rounding, round half away from zero, with np.floor(img + 0.5)).
6.14.4.2 📌 Computational Constraints
Open interval: angles equal to \(-45\) or \(45\) must not be considered.
Precision: compute the median using the original values; rounding must be performed only at the output.
Empty case: if there are no valid angles, no median must be computed.
6.14.4.3 🧠 Theoretical Background
Situation
Result
Most angles concentrated around the true skew
The median approximates the document orientation.
Few discrepant angles (outliers)
The median is little influenced by these values.
Angles outside the interval \((-45^\circ,45^\circ)\)
They are discarded before the computation.
No valid angle
No correction is applied (SEM_CORRECAO).
The median is used because it is more robust than the mean in the presence of a few discrepant values, producing a more stable estimate of the predominant document skew.
6.14.4.4 📦 Input and Output Specification (VPL)
Input:
Line 1: Integer \(M\).
Line 2: \(M\) real numbers, corresponding to the angles in degrees.
Output:
A single line containing the estimated angle, with two decimal places, or the word SEM_CORRECAO if no angle is valid.
6.14.4.5 📌 Examples
Input
Output
Remark
5
-50 -10.5 2.3 2.3 47
2.30
Only angles in the interval \((-45,45)\) are considered; the median is \(2.3\).
4
-46 50 45 -45
SEM_CORRECAO
No angle belongs to the open interval \((-45,45)\).