代码已测试过,可以直接使用。
from os import path
import math
import numpy as np
import cv2 as cv
import matplotlib.pyplot as plt
from utils import *
from ipywidgets import interact
fingerprint = cv.imread('samples/sample_1_1.png', cv.IMREAD_GRAYSCALE)
show(fingerprint, f'Fingerprint with size (w,h): {fingerprint.shape[::-1]}')
![](https://img-blog.csdnimg.cn/20ffc6995ccd4cb7a2b5c130f3ff4a81.png)
# Calculate the local gradient (using Sobel filters)
gx, gy = cv.Sobel(fingerprint, cv.CV_32F, 1, 0), cv.Sobel(fingerprint, cv.CV_32F, 0, 1)
show((gx, 'Gx'), (gy, 'Gy'))
![](https://img-blog.csdnimg.cn/129792c8a8a74dcb8d156dfab8eb308b.png)
Step 1: Fingerprint segmentation(指纹分割)
fingerprint = cv.imread('samples/sample_1_1.png', cv.IMREAD_GRAYSCALE)
show(fingerprint, f'Fingerprint with size (w,h): {fingerprint.shape[::-1]}')
Fingerprint with size (w,h): (256, 364) |
![](https://img-blog.csdnimg.cn/d63f75f705954bf78c2dee6f36b7c077.png) |
gx, gy = cv.Sobel(fingerprint, cv.CV_32F, 1, 0), cv.Sobel(fingerprint, cv.CV_32F, 0, 1)
show((gx, 'Gx'), (gy, 'Gy'))
Gx |
Gy |
![](https://img-blog.csdnimg.cn/5141cf5d6a5f4b7f833122034d891732.png) |
![](https://img-blog.csdnimg.cn/8f42eaf692014e718fff6c705193525c.png) |
sum_gm = cv.boxFilter(gm, -1, (25, 25), normalize = False)
show(sum_gm, 'Integral of the gradient magnitude')
Gx**2 |
Gy**2 |
Gradient magnitude |
![](https://img-blog.csdnimg.cn/a2a4f561df2d4505a3c8b0da55603301.png) |
![](https://img-blog.csdnimg.cn/c4feca9a939b4d5bbad42fffcd586dae.png) |
![](https://img-blog.csdnimg.cn/5b59f4cd327049f4b71fca9aae0c5034.png) |
sum_gm = cv.boxFilter(gm, -1, (25, 25), normalize = False)
show(sum_gm, 'Integral of the gradient magnitude')
Integral of the gradient magnitude |
![](https://img-blog.csdnimg.cn/b6ef4b6700634013a761d7236b453e5a.png) |
thr = sum_gm.max() * 0.2
mask = cv.threshold(sum_gm, thr, 255, cv.THRESH_BINARY)[1].astype(np.uint8)
show(fingerprint, mask, cv.merge((mask, fingerprint, fingerprint)))