Coverage for src/bob/bio/vein/preprocessor/preprocessor.py: 77%
22 statements
« prev ^ index » next coverage.py v7.6.0, created at 2024-07-12 23:27 +0200
« 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 :
3import h5py
5from bob.bio.base.preprocessor import Preprocessor as BasePreprocessor
8class Preprocessor(BasePreprocessor):
9 """
10 Extracts the mask and pre-processes fingervein images.
12 In this implementation, the finger image is (in this order):
14 #. The image is pre-cropped to remove obvious non-finger image parts
15 #. The mask is extrapolated from the image using one of our
16 :py:class:`Masker`'s concrete implementations
17 #. The image is normalized with one of our :py:class:`Normalizer`'s
18 #. The image is filtered with one of our :py:class:`Filter`'s
21 Parameters:
23 crop (:py:class:`Cropper`): An object that will perform pre-cropping on
24 the input image before a mask can be estimated. It removes parts of the
25 image which are surely not part of the finger region you'll want to
26 consider for the next steps.
28 mask (:py:class:`Masker`): An object representing a Masker instance which
29 will extrapolate the mask from the input image.
31 normalize (:py:class:`Normalizer`): An object representing a Normalizer
32 instance which will normalize the input image and its mask returning a
33 new image mask pair.
35 filter (:py:class:`Filter`): An object representing a Filter instance will
36 will filter the input image and return a new filtered image. The filter
37 instance also receives the extrapolated mask so it can, if desired, only
38 apply the filtering operation where the mask has a value of ``True``
40 """
42 def __init__(self, crop, mask, normalize, filter, **kwargs):
43 BasePreprocessor.__init__(
44 self,
45 crop=crop,
46 mask=mask,
47 normalize=normalize,
48 filter=filter,
49 **kwargs,
50 )
52 self.crop = crop
53 self.mask = mask
54 self.normalize = normalize
55 self.filter = filter
57 def __call__(self, data, annotations=None):
58 """Reads the input image or (image, mask) and prepares for fex.
60 Parameters:
62 data (numpy.ndarray): An 2D numpy array containing a gray-scaled image
63 with dtype ``uint8``. The image maybe annotated with an RoI.
66 Returns:
68 numpy.ndarray: The image, preprocessed and normalized
70 numpy.ndarray: A mask, of the same size of the image, indicating where
71 the valid data for the object is.
73 """
75 data = self.crop(data)
76 mask = self.mask(data)
77 data, mask = self.normalize(data, mask)
78 data = self.filter(data, mask)
79 return data, mask
81 def write_data(self, data, filename):
82 """Overrides the default method implementation to handle our tuple"""
84 with h5py.File(filename, "w") as f:
85 f["image"] = data[0]
86 f["mask"] = data[1]
88 def read_data(self, filename):
89 """Overrides the default method implementation to handle our tuple"""
90 with h5py.File(filename, "r") as f:
91 return f["image"][()], f["mask"][()]