% PHASECONG3 - Computes edge and corner phase congruency in an image.
%
% This function calculates the PC_2 measure of phase congruency.
% This function supersedes PHASECONG2 and PHASECONG being faster and requires
% less memory
%
% There are potentially many arguments, here is the full usage:
%
% [M m or ft pc EO, T] = phasecong3(im, nscale, norient, minWaveLength, ...
% mult, sigmaOnf, k, cutOff, g, noiseMethod)
%
% However, apart from the image, all parameters have defaults and the
% usage can be as simple as:
%
% M = phasecong3(im);
%
% Arguments:
% Default values Description
%
% nscale 4 - Number of wavelet scales, try values 3-6
% norient 6 - Number of filter orientations.
% minWaveLength 3 - Wavelength of smallest scale filter.
% mult 2.1 - Scaling factor between successive filters.
% sigmaOnf 0.55 - Ratio of the standard deviation of the Gaussian
% describing the log Gabor filter's transfer function
% in the frequency domain to the filter center frequency.
% k 2.0 - No of standard deviations of the noise energy beyond
% the mean at which we set the noise threshold point.
% You may want to vary this up to a value of 10 or
% 20 for noisy images
% cutOff 0.5 - The fractional measure of frequency spread
% below which phase congruency values get penalized.
% g 10 - Controls the sharpness of the transition in
% the sigmoid function used to weight phase
% congruency for frequency spread.
% noiseMethod -1 - Parameter specifies method used to determine
% noise statistics.
% -1 use median of smallest scale filter responses
% -2 use mode of smallest scale filter responses
% 0+ use noiseMethod value as the fixed noise threshold
%
% Returned values:
% M - Maximum moment of phase congruency covariance.
% This is used as a indicator of edge strength.
% m - Minimum moment of phase congruency covariance.
% This is used as a indicator of corner strength.
% or - Orientation image in integer degrees 0-180,
% positive anticlockwise.
% 0 corresponds to a vertical edge, 90 is horizontal.
% ft - Local weighted mean phase angle at every point in the
% image. A value of pi/2 corresponds to a bright line, 0
% corresponds to a step and -pi/2 is a dark line.
% pc - Cell array of phase congruency images (values between 0 and 1)
% for each orientation
% EO - A 2D cell array of complex valued convolution result
% T - Calculated noise threshold (can be useful for
% diagnosing noise characteristics of images). Once you know
% this you can then specify fixed thresholds and save some
% computation time.
%
% EO{s,o} = convolution result for scale s and orientation o. The real part
% is the result of convolving with the even symmetric filter, the imaginary
% part is the result from convolution with the odd symmetric filter.
%
% Hence:
% abs(EO{s,o}) returns the magnitude of the convolution over the
% image at scale s and orientation o.
% angle(EO{s,o}) returns the phase angles.
%
% Notes on specifying parameters:
%
% The parameters can be specified as a full list eg.
% >> [M m or ft pc EO] = phasecong3(im, 5, 6, 3, 2.5, 0.55, 2.0, 0.4, 10);
%
% or as a partial list with unspecified parameters taking on default values
% >> [M m or ft pc EO] = phasecong3(im, 5, 6, 3);
%
% or as a partial list of parameters followed by some parameters specified via a
% keyword-value pair, remaining parameters are set to defaults, for example:
% >> [M m or ft pc EO] = phasecong3(im, 5, 6, 3, 'cutOff', 0.3, 'k', 2.5);
%
% The convolutions are done via the FFT. Many of the parameters relate to the
% specification of the filters in the frequency plane. The values do not seem
% to be very critical and the defaults are usually fine. You may want to
% experiment with the values of 'nscales' and 'k', the noise compensation factor.
%
% Notes on filter settings to obtain even coverage of the spectrum
% sigmaOnf .85 mult 1.3
% sigmaOnf .75 mult 1.6 (filter bandwidth ~1 octave)
% sigmaOnf .65 mult 2.1
% sigmaOnf .55 mult 3 (filter bandwidth ~2 octaves)
%
% See Also: PHASECONG, PHASECONG2, PHASESYM, GABORCONVOLVE, PLOTGABORFILTERS
% References:
%
% Peter Kovesi, "Image Features From Phase Congruency". Videre: A
% Journal of Computer Vision Research. MIT Press. Volume 1, Number 3,
% Summer 1999 http://mitpress.mit.edu/e-journals/Videre/001/v13.html
%
% Peter Kovesi, "Phase Congruency Detects Corners and
% Edges". Proceedings DICTA 2003, Sydney Dec 10-12
% April 1996 Original Version written
% August 1998 Noise compensation corrected.
% October 1998 Noise compensation corrected. - Again!!!
% September 1999 Modified to operate on non-square images of arbitrary size.
% May 2001 Modified to return feature type image.
% July 2003 Altered to calculate 'corner' points.
% October 2003 Speed improvements and refinements.
% July 2005 Better argument handling, changed order of return values
% August 2005 Made Octave compatible
% May 2006 Bug in checkargs fixed
% Jan 2007 Bug in setting radius to 0 for odd sized images fixed.
% April 2009 Scaling of covariance values fixed. (Negligible change to results)
% May 2009 Noise compensation simplified reducing memory and
% computation overhead. Spread function changed to a cosine,
% eliminating parameter dThetaOnSigma and ensuring even
% angular coverage. Frequency width measure slightly
% improved.
% November 2010 Cosine angular spread function corrected (it was 2x as wide
% as it should have been)
% September 2017 Correction to setting up matrices of frequency values for
% odd sized images.
% Copyright (c) 1996-2017 Peter Kovesi
% Centre for Exploration Targeting
% The University of Western Australia
% peter.kovesi at uwa edu au
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% The Software is provided "as is", without warranty of any kind.
function [M, m, or, featType, PC, EO, T, pcSum] = phasecong3(varargin)
% Get arguments and/or default values
[im, nscale, norient, minWaveLength, mult, sigmaOnf, ...
k, cutOff, g, noiseMethod] = checkargs(varargin(:));
epsilon = .0001; % Used to prevent division by zero.
[rows,cols] = size(im);
imagefft = fft2(im); % Fourier transform of image
zero = zeros(rows,cols);
EO = cell(nscale, norient); % Array of convolution results.
PC = cell(norient,1);
covx2 = zero; % Matrices for covariance data
covy2 = zero;
covxy = zero;
EnergyV = zeros(rows,cols,3); % Matrix for accumulating total energy