SoFunction
Updated on 2025-03-02

OpenCV fingerprint recognition code example

1. Meaning

Using OpenCV for fingerprinting is a complex and challenging task, as fingerprinting often requires high-precision feature extraction and matching algorithms. While OpenCV provides a variety of tools for image processing and computer vision, fingerprinting may not always be effective enough using OpenCV’s built-in features (such as SIFT, SURF, ORB, etc.).

2. Code implementation

1. Calculate the matching point

import os  
import cv2
def getNum(src, model):  
    # Read two fingerprint images    img1 = (src)  
    img2 = (model)  
      
    # Create a SIFT feature detector    sift = cv2.SIFT_create()  
      
    # Detect feature points and calculate feature descriptors    kp1, des1 = (img1, None)  
    kp2, des2 = (img2, None)  
      
    # Create a FLANN matcher    flann = ()  
      
    # Use the KNN algorithm to find the best two matches    matches = (des1, des2, k=2)  
      
    #Storage good matches    ok = []  
    for m, n in matches:  
        # Filter matches based on Lowe's ratio test        if  < 0.8 * :  
            (m)  
      
    # Return the number of good matches    num = len(ok)  
    return num

Defines a function to calculate the number of matching feature points between two fingerprint images. This function uses the SIFT (Scale-Invariant Feature Transform) feature detector and the FLANN (Fast Library for Approximate Nearest Neighbors) matcher in the OpenCV library. Their similarity is evaluated by calculating the number of matching feature points between two fingerprint images.

2. Get the number

def getID(src, database):  
    max_num = 0  # Initialize the maximum number of match points to 0    for file in (database):  # traverse files in the database        model = (database, file)  # Build the full path to the model file        num = getNum(src, model)  # Calculate the number of matching points between the current model and the source fingerprint        print("file name:", file, "distance:", num)  # Print file name and matching points          
        # If the current number of matches is greater than the maximum number of matches, update the maximum number of matches and the corresponding file name.        if num > max_num:  
            max_num = num  
            name = file  
      
    # Extract the ID from the file name (here assumes that the first character of the file name is the ID)    ID = name[0] if name else None  # If name is empty, the ID is None (Error handling should be added here)      
    # If the maximum number of match points is less than 100, set the ID to 9999 (this is usually not a good practice as it can cause confusion)    if max_num < 100 and ID is not None:  # Add ID non-empty check        ID = 9999  
      
    return ID

Defines a fingerprint that identifies the most matching the source fingerprint image from the fingerprint database and returns the ID associated with the fingerprint. First use the function to list all files in the database directory and build the full path to each file. Call the getNum function to calculate the number of matching points between the source fingerprint image and the current model fingerprint image and print the result. If the current number of match points is greater than the maximum number of match points, the maximum number of match points and the corresponding file name are updated.

3. Obtain the name

def getName(ID):  
    # Define a dictionary to map ID to name    nameID = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f',  
              6: 'g', 7: 'h', 8: 'i', 9: 'j', 9999: 'k'}  
      
    # Get the name from the dictionary (return None if the ID is not in the dictionary)    name = (int(ID))  
    return name

Get the corresponding name based on the given ID through a predefined dictionary nameID. If the given ID is not in the dictionary, in theory, None should be returned or other measures should be taken to handle this situation.

4. Main function

if __name__ == "__main__":  
    src = ''  # The path to the source fingerprint image    database = 'database'  # Path to fingerprint database directory    ID = getID(src, database)  # Get fingerprint ID    name = getName(ID)  # Get name by ID    print('Identification result:', name)  # Print recognition results

Use the previously defined getID and getName functions to identify the fingerprint image and print out the corresponding name.

3. Summary

This code implements a simple fingerprint recognition system, using SIFT features and FLANN matchers to detect and identify fingerprints. But in fact, we may encounter various problems, so we need to adjust and optimize the code according to actual application scenarios, especially the preprocessing and feature extraction parts of fingerprint images.

This is all about this article about the implementation of OpenCV fingerprint recognition. For more related OpenCV fingerprint recognition content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!