Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
bob.bio.vein
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
1
Issues
1
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
bob
bob.bio.vein
Commits
a63d2044
Commit
a63d2044
authored
Jun 30, 2017
by
André Anjos
💬
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Fixes to Maximum Curvature implementation; Improved references & documentation
parent
ee00ee5d
Changes
10
Hide whitespace changes
Inline
Sidebyside
Showing
10 changed files
with
526 additions
and
240 deletions
+526
240
bob/bio/vein/extractor/MaximumCurvature.py
bob/bio/vein/extractor/MaximumCurvature.py
+460
211
bob/bio/vein/tests/extractors/image.hdf5
bob/bio/vein/tests/extractors/image.hdf5
+0
0
bob/bio/vein/tests/extractors/mask.hdf5
bob/bio/vein/tests/extractors/mask.hdf5
+0
0
bob/bio/vein/tests/extractors/mc_bin_matlab.hdf5
bob/bio/vein/tests/extractors/mc_bin_matlab.hdf5
+0
0
bob/bio/vein/tests/extractors/mc_g_matlab.hdf5
bob/bio/vein/tests/extractors/mc_g_matlab.hdf5
+0
0
bob/bio/vein/tests/extractors/mc_vt_matlab.hdf5
bob/bio/vein/tests/extractors/mc_vt_matlab.hdf5
+0
0
bob/bio/vein/tests/extractors/miuramax_input_fvr.mat
bob/bio/vein/tests/extractors/miuramax_input_fvr.mat
+0
0
bob/bio/vein/tests/extractors/miuramax_input_img.mat
bob/bio/vein/tests/extractors/miuramax_input_img.mat
+0
0
bob/bio/vein/tests/extractors/miuramax_output.mat
bob/bio/vein/tests/extractors/miuramax_output.mat
+0
0
bob/bio/vein/tests/test.py
bob/bio/vein/tests/test.py
+66
29
No files found.
bob/bio/vein/extractor/MaximumCurvature.py
View file @
a63d2044
...
...
@@ 3,14 +3,10 @@
import
math
import
numpy
import
bob.core
import
scipy.ndimage
import
bob.io.base
from
bob.bio.base.extractor
import
Extractor
from
..
import
utils
class
MaximumCurvature
(
Extractor
):
"""
...
...
@@ 18,12 +14,15 @@ class MaximumCurvature (Extractor):
Based on N. Miura, A. Nagasaka, and T. Miyatake, Extraction of FingerVein
Pattern Using Maximum Curvature Points in Image Profiles. Proceedings on IAPR
conference on machine vision applications, 9 (2005), pp. 347350
conference on machine vision applications, 9 (2005), pp. 347350.
**Parameters:**
Parameters:
sigma (:py:class:`int`, optional): standard deviation for the gaussian
smoothing kernel used to denoise the input image. The width of the
gaussian kernel will be set automatically to 4x this value (in pixels).
sigma : :py:class:`int`
Optional: Sigma used for determining derivatives.
"""
...
...
@@ 32,227 +31,477 @@ class MaximumCurvature (Extractor):
self
.
sigma
=
sigma
def
maximum_curvature
(
self
,
image
,
mask
):
"""Computes and returns the Maximum Curvature features for the given input
fingervein image"""
def
detect_valleys
(
self
,
image
,
mask
):
"""Detects valleys on the image respecting the mask
This step corresponds to Step 11 in the original paper. The objective is,
for all 4 crosssections (z) of the image (horizontal, vertical, 45 and 45
diagonals), to compute the following proposed valley detector as defined in
Equation 1, page 348:
.. math::
\
k
a
ppa(z) =
\
\
frac{d^2P_f(z)/dz^2}{(1 + (dP_f(z)/dz)^2)^
\
\
frac{3}{2}}
We start the algorithm by smoothing the image with a 2dimensional gaussian
filter. The equation that defines the kernel for the filter is:
.. math::
\
m
a
thcal{N}(x,y)=
\
\
frac{1}{2
\
pi
\
sigma^2}e^
\
\
frac{(x^2+y^2)}{2
\
sigm
a
^2}
This is done to avoid noise from the raw data (from the sensor). The
maximum curvature method then requires we compute the first and second
derivative of the image for all crosssections, as per the equation above.
We instead take the following equivalent approach:
1. construct a gaussian filter
2. take the first (dh/dx) and second (d^2/dh^2) deritivatives of the filter
3. calculate the first and second derivatives of the smoothed signal using
the results from 3. This is done for all directions we're interested in:
horizontal, vertical and 2 diagonals. First and second derivatives of a
convolved signal
.. note::
Item 3 above is only possible thanks to the steerable filter property of
the gaussian kernel. See "The Design and Use of Steerable Filters" from
Freeman and Adelson, IEEE Transactions on Pattern Analysis and Machine
Intelligence, Vol. 13, No. 9, September 1991.
Parameters:
finger_mask
=
numpy
.
zeros
(
mask
.
shape
)
finger_mask
[
mask
==
True
]
=
1
image (numpy.ndarray): an array of 64bit floats containing the input
image
mask (numpy.ndarray): an array, of the same size as ``image``, containing
a mask (booleans) indicating where the finger is on ``image``.
winsize
=
numpy
.
ceil
(
4
*
self
.
sigma
)
x
=
numpy
.
arange
(

winsize
,
winsize
+
1
)
y
=
numpy
.
arange
(

winsize
,
winsize
+
1
)
X
,
Y
=
numpy
.
meshgrid
(
x
,
y
)
Returns:
h
=
(
1
/
(
2
*
math
.
pi
*
self
.
sigma
**
2
))
*
numpy
.
exp
(

(
X
**
2
+
Y
**
2
)
/
(
2
*
self
.
sigma
**
2
))
hx
=
(

X
/
(
self
.
sigma
**
2
))
*
h
hxx
=
((
X
**
2

self
.
sigma
**
2
)
/
(
self
.
sigma
**
4
))
*
h
hy
=
hx
.
T
hyy
=
hxx
.
T
hxy
=
((
X
*
Y
)
/
(
self
.
sigma
**
4
))
*
h
numpy.ndarray: a 3dimensional array of 64bits containing $
\
k
a
ppa$ for
all considered directions. $
\
k
a
ppa$ has the same shape as ``image``,
except for the 3rd. dimension, which provides planes for the
crosssection valley detections for each of the contemplated directions,
in this order: horizontal, vertical, +45 degrees, 45 degrees.
# Do the actual filtering
"""
fx
=
utils
.
imfilter
(
image
,
hx
)
fxx
=
utils
.
imfilter
(
image
,
hxx
)
fy
=
utils
.
imfilter
(
image
,
hy
)
fyy
=
utils
.
imfilter
(
image
,
hyy
)
fxy
=
utils
.
imfilter
(
image
,
hxy
)
# 1. constructs the 2D gaussian filter "h" given the window size,
# extrapolated from the "sigma" parameter (4x)
# N.B.: This is a textbook gaussian filter definition
winsize
=
numpy
.
ceil
(
4
*
self
.
sigma
)
#enough space for the filter
window
=
numpy
.
arange
(

winsize
,
winsize
+
1
)
X
,
Y
=
numpy
.
meshgrid
(
window
,
window
)
G
=
1.0
/
(
2
*
math
.
pi
*
self
.
sigma
**
2
)
G
*=
numpy
.
exp
(

(
X
**
2
+
Y
**
2
)
/
(
2
*
self
.
sigma
**
2
))
f1
=
0.5
*
numpy
.
sqrt
(
2
)
*
(
fx
+
fy
)
# \ #
f2
=
0.5
*
numpy
.
sqrt
(
2
)
*
(
fx

fy
)
# / #
f11
=
0.5
*
fxx
+
fxy
+
0.5
*
fyy
# \\ #
f22
=
0.5
*
fxx

