6.14.6 EP06_06 🔴 Local Variance Map for Texture Detection
A fabric factory needs to inspect rolls of cloth in real time, without having a reference image available — each roll presents small natural variations. In this situation, the strategy presented in the chapter consists of analyzing the local homogeneity of the texture: uniform regions exhibit low intensity variance in small neighborhoods, while scratches, stains, and manufacturing defects produce local increases in this variance.
In this exercise, you will implement the core of this method, calculating the local variance in a sliding window and generating a binary mask that identifies regions whose variance exceeds a threshold.
6.14.6.1 📋 Implementation Guidelines
Dimensions and parameters: Read the integers \(L\), \(C\), \(k\) (window size, always odd) and \(T\) (variance threshold).
Image: Read the \(L \times C\) integer values of the texture matrix (intensities between 0 and 255).
Border handling: When the window extends beyond the image boundaries, use border replication, i.e., repeat the value of the nearest valid pixel.
Local mean: For each position \((i,j)\), compute \[ \mu(i,j)= \frac{1}{k^2} \sum_{(p,q)\in\text{window}} \text{texture}(p,q). \]
Local variance: Compute the population variance of the window, \[ \sigma^2(i,j)= \frac{1}{k^2} \sum_{(p,q)\in\text{window}} \left(\text{texture}(p,q)-\mu(i,j)\right)^2, \] or, equivalently, \[ \sigma^2(i,j)=\overline{x^2}-\mu(i,j)^2, \] where \(\overline{x^2}\) represents the mean of the squared intensities.
Rounding: Round the variance to the nearest integer (round half away from zero, using
np.floor(res_norm + 0.5)).Thresholding: Set \(\text{mask}(i,j)=1\) if the rounded variance is strictly greater than \(T\); otherwise, set \(\text{mask}(i,j)=0\).
Output: Print the resulting binary mask.
6.14.6.2 📌 Computational Constraints
- Border replication: use the value of the nearest valid pixel whenever the window extends beyond the image boundaries.
- Population variance: use denominator \(k^2\), never \(k^2-1\).
- Strict comparison: the mask must be computed using the condition \(\sigma^2_{\text{rounded}}>T\).
- Odd window: the value of \(k\) is always odd, ensuring a central pixel.
6.14.6.3 🧠 Theoretical Foundation
| Situation | Local variance | Interpretation |
|---|---|---|
| Uniform region | Low | Similar intensities in the neighborhood. |
| Region containing a defect | High | The presence of distinct intensities increases the dispersion of values. |
| Small window | Greater sensitivity to details and noise | Detects localized changes. |
| Large window | Smoother response | Highlights larger defects, but reduces the precision of their localization. |
Local variance measures the dispersion of intensities in a neighborhood. Homogeneous regions exhibit low variance, while texture changes increase this measure, allowing the identification of potential defects through simple thresholding.
6.14.6.4 📦 Input and Output Specification (VPL)
Input:
- Line 1: Integer \(L\).
- Line 2: Integer \(C\).
- Line 3: Integer \(k\) (odd).
- Line 4: Integer \(T\).
- Next \(L\) lines: integer elements of the texture matrix.
Output:
- Binary mask (values 0 or 1), with \(L\) rows and \(C\) columns.
6.14.6.5 📌 Examples
| Input | Output | Observation |
|---|---|---|
| 3 3 3 50 10 10 10 10 10 10 10 90 10 |
0 0 0 1 1 1 1 1 1 |
The defect increases the variance in all windows that contain it. |
| 2 2 3 5 100 100 100 100 |
0 0 0 0 |
The texture is uniform; the variance is zero throughout the image. |
%%writefile EP06_06.py
# Python codeOverwriting EP06_06.py
TestSuite("EP06_06.py").run()✔️ EP06_06.cases already exists in casos/
📋 5 case(s) loaded from casos/EP06_06.cases
🔍 Testing Python: EP06_06.py
⚠️ EP06_06.py: Empty file (fewer than 3 lines). Tests skipped.