Phase congruency¶
| Fact | Value |
|---|---|
| Also called | Phase congruency edges and corners, Kovesi phase congruency, phasecong |
| Status | Implemented and tested |
| Family | Edge detection and boundary analysis (also Texture, corners, blobs and features) |
| Engine | Science engine |
| Type | Nonlinear, frequency, local phase agreement |
| Output | Image |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/phase.py, phase_congruency_filter |
| Operator code | engine/src/pixelnexus_engine/filters/phase.py, phase_congruency_representation |
| Packages | NumPy, SciPy |
Overview¶
Split a signal into sine waves, and look at a step or a thin line. There, all the waves reach a peak or cross zero at the same point: their phases agree. Phase congruency measures this agreement, from 0 to 1. Kovesi built a version for images with log-Gabor filters, noise compensation and a weight for the spread of the frequencies. It finds steps and lines by their shape, not by their contrast, so a faint edge counts about as much as a strong one. From the same computation it gives edges, corners and the type of each feature. The filter runs in the PixelNexus engine, in our own code after Kovesi's.
How it works¶
- Take the luma, extend it with the border mode, and take its FFT.
- Filter it with log-Gabor filters at every scale and direction.
- For each direction, measure how well the phases of the scales agree, less the noise threshold.
- Divide by the summed amplitudes and weight by the spread of the frequencies.
- Combine the directions into the moments M and m, or into the feature type.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(A_s, \phi_s\): The amplitude and phase of the filter response at scale s
- \(\bar\phi\): The phase of the summed responses
- \(T\): The noise threshold
- \(W_o\): The weight for the spread of the frequencies
Phase congruency asks whether the Fourier components of the image agree in phase at a point. At a step or a line they all do, whatever the contrast. Each direction o has a bank of log-Gabor filters at several scales s. Each filter gives an even and an odd response, with the amplitude A_s and the phase phi_s. The energy sums A_s (cos(phi_s - phi_mean) - |sin(phi_s - phi_mean)|), so it is large where the phases agree with their weighted mean. The noise threshold T is subtracted. Kovesi estimates it from the smallest scale, as the mean plus k standard deviations of a Rayleigh distribution. Dividing by the sum of the amplitudes removes the contrast, and a sigmoid weight W_o lowers points where only a few scales respond. The vectors PC_o (cos theta_o, sin theta_o) of all directions form a covariance matrix. Its largest moment M is high on edges and its smallest moment m on corners. The feature type is the angle between the summed even and odd responses.
A log-Gabor filter on one side of the spectrum gives a complex response. The real part is the response of an even filter, which looks like a line. The imaginary part is the response of an odd filter, which looks like a step. Together they give the local amplitude A and phase phi at each scale.
Where all scales have the same phase, the sum of the responses is as long as the sum of their amplitudes, and the ratio is 1. Kovesi's measure uses cos minus the absolute sine of the phase difference. It falls off faster than the cosine alone as the phases drift apart. Dividing by the summed amplitudes removes the contrast. Multiplying the image by a number scales every amplitude, the noise threshold included, so the result stays the same.
Noise also has phase, and in a flat area the few small responses can agree by chance. Kovesi assumes Gaussian noise, whose filter amplitudes follow a Rayleigh distribution. Its median at the smallest scale gives the Rayleigh parameter tau. Kovesi scales the expected noise of each larger scale down by the factor mult, in line with its smaller bandwidth. The threshold T is the mean plus k standard deviations of the summed noise amplitudes. Energy below T counts as 0.
At a point where only one scale responds, all phases trivially agree. The weight W uses the spread of the amplitudes over the scales, from 0 when one scale dominates to 1 when all are equal. A sigmoid with the cutoff and the sharpness g lowers points with little spread.
The directions are combined like a structure tensor. Each direction o gives the vector PC_o (cos theta_o, sin theta_o). The largest moment M of their covariance is high when phase congruency is high in some direction, as at an edge. The smallest moment m is high only when it is high in all directions, as at a corner or a line end.
Kernel or operator¶
The filters are built in the frequency domain. The radial part is Field's log-Gabor function exp(-(ln(f / f_0))^2 / (2 (ln sigmaOnf)^2)), which is 0 at f = 0. It is multiplied by Kovesi's low-pass 1 / (1 + (f / 0.45)^30). The angular part for D directions is the raised cosine (1 + cos(min(D |d theta| / 2, pi))) / 2. It reaches 0 at 2 pi / D from the filter direction. The Inspector asks the engine for the stages with the current settings.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Edges (largest moment M) | Edges (largest moment M), Corners (smallest moment m), Feature type | The largest moment of the phase congruency over the directions marks edges, the smallest marks corners. The feature type tells bright lines, steps and dark lines apart. It shows only where the edge value M is at least 0.05, which is our choice. |
Scales scales |
4 | 2 to 6 | The number of filter scales. Kovesi suggests 3 to 6. |
Directions orientations |
6 | 3 to 12 | The number of filter directions over half a turn. |
Smallest wavelength minWavelength |
3 | 2 to 20 px | The wavelength of the finest filter. |
Scale factor mult |
2.1 | 1.3 to 4 | Each filter has this many times the wavelength of the one before. |
Bandwidth ratio sigmaOnf |
0.55 | 0.3 to 0.9 | The ratio sigma / f_0 of the log-Gabor filters. 0.55 gives about 2 octaves, 0.75 about 1 octave. |
Noise factor k k |
2 | 0 to 20 | The noise threshold is the estimated mean of the noise energy plus k standard deviations. |
Spread cutoff cutoff |
0.5 | 0 to 1 | The spread of the frequencies below which phase congruency is lowered. |
Spread sharpness g |
10 | 1 to 50 | How sharply the weight for the spread of the frequencies changes at the cutoff. |
Stretch stretch |
on | On or off | Stretch the edges or corners so the largest value is white. With it off, values from 0 to 1 show from black to white. A result below 0.001 everywhere is not stretched. |
Border mode border |
Reflect | Reflect 101 (default), Reflect, Replicate, Constant (zero), Wrap | How pixels outside the image are filled when the window reaches past the edge. |
Implementation¶
phase_congruency_filter in engine/src/pixelnexus_engine/filters/phase.py follows the steps of phasecong3 in Kovesi's ImagePhaseCongruency.jl, with NumPy and scipy.fft. It takes the luma with the BT.709 weights in pixel levels. The direction of a frequency is atan2(f_x, f_y), as in Kovesi's code. Kovesi's code differs from this one in four ways:
- It runs on the image as it is. This code first extends the image with the border mode, by half its size and at most 256 pixels on each side.
- It takes the median for the noise over the whole grid. This code takes it over the pixels of the image only.
- It divides by the summed amplitudes as they are. This code adds the epsilon 1e-5, so a flat image gives 0.
- It also offers the mode or a fixed value for the noise. This code uses the median.
The edges and corners are M and m, clipped to 0 to 1. With Stretch on, they are divided by their largest value, unless that is below 0.001. The feature type maps -pi / 2 to black and pi / 2 to white. Away from features it has no meaning, so it shows only where M is at least 0.05, and mid gray elsewhere. That limit is our choice.
Worked example¶
| Step height | No noise | Noise, k = 2 | Noise, k = 5 |
|---|---|---|---|
| 10 | 0.74 | 0.25 | 0.12 |
| 40 | 0.74 | 0.58 | 0.50 |
| 160 | 0.74 | 0.70 | 0.67 |
The largest moment M at a vertical step on a ground of 40, as the median along the middle column of the step, which holds the mean of both sides. The image is 64 x 64 and not rounded. The noise is Gaussian with sigma 2. The other settings are the defaults.
Without noise, every step height gives the same M. With noise, the threshold lowers faint steps much more than strong ones, since their energy is close to the noise energy. A larger k removes more noise and also more of the faint steps.
Complexity¶
- Time: \(O(S D N log N)\)
- Memory: \(O(S N)\)
N is the number of pixels of the extended image, S the number of scales and D the number of directions.
Artifacts and limitations¶
Limitations
- Slow compared to gradient filters. It needs one FFT for each scale and direction.
- Faint texture counts as much as strong edges where it rises above the noise threshold.
- Many parameters, though the defaults are usually fine.
Artifacts
- Thin double responses on each side of a step that falls between pixels.
Tests¶
The grid code must equal a transcription of Kovesi's phasecong3 for M, m and the feature type, to 1e-6, for three images and parameter sets. The filters must have their peak at f_0, 0 at f = 0, and an angular window that reaches 0 at 2 pi / D. The wavelengths and the noise threshold must follow their formulas. Multiplying the image by 3 and adding 17 must not change the result. A step must have type 0 and one peak, a bright line pi / 2 and a dark line -pi / 2. Corners of a square must show in m and not along its sides. A faint and a strong step in one image must give values within a factor of 2. A flat image must stay black even when stretched. The engine API test runs the filter.
References¶
- Kovesi, P. Image features from phase congruency. Videre, Journal of Computer Vision Research 1(3), MIT Press, 1999.
- Kovesi, P. Phase congruency, a low-level image invariant. Psychological Research 64(2), 136 to 148, 2000. DOI 10.1007/s004260000024.
- Kovesi, P. Phase congruency detects corners and edges. Proceedings of DICTA 2003, Sydney, 2003.
- Kovesi, P. ImagePhaseCongruency.jl, the function phasecong3. This code follows its steps. https://github.com/peterkovesi/ImagePhaseCongruency.jl