fxy
+
0.5
*
fyy
# // #
# 2. calculates first and second derivatives of "G" with respect to "X"
# (0), "Y" (90 degrees) and 45 degrees (?)
G1_0
=
(

X
/
(
self
.
sigma
**
2
))
*
G
G2_0
=
((
X
**
2

self
.
sigma
**
2
)
/
(
self
.
sigma
**
4
))
*
G
G1_90
=
G1_0
.
T
G2_90
=
G2_0
.
T
hxy
=
((
X
*
Y
)
/
(
self
.
sigma
**
4
))
*
G
# 3. calculates derivatives w.r.t. to all directions of interest
# stores results in the variable "k". The entries (last dimension) in k
# correspond to curvature detectors in the following directions:
#
# [0] horizontal
# [1] vertical
# [2] diagonal \ (45 degrees rotation)
# [3] diagonal / (45 degrees rotation)
image_g1_0
=
scipy
.
ndimage
.
convolve
(
image
,
G1_0
,
mode
=
'nearest'
)
image_g2_0
=
scipy
.
ndimage
.
convolve
(
image
,
G2_0
,
mode
=
'nearest'
)
image_g1_90
=
scipy
.
ndimage
.
convolve
(
image
,
G1_90
,
mode
=
'nearest'
)
image_g2_90
=
scipy
.
ndimage
.
convolve
(
image
,
G2_90
,
mode
=
'nearest'
)
fxy
=
scipy
.
ndimage
.
convolve
(
image
,
hxy
,
mode
=
'nearest'
)
# support calculation for diagonals, given the gaussian kernel is
# steerable. To calculate the derivatives for the "\" diagonal, we first
# **would** have to rotate the image 45 degrees counterclockwise (so the
# diagonal lies on the horizontal axis). Using the steerable property, we
# can evaluate the first derivative like this:
#
# image_g1_45 = cos(45)*image_g1_0 + sin(45)*image_g1_90
# = sqrt(2)/2*fx + sqrt(2)/2*fx
#
# to calculate the first derivative for the "/" diagonal, we first
# **would** have to rotate the image 45 degrees "counter"clockwise.
# Therefore, we can calculate it like this:
#
# image_g1_m45 = cos(45)*image_g1_0 + sin(45)*image_g1_90
# = sqrt(2)/2*image_g1_0  sqrt(2)/2*image_g1_90
#
image_g1_45
=
0.5
*
numpy
.
sqrt
(
2
)
*
(
image_g1_0
+
image_g1_90
)
image_g1_m45
=
0.5
*
numpy
.
sqrt
(
2
)
*
(
image_g1_0

image_g1_90
)
# NOTE: You can't really get image_g2_45 and image_g2_m45 from the theory
# of steerable filters. In contact with B.Ton, he suggested the following
# material, where that is explained: Chapter 5.2.3 of van der Heijden, F.
# (1994) Image based measurement systems: object recognition and parameter
# estimation. John Wiley & Sons Ltd, Chichester. ISBN 9780471950622
# This also shows the same result:
# http://www.mif.vu.lt/atpazinimas/dip/FIP/fipDerivati.html (look for
# SDGD)
# He also suggested to look at slide 75 of the following presentation
# indicating it is selfexplanatory: http://slideplayer.com/slide/5084635/
image_g2_45
=
0.5
*
image_g2_0
+
fxy
+
0.5
*
image_g2_90
image_g2_m45
=
0.5
*
image_g2_0

