Skip to main content

image processing - How to patch fingerprint ridges


I get a fingerprint:



enter image description here


But as you see,there are some place is discontinuous,such as:


enter image description here


The question is how to patch the discontinuous line according its gradient?The only one thing that I can think it may be helpful is use Closing or Dilationin transverse orient,but the effect is very bad


enter image description here enter image description here


Could anybody can give some advice for this?



Answer



This might be difficult to do for a human, as it would require very strict definitions of a gap. I will present a method to get lines which will come very close to representing the gaps.


First we'll set up a table of all of the white pixels


a = [IMAGE]

b = ImageValuePositions[a, White];

b is just:


ListPlot[b]

finger


Now we want to calculate each points distance from every other point:


findDist[p1_, p2_] := EuclideanDistance[p1, p2];
AbsoluteTiming[
dists = Table[{i, j, findDist[b[[i]], b[[j]]] },

{i, 1, Length[b]}, {j, 1, Length[b] }]]

(*63s on an i5 4570*)

Now we'll filter (or cluster) for pixels close to other pixels, then dump these results generate a graph:


s1 = Select[Flatten[dists, 1], #[[3]] > 0 && #[[3]] < 3 &];
sa = SparseArray[# -> 1 & /@ s1[[All, {1, 2}]]];
adj = AdjacencyGraph[sa, DirectedEdges -> True];

adj:



adjacency graph


Each one of these 'fingers' represents a string(line/curve) of pixels, these are what we will use later on.


Separate these 'fingers' of pixels (pardon the horrible variable name):


numbers = Table[ConnectedComponents[adj][[i]], 
{i, 1,Length@ConnectedComponents[adj]}];

Now we want to calculate the largest and smallest points on each of these 'fingers' (these are the start/stop of these segments)


largestPointArr = 
Table[{#, Total[b[[#]]]} & /@ numbers[[i]], {i, 1,
Length@numbers}];

largestPts = (TakeLargestBy[#, #[[2]] &, 1] & /@ largestPointArr)[[All, 1, 1]];

smallestPointArr =
Table[{#, Total[b[[#]]]} & /@ numbers[[i]], {i, 1,
Length@numbers}];
smallestPts = (TakeSmallestBy[#, #[[2]] &, 1] & /@ largestPointArr)[[All, 1, 1]];

Now that we have the largest and smallest points, we want to create a curve that extends from these points. In order to generate a curve we need to have trailing and leading points from these largest/smallest points


This function grabs adjoining pixels to the leader or contra-leader of the 'finger'


findCurvePts[p_, 1] := Flatten[AdjacencyList[adj, #] & /@ Flatten[p]]

findCurvePts[p_, n_] :=
findCurvePts[(AdjacencyList[adj, #] & /@ Flatten[p]), n - 1]

Now we're going to fit a curve to the smallest and largest points using their neighbors.


largestPtsAndCurves = 
Flatten@Table[{largestPts[[i]] ->
Fit[b[[#]] & /@ findCurvePts[{largestPts[[i]]}, 6],
{1, x, x^2}, x]}, {i, 1, Length@largestPts}];

smallestPtsAndCurces =

Flatten@Table[{smallestPts[[i]] ->
Fit[b[[#]] & /@ findCurvePts[{smallestPts[[i]]}, 6],
{1, x, x^2}, x]}, {i, 1, Length@smallestPts}];

Each of the largest and smallest pts/curve variables contain the index of the large / small pixel correspond to a function


Because we now have functions which can predict where the next pixels should be, we can plug in the domain from the smallest points into our functions and compare the ranges


Here we plug and chug, only keeping connections where the predicted range is within 3 units of the actual range:


cadidates = 
Select[Flatten[
Table[{largestPts[[i]], smallestPts[[j]],

b[[smallestPts[[j]]]][[2]] - (largestPts[[i]] /. largestPtsAndCurves)
/. x -> b[[smallestPts[[j]]]][[1]]},
{i, 1, Length@largestPts}, {j, 1, Length@smallestPts}], 1],
Abs[#[[3]]] < 3 &];

smallCadidates =
Select[Flatten[
Table[{smallestPts[[i]], largestPts[[j]],
b[[largestPts[[j]]]][[2]] - (smallestPts[[i]] /. smallestPtsAndCurces)
/. x -> b[[largestPts[[j]]]][[1]]},

{i, 1, Length@smallestPts}, {j, 1, Length@largestPts}], 1],
Abs[#[[3]]] < 3 &];

Now lets see what we got- lets plot all of b(the white pixels) with the candidate connections:


Show[ListPlot[{b}], 
Graphics[{Line[{b[[#[[1]]]], b[[#[[2]]]]}] & /@
cadidates, {Pink, Point[b[[#[[1]]]]]} & /@
cadidates, {Blue, Point[b[[#[[2]]]]]} & /@ cadidates}]]

Show[ListPlot[{b}],

Graphics[{Line[{b[[#[[1]]]], b[[#[[2]]]]}] & /@
smallCadidates, {Pink, Point[b[[#[[1]]]]]} & /@
smallCadidates, {Blue, Point[b[[#[[2]]]]]} & /@ smallCadidates}]]

see what we got


What a mess! We are in need of some elimination- all of the candidates can be eliminated if they cross over other points- the "gaps" never cross points.


Now make some lines and remove leading and ending points:


bigLines = Line[{b[[#[[1]]]], b[[#[[2]]]]}] & /@ cadidates;
smallLines = Line[{b[[#[[1]]]], b[[#[[2]]]]}] & /@ smallCadidates;


filteredPts =
Complement[b, b[[#]] & /@ Join[smallestPts, largestPts]];

Now we calculate the distance from every line, to every other point- only qualify a line if the distance to other points is >= 1


 AbsoluteTiming[
smallPointDist =
ParallelTable[{i,
RegionDistance[smallLines[[i]], filteredPts[[j]]]},
{i, 1, Length@smallLines}, {j, 1, Length@filteredPts}]];


winningSmall = Select[Flatten[
Table[ MinimalBy[smallPointDist[[i]], #[[2]] &, 1],
{i, 1, Length@smallLines}], 1], #[[2]] >= 1 &]

AbsoluteTiming[
largePointDist =
ParallelTable[{i,
RegionDistance[bigLines[[i]], filteredPts[[j]]]},
{i, 1, Length@bigLines}, {j, 1, Length@filteredPts}]];


winningLarge =
Select[Flatten[
Table[ MinimalBy[largePointDist[[i]], #[[2]] &, 1],
{i, 1, Length@bigLines}], 1], #[[2]] >= 1 &]

Here's what we have:


Show[ListPlot[{b}], 
Graphics[{Red, Thick, bigLines[[#]]}] & /@ winningLarge[[All, 1]],
Graphics[{Blue, Thick, smallLines[[#]]}] & /@ winningSmall[[All, 1]]]


final


Here it is having some trouble with the reflection:


reflect


Here is the transposition:


enter image description here


I would love to hear ways to improve this!


Comments

Popular posts from this blog

functions - Get leading series expansion term?

Given a function f[x] , I would like to have a function leadingSeries that returns just the leading term in the series around x=0 . For example: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x)] x and leadingSeries[(1/x + 2 + (1 - 1/x^3)/4)/(4 + x)] -(1/(16 x^3)) Is there such a function in Mathematica? Or maybe one can implement it efficiently? EDIT I finally went with the following implementation, based on Carl Woll 's answer: lds[ex_,x_]:=( (ex/.x->(x+O[x]^2))/.SeriesData[U_,Z_,L_List,Mi_,Ma_,De_]:>SeriesData[U,Z,{L[[1]]},Mi,Mi+1,De]//Quiet//Normal) The advantage is, that this one also properly works with functions whose leading term is a constant: lds[Exp[x],x] 1 Answer Update 1 Updated to eliminate SeriesData and to not return additional terms Perhaps you could use: leadingSeries[expr_, x_] := Normal[expr /. x->(x+O[x]^2) /. a_List :> Take[a, 1]] Then for your examples: leadingSeries[(1/x + 2)/(4 + 1/x^2 + x), x] leadingSeries[Exp[x], x] leadingSeries[(1/x + 2 + (1 - 1/x...

mathematical optimization - Minimizing using indices, error: Part::pkspec1: The expression cannot be used as a part specification

I want to use Minimize where the variables to minimize are indices pointing into an array. Here a MWE that hopefully shows what my problem is. vars = u@# & /@ Range[3]; cons = Flatten@ { Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; Minimize[{Total@((vec1[[#]] - vec2[[u[#]]])^2 & /@ Range[1, 3]), cons}, vars, Integers] The error I get: Part::pkspec1: The expression u[1] cannot be used as a part specification. >> Answer Ok, it seems that one can get around Mathematica trying to evaluate vec2[[u[1]]] too early by using the function Indexed[vec2,u[1]] . The working MWE would then look like the following: vars = u@# & /@ Range[3]; cons = Flatten@{ Table[(u[j] != #) & /@ vars[[j + 1 ;; -1]], {j, 1, 3 - 1}], 1 vec1 = {1, 2, 3}; vec2 = {1, 2, 3}; NMinimize[ {Total@((vec1[[#]] - Indexed[vec2, u[#]])^2 & /@ R...

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]