Switch to unified view

a b/functions/functions_Kovesi/ridgeorient.m
1
% RIDGEORIENT - Estimates the local orientation of ridges in a fingerprint
2
%
3
% Usage:  [orientim, reliability, coherence] = ridgeorientation(im, gradientsigma,...
4
%                                             blocksigma, ...
5
%                                             orientsmoothsigma)
6
%
7
% Arguments:  im                - A normalised input image.
8
%             gradientsigma     - Sigma of the derivative of Gaussian
9
%                                 used to compute image gradients.
10
%             blocksigma        - Sigma of the Gaussian weighting used to
11
%                                 sum the gradient moments.
12
%             orientsmoothsigma - Sigma of the Gaussian used to smooth
13
%                                 the final orientation vector field. 
14
%                                 Optional: if omitted it defaults to 0
15
% 
16
% Returns:    orientim          - The orientation image in radians.
17
%                                 Orientation values are +ve clockwise
18
%                                 and give the direction *along* the
19
%                                 ridges.
20
%             reliability       - Measure of the reliability of the
21
%                                 orientation measure.  This is a value
22
%                                 between 0 and 1. I think a value above
23
%                                 about 0.5 can be considered 'reliable'.
24
%                                 reliability = 1 - Imin./(Imax+.001);
25
%             coherence         - A measure of the degree to which the local
26
%                                 area is oriented.
27
%                                 coherence = ((Imax-Imin)./(Imax+Imin)).^2;
28
%
29
% With a fingerprint image at a 'standard' resolution of 500dpi suggested
30
% parameter values might be:
31
%
32
%    [orientim, reliability] = ridgeorient(im, 1, 3, 3);
33
%
34
% See also: RIDGESEGMENT, RIDGEFREQ, RIDGEFILTER
35
36
% May 2003      Original version by Raymond Thai,  
37
% January 2005  Reworked by Peter Kovesi           
38
% October 2011  Added coherence computation and orientsmoothsigma
39
%               made optional
40
% April 2018    Change computation of gradients to use DERIAVTIVE5
41
%
42
% Peter Kovesi
43
% peterkovesi.com
44
45
46
function [orientim, reliability, coherence] = ...
47
             ridgeorient(im, gradientsigma, blocksigma, orientsmoothsigma)
48
        
49
    if ~exist('orientsmoothsigma', 'var'), orientsmoothsigma = 0; end
50
    [rows,cols] = size(im);
51
    
52
    % Calculate image gradients.
53
    [Gx, Gy] = derivative5(gaussfilt(im, gradientsigma), 'x', 'y');
54
    
55
    % Estimate the local ridge orientation at each point by finding the
56
    % principal axis of variation in the image gradients.
57
    Gxx = Gx.^2;       % Covariance data for the image gradients
58
    Gxy = Gx.*Gy;
59
    Gyy = Gy.^2;
60
    
61
    % Now smooth the covariance data to perform a weighted summation of the
62
    % data.
63
    sze = fix(6*blocksigma);   if ~mod(sze,2); sze = sze+1; end    
64
    f = fspecial('gaussian', sze, blocksigma);
65
    Gxx = filter2(f, Gxx); 
66
    Gxy = 2*filter2(f, Gxy);
67
    Gyy = filter2(f, Gyy);
68
    
69
    % Analytic solution of principal direction
70
    denom = sqrt(Gxy.^2 + (Gxx - Gyy).^2) + eps;
71
    sin2theta = Gxy./denom;            % Sine and cosine of doubled angles
72
    cos2theta = (Gxx-Gyy)./denom;
73
74
    if orientsmoothsigma
75
        sze = fix(6*orientsmoothsigma);   if ~mod(sze,2); sze = sze+1; end    
76
        f = fspecial('gaussian', sze, orientsmoothsigma);    
77
        cos2theta = filter2(f, cos2theta); % Smoothed sine and cosine of
78
        sin2theta = filter2(f, sin2theta); % doubled angles
79
    end
80
    
81
    orientim = pi/2 + atan2(sin2theta,cos2theta)/2;
82
    
83
    % Calculate 'reliability' of orientation data.  Here we calculate the area
84
    % moment about the orientation axis found (this will be the minimum moment)
85
    % and an axis perpendicular (which will be the maximum moment).  The
86
    % reliability measure is given by 1.0-min_moment/max_moment.  The reasoning
87
    % being that if the ratio of the minimum to maximum moments is close to one
88
    % we have little orientation information.
89
    
90
    Imin = (Gyy+Gxx)/2 - (Gxx-Gyy).*cos2theta/2 - Gxy.*sin2theta/2;
91
    Imax = Gyy+Gxx - Imin;
92
    
93
    reliability = 1 - Imin./(Imax+.001);
94
    coherence = ((Imax-Imin)./(Imax+Imin)).^2;
95
    
96
    % Finally mask reliability to exclude regions where the denominator
97
    % in the orientation calculation above was small.  Here I have set
98
    % the value to 0.001, adjust this if you feel the need
99
    reliability = reliability.*(denom>.001);