fxy
+
0.5
*
image_g2_90
img_h
,
img_w
=
image
.
shape
#Image height and width
# Calculate curvatures
k
=
numpy
.
zeros
((
img_h
,
img_w
,
4
))
k
[:,:,
0
]
=
(
fxx
/
((
1
+
fx
**
2
)
**
(
3
/
2
)))
*
finger_mask
# hor #
k
[:,:,
1
]
=
(
fyy
/
((
1
+
fy
**
2
)
**
(
3
/
2
)))
*
finger_mask
# ver #
k
[:,:,
2
]
=
(
f11
/
((
1
+
f1
**
2
)
**
(
3
/
2
)))
*
finger_mask
# \ #
k
[:,:,
3
]
=
(
f22
/
((
1
+
f2
**
2
)
**
(
3
/
2
)))
*
finger_mask
# / #
# ######################################################################
# [Step 11] Calculation of curvature profiles
# ######################################################################
# Peak detection (k or kappa) calculation as per equation (1) page 348 on
# Miura's paper
finger_mask
=
mask
.
astype
(
'float64'
)
return
numpy
.
dstack
([
(
image_g2_0
/
((
1
+
image_g1_0
**
2
)
**
(
1.5
))
)
*
finger_mask
,
(
image_g2_90
/
((
1
+
image_g1_90
**
2
)
**
(
1.5
))
)
*
finger_mask
,
(
image_g2_45
/
((
1
+
image_g1_45
**
2
)
**
(
1.5
))
)
*
finger_mask
,
(
image_g2_m45
/
((
1
+
image_g1_m45
**
2
)
**
(
1.5
)))
*
finger_mask
,
])
def
eval_vein_probabilities
(
self
,
k
):
'''Evaluates joint vein centre probabilities from crosssections
This function will take $
\
k
a
ppa$ and will calculate the vein centre
probabilities taking into consideration valley widths and depths. It
aggregates the following steps from the paper:
* [Step 12] Detection of the centres of veins
* [Step 13] Assignment of scores to the centre positions
* [Step 14] Calculation of all the profiles
Once the arrays of curvatures (concavities) are calculated, here is how
detection works: The code scans the image in a precise direction (vertical,
horizontal, diagonal, etc). It tries to find a concavity on that direction
and measure its width (see Wr on Figure 3 on the original paper). It then
identifies the centers of the concavity and assign a value to it, which
depends on its width (Wr) and maximum depth (where the peak of darkness
occurs) in such a concavity. This value is accumulated on a variable (Vt),
which is reused for all directions. Vt represents the vein probabilites
from the paper.
Parameters:
k (numpy.ndarray): a 3dimensional array of 64bits containing $
\
k
a
ppa$
for all considered directions. $
\
k
a
ppa$ has the same shape as
``image``, except for the 3rd. dimension, which provides planes for the
crosssection valley detections for each of the contemplated
directions, in this order: horizontal, vertical, +45 degrees, 45
degrees.
Returns:
numpy.ndarray: The unaccumulated vein centre probabilities ``V``. This
is a 3D array with 64bit floats with the same dimensions of the input
array ``k``. You must accumulate (sum) over the last dimension to
retrieve the variable ``V`` from the paper.
'''
V
=
numpy
.
zeros_like
(
k
)
def
_prob_1d
(
a
):
'''Finds "vein probabilities" in a 1D signal
This function efficiently counts the width and height of concavities in
the crosssection (1D) curvature signal ``s``.
It works like this:
1. We create a 1shift difference between the thresholded signal and
itself
2. We compensate for starting and ending regions
3. For each sequence of start/ends, we compute the maximum in the
original signal
Example (mixed with pseudocode):
a = 0 1 2 3 2 1 0 1 0 0 1 2 5 2 2 2 1
b = a > 0 (as type int)
b = 0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1
0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1
0 1 1 1 1 1 0 0 0 0 1 1 1 1 1 1 1 ()

X 1 0 0 0 0 1 0 0 0 1 0 0 0 0 0 0 X (length is smaller than orig.)
starts = numpy.where(diff > 0)
ends = numpy.where(diff < 0)
> now the number of starts and ends should match, otherwise, we must
compensate
> case 1: b starts with 1: add one start in begin of "starts"
> case 2: b ends with 1: add one end in the end of "ends"
> iterate over the sequence of starts/ends and find maximums
Parameters:
a (numpy.ndarray): 1D signal with curvature to explore
Returns:
numpy.ndarray: 1D container with the vein centre probabilities
'''
b
=
(
a
>
0
).
astype
(
int
)
diff
=
b
[
1
:]

