Coverage for src/bob/bio/vein/algorithm/Correlate.py: 60%

25 statements  

« prev     ^ index     » next       coverage.py v7.6.0, created at 2024-07-12 23:27 +0200

1#!/usr/bin/env python 

2# vim: set fileencoding=utf-8 : 

3 

4import numpy 

5import skimage.feature 

6 

7from bob.bio.base.pipelines import BioAlgorithm 

8 

9 

10class Correlate(BioAlgorithm): 

11 """Correlate probe and model without cropping 

12 

13 The method is based on "cross-correlation" between a model and a probe image. 

14 The difference between this and :py:class:`MiuraMatch` is that **no** 

15 cropping takes place on this implementation. We simply fill the excess 

16 boundary with zeros and extract the valid correlation region between the 

17 probe and the model using :py:func:`skimage.feature.match_template`. 

18 

19 """ 

20 

21 def __init__( 

22 self, 

23 probes_score_fusion="max", 

24 enrolls_score_fusion="mean", 

25 **kwargs, 

26 ): 

27 super().__init__( 

28 probes_score_fusion=probes_score_fusion, 

29 enrolls_score_fusion=enrolls_score_fusion, 

30 **kwargs, 

31 ) 

32 

33 def create_templates(self, feature_sets, enroll): 

34 return feature_sets 

35 

36 def compare(self, enroll_templates, probe_templates): 

37 # returns scores NxM where N is the number of enroll templates and M is the number of probe templates 

38 # enroll_templates is Nx?1xD 

39 # probe_templates is Mx?2xD 

40 scores = [] 

41 for enroll in enroll_templates: 

42 scores.append([]) 

43 for probe in probe_templates: 

44 s = [[self.score(e, p) for p in probe] for e in enroll] 

45 s = self.fuse_probe_scores(s, axis=1) 

46 s = self.fuse_enroll_scores(s, axis=0) 

47 scores[-1].append(s) 

48 return numpy.array(scores) 

49 

50 def score(self, model, probe): 

51 """Computes the score between the probe and the model. 

52 

53 Parameters: 

54 

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

56 

57 probe (numpy.ndarray): The probe to test 

58 

59 

60 Returns: 

61 

62 float: Value between 0 and 0.5, larger value means a better match 

63 

64 """ 

65 

66 image_ = probe.astype(numpy.float64) 

67 

68 R = model.astype(numpy.float64) 

69 Nm = skimage.feature.match_template(image_, R) 

70 

71 # figures out where the maximum is on the resulting matrix 

72 t0, s0 = numpy.unravel_index(Nm.argmax(), Nm.shape) 

73 

74 # this is our output 

75 score = Nm[t0, s0] 

76 

77 return score