• 大小: 1.75MB
    文件类型: .zip
    金币: 1
    下载: 6 次
    发布日期: 2021-04-10
  • 语言: Python
  • 标签: python  sift  opencv  

资源简介

Windows64位环境下: 1. 安装python3.6.5 2. pip install numpy 3. pip install matplotlib 4. pip install opencv-python 5. 安装opencv-conrib(https://download.csdn.net/download/u012442083/10809277)

资源截图

代码片段和文件信息

# coding=utf-8
import os
import cv2
import shutil



# 指纹匹配
# src_file  待匹配的指纹图片
# db_file   指纹数据库中的特征指纹图片
# 指纹 1 x 1 匹配,返回该指纹的匹配得分
# 参考 http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_feature2d/py_matcher/py_matcher.html
def match1_1_sift(src_file db_file):

    img1 = cv2.imread(src_file)  # queryImage
    #img1 = cv2.cvtColor(img1 cv2.COLOR_BGR2GRAY)
    img2 = cv2.imread(db_file)  # trainImage
    #img2 = cv2.cvtColor(img2 cv2.COLOR_BGR2GRAY)

    # Initiate SIFT detector
    cv2.ocl.setUseOpenCL(False)
    sift = cv2.xfeatures2d.SIFT_create()

    # find the keypoints and descriptors with SIFT
    kp1 des1 = sift.detectAndCompute(img1 None)
    kp2 des2 = sift.detectAndCompute(img2 None)

    FLANN_INDEX_KDTREE = 0
    index_params = dict(algorithm=FLANN_INDEX_KDTREE trees=5)
    search_params = dict(checks=50)

    flann = cv2.FlannbasedMatcher(index_params search_params)
    matches = flann.knnMatch(des1 des2 k=2)

    good = []
    for m n in matches:
        if m.distance < 0.55 * n.distance:#0.65
            good.append(m)

    return len(good)


# 指纹批量匹配
# src_file  待匹配的指纹图片
# db_path   指纹数据库路径,该路径下包含若干特征指纹图片
# 指纹 1 x N 匹配,返回该指纹是否匹配成功
def match1_N(src_file db_path):
    good_max = 0
    for afile in os.listdir(db_path):
        db_file = os.path.join(db_path afile)
        num = match1_1_sift(src_file db_file)
        if good_max < num:
            good_max = num
    if good_max < 7:   # 指纹得分阈值为10
        return False
    else:
        return True


# 指纹批量匹配
# src_file  待匹配的指纹图片
# db_path   指纹数据库路径,该路径下包含若干特征指纹图片
# 指纹 1 x N 匹配,返回该指纹的最高得分
def match1_N_test2(src_file db_path):
    good_max = 0
    for afile in os.listdir(db_path):
        db_file = os.path.join(db_path afile)
        num = match1_1_sift(src_file db_file)
        if good_max < num:
            good_max = num
    return good_max


# 指纹批量测试
# src_path  待匹配的指纹文件夹,该文件夹下包含若干指纹图片
# db_path   指纹数据库路径,该路径下包含若干特征指纹图片
# f         文件标识符,该函数会把结果写入到文件
# 指纹 N x N 匹配,输出该文件夹下指纹匹配成功的比例
def matchN_N_test(src_path db_path f):
    i = 0
    j = 0
    for afile in os.listdir(src_path):
        src_file = os.path.join(src_path afile)
        j = j + 1
        if match1_N(src_file db_path):
            i = i + 1

    f.write(src_path + ‘----------‘ + db_path + ‘\n‘)
    f.write(‘ratio:‘ + str(i) + ‘/‘ + str(j) + ‘\n‘)
    f.write(‘--------------------‘ + ‘\n‘)
    f.flush()


# 指纹批量测试
# src_path  待匹配的指纹文件夹,该文件夹下包含若干指纹图片
# db_path   指纹数据库路径,该路径下包含若干特征指纹图片
# f         文件标识符,该函数会把结果写入到文件
# 指纹 N x N 匹配,输出该指纹的最高得分
def matchN_N_test2(src_path db_path f):
    for afile in os.listdir(src_path):
        src_file = os.path.join(src_path afile)
        # 对每个指纹图片进行 1 x N 匹配,得到一个得分
        good_num = match1_N_test2(src_file db_path)
        f.write(src_file + ‘----------‘ + db_path + ‘\n‘)
        f.write(‘max good:‘ + str(good_num) + ‘\n‘)
        f.write(‘--------------------‘ + ‘\n‘)
        f.flush()


# 判断指纹质量
# file_path 指纹图片文件路径
# 该函数通过计算指纹图片中白色无效块和黑色无

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-12-03 21:09  sift\
     文件          61  2018-12-03 21:11  sift\data.txt
     目录           0  2018-11-26 21:22  sift\db1\
     目录           0  2018-11-27 15:40  sift\db1\img\
     文件       26678  2018-11-27 15:20  sift\db1\img\fp0_1.bmp
     文件       26678  2018-11-27 15:20  sift\db1\img\fp0_2.bmp
     文件       26678  2018-11-27 15:20  sift\db1\img\fp0_3.bmp
     文件       26678  2018-11-27 15:20  sift\db1\img\fp0_4.bmp
     文件       26678  2018-11-27 15:20  sift\db1\img\fp0_5.bmp
     文件       26678  2018-11-27 15:20  sift\db1\img\fp0_6.bmp
     文件       26678  2018-11-27 15:20  sift\db1\img\fp1_0.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp1_1.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp1_2.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp1_3.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp1_4.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp1_5.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp1_6.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp2_0.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp2_1.bmp
     文件       26678  2018-11-27 15:21  sift\db1\img\fp2_2.bmp
     文件       26678  2018-11-27 15:22  sift\db1\img\fp2_3.bmp
     文件       26678  2018-11-27 15:22  sift\db1\img\fp2_4.bmp
     文件       26678  2018-11-27 15:22  sift\db1\img\fp2_5.bmp
     文件       26678  2018-11-27 15:22  sift\db1\img\fp2_6.bmp
     文件       26678  2018-11-27 15:22  sift\db1\img\fp3_0.bmp
     文件       26678  2018-11-27 15:22  sift\db1\img\fp3_1.bmp
     文件       26678  2018-11-27 15:23  sift\db1\img\fp3_2.bmp
     文件       26678  2018-11-27 15:23  sift\db1\img\fp3_3.bmp
     文件       26678  2018-11-27 15:23  sift\db1\img\fp3_4.bmp
     文件       26678  2018-11-27 15:23  sift\db1\img\fp3_5.bmp
     文件       26678  2018-11-27 15:23  sift\db1\img\fp3_6.bmp
............此处省略54个文件信息

评论

共有 条评论