b
[:

1
]
starts
=
numpy
.
argwhere
(
diff
>
0
)
starts
+=
1
#compensates for shifted different
ends
=
numpy
.
argwhere
(
diff
<
0
)
ends
+=
1
#compensates for shifted different
if
b
[
0
]:
starts
=
numpy
.
insert
(
starts
,
0
,
0
)
if
b
[

1
]:
ends
=
numpy
.
append
(
ends
,
len
(
a
))
z
=
numpy
.
zeros_like
(
a
)
if
starts
.
size
==
0
and
ends
.
size
==
0
:
return
z
for
start
,
end
in
zip
(
starts
,
ends
):
maximum
=
numpy
.
argmax
(
a
[
int
(
start
):
int
(
end
)])
z
[
start
+
maximum
]
=
a
[
start
+
maximum
]
*
(
end

start
)
return
z
# Scores
Vt
=
numpy
.
zeros
(
image
.
shape
)
Wr
=
0
# Horizontal direction
bla
=
k
[:,:,
0
]
>
0
for
y
in
range
(
0
,
img_h
):
for
x
in
range
(
0
,
img_w
):
if
(
bla
[
y
,
x
]):
Wr
=
Wr
+
1
if
(
Wr
>
0
and
(
x
==
(
img_w

1
)
or
not
bla
[
y
,
x
])
):
if
(
x
==
(
img_w

1
)):
# Reached edge of image
pos_end
=
x
else
:
pos_end
=
x

