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

import numpy
5
import scipy
Pedro TOME's avatar
Pedro TOME committed
6
import scipy.misc
7

8 9 10
import bob.io.base
import bob.ip.base

11
from bob.bio.base.extractor import Extractor
12

Pedro TOME's avatar
Pedro TOME committed
13 14

class WideLineDetector (Extractor):
15 16 17 18 19 20
  """Wide Line Detector feature extractor

  Based on B. Huang, Y. Dai, R. Li, D. Tang and W. Li. Finger-vein
  authentication based on wide line detector and pattern normalization,
  Proceedings on 20th International Conference on Pattern Recognition (ICPR),
  2010.
Pedro TOME's avatar
Pedro TOME committed
21 22 23 24 25 26 27
  """

  def __init__(
      self,
      radius = 5,    #Radius of the circular neighbourhood region
      threshold = 1, #Neigborhood threshold
      g = 41,         #Sum of neigbourhood threshold
28 29
      rescale = True,
      ):
Pedro TOME's avatar
Pedro TOME committed
30 31 32 33 34 35 36

    # call base class constructor
    Extractor.__init__(
        self,
        radius = radius,
        threshold = threshold,
        g = g,
37 38 39
        rescale = rescale,
        )

Pedro TOME's avatar
Pedro TOME committed
40 41 42 43 44 45 46 47
    # block parameters
    self.radius = radius
    self.threshold = threshold
    self.g = g
    self.rescale = rescale


  def wide_line_detector(self, finger_image, mask):
48 49 50 51
    """Computes and returns the Wide Line Detector features for the given input
    fingervein image"""

    finger_image = finger_image.astype(numpy.float64)
Pedro TOME's avatar
Pedro TOME committed
52 53

    finger_mask = numpy.zeros(mask.shape)
54 55
    finger_mask[mask == True] = 1

Pedro TOME's avatar
Pedro TOME committed
56 57 58 59 60 61 62 63 64 65 66 67 68
    # Rescale image if required
    if self.rescale == True:
      scaling_factor = 0.24
      #finger_image = scipy.misc.imresize(finger_image,scaling_factor).astype()
      finger_image = bob.ip.base.scale(finger_image,scaling_factor)
      #finger_mask = scipy.misc.imresize(finger_mask,scaling_factor)
      finger_mask = bob.ip.base.scale(finger_mask,scaling_factor)
      #To eliminate residuals from the scalation of the binary mask
      finger_mask = scipy.ndimage.binary_dilation(finger_mask, structure=numpy.ones((1,1))).astype(int)

    x = numpy.arange((-1)*self.radius, self.radius+1)
    y = numpy.arange((-1)*self.radius, self.radius+1)
    X, Y = numpy.meshgrid(x,y)
69

Pedro TOME's avatar
Pedro TOME committed
70
    N = X**2 + Y**2 <= self.radius**2  # Neighbourhood mask
71 72 73

    img_h, img_w = finger_image.shape  #Image height and width

Pedro TOME's avatar
Pedro TOME committed
74
    veins = numpy.zeros(finger_image.shape)
75 76 77

    for y in range(self.radius,img_h-self.radius):
        for x in range(self.radius,img_w-self.radius):
Pedro TOME's avatar
Pedro TOME committed
78 79 80
            s=((finger_image[y-self.radius:y+self.radius+1,x-self.radius:x+self.radius+1] - finger_image[y,x]) <= self.threshold)
            m = (s*N).sum()
            veins[y,x] = float(m <= self.g)
81

Pedro TOME's avatar
Pedro TOME committed
82 83
    # Mask the vein image with the finger region
    img_veins_bin = veins*finger_mask
84

Pedro TOME's avatar
Pedro TOME committed
85
    return img_veins_bin
86 87 88 89 90 91 92


  def __call__(self, image):
    """Reads the input image, extract the features based on Wide Line Detector
    of the fingervein image, and writes the resulting template"""
    #For debugging

Pedro TOME's avatar
Pedro TOME committed
93
    finger_image = image[0]    #Normalized image with histogram equalization
94 95 96 97 98
    finger_mask = image[1]

    return self.wide_line_detector(finger_image, finger_mask)


Pedro TOME's avatar
Pedro TOME committed
99 100 101
  def save_feature(self, feature, feature_file):
    f = bob.io.base.HDF5File(feature_file, 'w')
    f.set('feature', feature)
102 103


Pedro TOME's avatar
Pedro TOME committed
104 105 106
  def read_feature(self, feature_file):
    f = bob.io.base.HDF5File(feature_file, 'r')
    image = f.read('feature')
107
    return (image)