MiuraMatch.py 4.37 KB
Newer Older
Pedro TOME's avatar
Pedro TOME committed
1 2 3 4 5 6
#!/usr/bin/env python
# vim: set fileencoding=utf-8 :

import numpy
import scipy.signal

7
from bob.bio.base.algorithm import Algorithm
Pedro TOME's avatar
Pedro TOME committed
8

9 10

class MiuraMatch (Algorithm):
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
  """Finger vein matching: match ratio via cross-correlation

  The method is based on "cross-correlation" between a model and a probe image.
  It convolves the binary image(s) representing the model with the binary image
  representing the probe (rotated by 180 degrees), to evaluate how they
  cross-correlate. If the model and probe are very similar, the output of the
  correlation corresponds to a single scalar and approaches a maximum.  The
  value is then normalized by the sum of the pixels lit in both binary images.
  Therefore, the output of this method is a floating-point number in the range
  :math:`[0, 0.5]`. The higher, the better match.

  In case model and probe represent images from the same vein structure, but
  are misaligned, the output is not guaranteed to be accurate. To mitigate this
  aspect, Miura et al. proposed to add a *small** erosion factor to the model
  image, assuming not much information is available on the borders (``ch``, for
  the vertical direction and ``cw``, for the horizontal direction). This allows
  the convolution to yield searches for different areas in the probe image. The
  maximum value is then taken from the resulting operation. The convolution
  result is normalized by the pixels lit in both the eroded model image and the
  matching pixels on the probe that yield the maximum on the resulting
  convolution.
32 33 34 35 36

  Based on N. Miura, A. Nagasaka, and T. Miyatake. Feature extraction of finger
  vein patterns based on repeated line tracking and its application to personal
  identification. Machine Vision and Applications, Vol. 15, Num. 4, pp.
  194--203, 2004
37

André Anjos's avatar
André Anjos committed
38
  Parameters:
39

40
    ch (:py:class:`int`, optional): Maximum search displacement in y-direction.
André Anjos's avatar
André Anjos committed
41

42
    cw (:py:class:`int`, optional): Maximum search displacement in x-direction.
43

Pedro TOME's avatar
Pedro TOME committed
44 45
  """

46
  def __init__(self,
Pedro TOME's avatar
Pedro TOME committed
47 48
      ch = 8,       # Maximum search displacement in y-direction
      cw = 5,       # Maximum search displacement in x-direction
49
      ):
Pedro TOME's avatar
Pedro TOME committed
50 51

    # call base class constructor
52
    Algorithm.__init__(
Pedro TOME's avatar
Pedro TOME committed
53 54 55 56 57 58 59 60 61 62 63 64
        self,

        ch = ch,
        cw = cw,

        multiple_model_scoring = None,
        multiple_probe_scoring = None
    )

    self.ch = ch
    self.cw = cw

65

Pedro TOME's avatar
Pedro TOME committed
66 67
  def enroll(self, enroll_features):
    """Enrolls the model by computing an average graph for each model"""
68

Pedro TOME's avatar
Pedro TOME committed
69 70 71 72
    # return the generated model
    return numpy.array(enroll_features)


73 74
  def score(self, model, probe):
    """Computes the score between the probe and the model.
75

76
    Parameters:
77

78
      model (numpy.ndarray): The model of the user to test the probe agains
79

80
      probe (numpy.ndarray): The probe to test
Pedro TOME's avatar
Pedro TOME committed
81 82


83
    Returns:
Olegs NIKISINS's avatar
Olegs NIKISINS committed
84

85
      score (float): Value between 0 and 0.5, larger value means a better match
Olegs NIKISINS's avatar
Olegs NIKISINS committed
86

Pedro TOME's avatar
Pedro TOME committed
87
    """
88

Pedro TOME's avatar
Pedro TOME committed
89
    I=probe.astype(numpy.float64)
90 91

    if len(model.shape) == 2:
Pedro TOME's avatar
Pedro TOME committed
92
      model = numpy.array([model])
93

Pedro TOME's avatar
Pedro TOME committed
94 95 96
    n_models = model.shape[0]

    scores = []
97

98 99
    # iterate over all models for a given individual
    for md in model:
100 101

      # erode model by (ch, cw)
102
      R = md.astype(numpy.float64)
Pedro TOME's avatar
Pedro TOME committed
103 104
      h, w = R.shape
      crop_R = R[self.ch:h-self.ch, self.cw:w-self.cw]
105

106 107 108 109 110 111 112 113 114 115 116
      # correlates using scipy - fastest option available iff the self.ch and
      # self.cw are height (>30). In this case, the number of components
      # returned by the convolution is high and using an FFT-based method
      # yields best results. Otherwise, you may try  the other options bellow
      # -> check our test_correlation() method on the test units for more
      # details and benchmarks.
      Nm = scipy.signal.fftconvolve(I, numpy.rot90(crop_R, k=2), 'valid')
      # 2nd best: use convolve2d or correlate2d directly;
      # Nm = scipy.signal.convolve2d(I, numpy.rot90(crop_R, k=2), 'valid')
      # 3rd best: use correlate2d
      # Nm = scipy.signal.correlate2d(I, crop_R, 'valid')
117 118

      # figures out where the maximum is on the resulting matrix
Pedro TOME's avatar
Pedro TOME committed
119
      t0, s0 = numpy.unravel_index(Nm.argmax(), Nm.shape)
120 121

      # this is our output
Pedro TOME's avatar
Pedro TOME committed
122
      Nmm = Nm[t0,s0]
123 124 125 126

      # normalizes the output by the number of pixels lit on the input
      # matrices, taking into consideration the surface that produced the
      # result (i.e., the eroded model and part of the probe)
Pedro TOME's avatar
Pedro TOME committed
127
      scores.append(Nmm/(sum(sum(crop_R)) + sum(sum(I[t0:t0+h-2*self.ch, s0:s0+w-2*self.cw]))))
128

Pedro TOME's avatar
Pedro TOME committed
129
    return numpy.mean(scores)