1
pos_start
=
pos_end

Wr
+
1
# Start pos of concave
if
(
pos_start
==
pos_end
):
I
=
numpy
.
argmax
(
k
[
y
,
pos_start
,
0
])
else
:
I
=
numpy
.
argmax
(
k
[
y
,
pos_start
:
pos_end
+
1
,
0
])
pos_max
=
pos_start
+
I
Scr
=
k
[
y
,
pos_max
,
0
]
*
Wr
Vt
[
y
,
pos_max
]
=
Vt
[
y
,
pos_max
]
+
Scr
Wr
=
0
for
index
in
range
(
k
.
shape
[
0
]):
V
[
index
,:,
0
]
+=
_prob_1d
(
k
[
index
,:,
0
])
# Vertical direction
for
index
in
range
(
k
.
shape
[
1
]):
V
[:,
index
,
1
]
+=
_prob_1d
(
k
[:,
index
,
1
])
# Direction: 45 degrees (\)
curv
=
k
[:,:,
2
]
i
,
j
=
numpy
.
indices
(
curv
.
shape
)
for
index
in
range
(

curv
.
shape
[
0
]
+
1
,
curv
.
shape
[
1
]):
V
[
i
==
(
j

index
),
2
]
+=
_prob_1d
(
curv
.
diagonal
(
index
))
# Direction: 45 degrees (/)
# NOTE: due to the way the access to the diagonals are implemented, in this
# loop, we operate bottomup. To match this behaviour, we also address V
# through Vud.
curv
=
numpy
.
flipud
(
k
[:,:,
3
])
#required so we get "/" diagonals correctly
Vud
=
numpy
.
flipud
(
V
)
#match above inversion
for
index
in
reversed
(
range
(
curv
.
shape
[
1
]

1
,

curv
.
shape
[
0
],

1
)):
Vud
[
i
==
(
j

index
),
3
]
+=
_prob_1d
(
curv
.
diagonal
(
index
))
return
V
def
connect_centres
(
self
,
V
):
"""Connects vein centres by filtering vein probabilities ``V``
This function does the equivalent of Step 2 / Equation 4 at Miura's paper.
The operation is applied on a row from the ``V`` matrix, which may be
acquired horizontally, vertically or on a diagonal direction. The pixel
value is then reset in the center of a windowing operation (width = 5) with
the following value:
.. math::
b[w] = min(max(a[w+1], a[w+2]) + max(a[w1], a[w2]))
Parameters:
V (numpy.ndarray): The accumulated vein centre probabilities ``V``. This
is a 2D array with 64bit floats and is defined by Equation (3) on the
paper.
Returns:
numpy.ndarray: A 3dimensional 64bit array ``Cd`` containing the result
of the filtering operation for each of the directions. ``Cd`` has the
dimensions of $
\
k
a
ppa$ and $V_i$. Each of the planes correspond to the
horizontal, vertical, +45 and 45 directions.
"""
def
_connect_1d
(
a
):
'''Connects centres in the given vector
The strategy we use to vectorize this is to shift a twice to the left and
twice to the right and apply a vectorized operation to compute the above.
Parameters:
a (numpy.ndarray): Input 1D array which will be window scanned
Returns:
numpy.ndarray: Output 1D array (must be writeable), in which we will
set the corrected pixel values after the filtering above. Notice that,
given the windowing operation, the returned array size would be 4 short
of the input array.
'''
return
numpy
.
amin
([
numpy
.
amax
([
a
[
3
:

1
],
a
[
4
:]],
axis
=
0
),
numpy
.
amax
([
a
[
1
:

3
],
a
[:

4
]],
axis
=
0
)],
axis
=
0
)
Cd
=
numpy
.
zeros
(
V
.
shape
+
(
4
,),
dtype
=
'float64'
)
# Horizontal direction
for
index
in
range
(
V
.
shape
[
0
]):
Cd
[
index
,
2
:

2
,
0
]
=
_connect_1d
(
V
[
index
,:])
# Vertical direction
bla
=
k
[:,:,
1
]
>
0
for
x
in
range
(
0
,
img_w
):
for
y
in
range
(
0
,
img_h
):
if
(
bla
[
y
,
x
]):
Wr
=
Wr
+
1
if
(
Wr
>
0
and
(
y
==
(
img_h

1
)
or
not
bla
[
y
,
x
])
):
if
(
y
==
(
img_h

1
)):
# Reached edge of image
pos_end
=
y
else
:
pos_end
=
y

1
pos_start
=
pos_end

Wr
+
1
# Start pos of concave
if
(
pos_start
==
pos_end
):
I
=
numpy
.
argmax
(
k
[
pos_start
,
x
,
1
])
else
:
I
=
numpy
.
argmax
(
k
[
pos_start
:
pos_end
+
1
,
x
,
1
])
pos_max
=
pos_start
+
I
Scr
=
k
[
pos_max
,
x
,
1
]
*
Wr
Vt
[
pos_max
,
x
]
=
Vt
[
pos_max
,
x
]
+
Scr
Wr
=
0
# Direction: \ #
bla
=
k
[:,:,
2
]
>
0
for
start
in
range
(
0
,
img_w
+
img_h

1
):
# Initial values
if
(
start
<=
img_w

1
):
x
=
start
y
=
0
else
:
x
=
0
y
=
start

img_w
+
1
done
=
False
while
(
not
done
):
if
(
bla
[
y
,
x
]):
Wr
=
Wr
+
1
if
(
Wr
>
0
and
(
y
==
img_h

1
or
x
==
img_w

1
or
not
bla
[
y
,
x
])
):
if
(
y
==
img_h

1
or
x
==
img_w

1
):
# Reached edge of image
pos_x_end
=
x
pos_y_end
=
y
else
:
pos_x_end
=
x

1
pos_y_end
=
y

1
pos_x_start
=
pos_x_end

Wr
+
1
pos_y_start
=
pos_y_end

Wr
+
1
if
(
pos_y_start
==
pos_y_end
and
pos_x_start
==
pos_x_end
):
d
=
k
[
pos_y_start
,
pos_x_start
,
2
]
elif
(
pos_y_start
==
pos_y_end
):
d
=
numpy
.
diag
(
k
[
pos_y_start
,
pos_x_start
:
pos_x_end
+
1
,
2
])
elif
(
pos_x_start
==
pos_x_end
):
d
=
numpy
.
diag
(
k
[
pos_y_start
:
pos_y_end
+
1
,
pos_x_start
,
2
])
else
:
d
=
numpy
.
diag
(
k
[
pos_y_start
:
pos_y_end
+
1
,
pos_x_start
:
pos_x_end
+
1
,
2
])
I
=
numpy
.
argmax
(
d
)
pos_x_max
=
pos_x_start
+
I
pos_y_max
=
pos_y_start
+
I
Scr
=
k
[
pos_y_max
,
pos_x_max
,
2
]
*
Wr
Vt
[
pos_y_max
,
pos_x_max
]
=
Vt
[
pos_y_max
,
pos_x_max
]
+
Scr
Wr
=
0
if
((
x
==
img_w

1
)
or
(
y
==
img_h

1
)):
done
=
True
else
:
x
=
x
+
1
y
=
y
+
1
# Direction: /
bla
=
k
[:,:,
3
]
>
0
for
start
in
range
(
0
,
img_w
+
img_h

1
):
# Initial values
if
(
start
<=
(
img_w

1
)):
x
=
start
y
=
img_h

1
else
:
x
=
0
y
=
img_w
+
img_h

start

1
done
=
False
while
(
not
done
):
if
(
bla
[
y
,
x
]):
Wr
=
Wr
+
1
if
(
Wr
>
0
and
(
y
==
0
or
x
==
img_w

1
or
not
bla
[
y
,
x
])
):
if
(
y
==
0
or
x
==
img_w

1
):
# Reached edge of image
pos_x_end
=
x
pos_y_end
=
y
else
:
pos_x_end
=
x

1
pos_y_end
=
y
+
1
pos_x_start
=
pos_x_end

Wr
+
1
pos_y_start
=
pos_y_end
+
Wr

1
if
(
pos_y_start
==
pos_y_end
and
pos_x_start
==
pos_x_end
):
d
=
k
[
pos_y_end
,
pos_x_start
,
3
]
elif
(
pos_y_start
==
pos_y_end
):
d
=
numpy
.
diag
(
numpy
.
flipud
(
k
[
pos_y_end
,
pos_x_start
:
pos_x_end
+
1
,
3
]))
elif
(
pos_x_start
==
pos_x_end
):
d
=
numpy
.
diag
(
numpy
.
flipud
(
k
[
pos_y_end
:
pos_y_start
+
1
,
pos_x_start
,
3
]))
else
:
d
=
numpy
.
diag
(
numpy
.
flipud
(
k
[
pos_y_end
:
pos_y_start
+
1
,
pos_x_start
:
pos_x_end
+
1
,
3
]))
I
=
numpy
.
argmax
(
d
)
pos_x_max
=
pos_x_start
+
I
pos_y_max
=
pos_y_start

I
Scr
=
k
[
pos_y_max
,
pos_x_max
,
3
]
*
Wr
Vt
[
pos_y_max
,
pos_x_max
]
=
Vt
[
pos_y_max
,
pos_x_max
]
+
Scr
Wr
=
0
if
((
x
==
img_w

1
)
or
(
y
==
0
)):
done
=
True
else
:
x
=
x
+
1
y
=
y

1
## Connection of vein centres
Cd
=
numpy
.
zeros
((
img_h
,
img_w
,
4
))
for
x
in
range
(
2
,
img_w

3
):
for
y
in
range
(
2
,
img_h

3
):
Cd
[
y
,
x
,
0
]
=
min
(
numpy
.
amax
(
Vt
[
y
,
x
+
1
:
x
+
3
]),
numpy
.
amax
(
Vt
[
y
,
x

2
:
x
]))
# Hor #
Cd
[
y
,
x
,
1
]
=
min
(
numpy
.
amax
(
Vt
[
y
+
1
:
y
+
3
,
x
]),
numpy
.
amax
(
Vt
[
y

2
:
y
,
x
]))
# Vert #
Cd
[
y
,
x
,
2
]
=
min
(
numpy
.
amax
(
Vt
[
y

2
:
y
,
x

2
:
x
]),
numpy
.
amax
(
Vt
[
y
+
1
:
y
+
3
,
x
+
1
:
x
+
3
]))
# \ #
Cd
[
y
,
x
,
3
]
=
min
(
numpy
.
amax
(
Vt
[
y
+
1
:
y
+
3
,
x

2
:
x
]),
numpy
.
amax
(
Vt
[
y

2
:
y
,
x
+
1
:
x
+
3
]))
# / #
#Veins
img_veins
=
numpy
.
amax
(
Cd
,
axis
=
2
)
# Binarise the vein image
md
=
numpy
.
median
(
img_veins
[
img_veins
>
0
])
img_veins_bin
=
img_veins
>
md
return
img_veins_bin
.
astype
(
numpy
.
float64
)
for
index
in
range
(
V
.
shape
[
1
]):
Cd
[
2
:

2
,
index
,
1
]
=
_connect_1d
(
V
[:,
index
])
# Direction: 45 degrees (\)
i
,
j
=
numpy
.
indices
(
V
.
shape
)
border
=
numpy
.
zeros
((
2
,),
dtype
=
'float64'
)
for
index
in
range
(

V
.
shape
[
0
]
+
5
,
V
.
shape
[
1
]

4
):
# NOTE: hstack **absolutately** necessary here as double indexing after
# array indexing is **not** possible with numpy (it returns a copy)
Cd
[:,:,
2
][
i
==
(
j

index
)]
=
numpy
.
hstack
([
border
,
_connect_1d
(
V
.
diagonal
(
index
)),
border
])
# Direction: 45 degrees (/)
Vud
=
numpy
.
flipud
(
V
)
Cdud
=
numpy
.
flipud
(
Cd
[:,:,
3
])
for
index
in
reversed
(
range
(
V
.
shape
[
1
]

5
,

V
.
shape
[
0
]
+
4
,

1
)):
# NOTE: hstack **absolutately** necessary here as double indexing after
# array indexing is **not** possible with numpy (it returns a copy)
Cdud
[:,:][
i
==
(
j

index
)]
=
numpy
.
hstack
([
border
,
_connect_1d
(
Vud
.
diagonal
(
index
)),
border
])
return
Cd
def
binarise
(
self
,
G
):
"""Binarise vein images using a threshold assuming distribution is diphasic
This function implements Step 3 of the paper. It binarises the 2D array
``G`` assuming its histogram is mostly diphasic and using a median value.
Parameters:
G (numpy.ndarray): A 2dimensional 64bit array ``G`` containing the
result of the filtering operation. ``G`` has the dimensions of the
original image.
Returns:
numpy.ndarray: A 2dimensional 64bit float array with the same
dimensions of the input image, but containing its veinbinarised version.
The output of this function corresponds to the output of the method.
"""
median
=
numpy
.
median
(
G
[
G
>
0
])
Gbool
=
G
>
median
return
Gbool
.
astype
(
numpy
.
float64
)
def
_view_four
(
self
,
k
,
suptitle
):
'''Display four plots using matplotlib'''
import
matplotlib.pyplot
as
plt
k
[
k
<=
0
]
=
0
k
/=
k
.
max
()
plt
.
subplot
(
2
,
2
,
1
)
plt
.
imshow
(
k
[...,
0
],
cmap
=
'gray'
)
plt
.
title
(
'Horizontal'
)
plt
.
subplot
(
2
,
2
,
2
)
plt
.
imshow
(
k
[...,
1
],
cmap
=
'gray'
)
plt
.
title
(
'Vertical'
)
plt
.
subplot
(
2
,
2
,
3
)
plt
.
imshow
(
k
[...,
2
],
cmap
=
'gray'
)
plt
.
title
(
'+45 degrees'
)
plt
.
subplot
(
2
,
2
,
4
)
plt
.
imshow
(
k
[...,
3
],
cmap
=
'gray'
)
plt
.
title
(
'45 degrees'
)
plt
.
suptitle
(
suptitle
)
plt
.
tight_layout
()
plt
.
show
()
def
_view_single
(
self
,
k
,
title
):
'''Displays a single plot using matplotlib'''
import
matplotlib.pyplot
as
plt
plt
.
imshow
(
k
,
cmap
=
'gray'
)
plt
.
title
(
title
)
plt
.
tight_layout
()
plt
.
show
()
def
__call__
(
self
,
image
):
"""Reads the input image, extract the features based on Maximum Curvature of the fingervein image, and writes the resulting template"""
finger_image
=
image
[
0
]
#Normalized image with or without histogram equalization
finger_image
=
image
[
0
]
finger_mask
=
image
[
1
]
return
self
.
maximum_curvature
(
finger_image
,
finger_mask
)
import
time
start
=
time
.
time
()
kappa
=
self
.
detect_valleys
(
finger_image
,
finger_mask
)
#self._view_four(kappa, "Valley Detectors  $\kappa$")