我想使用 python 比较两个图像以检查它们是否相同,我想将其用于 django 应用程序中的指纹功能来验证提供的指纹是否与数据库中存储的指纹匹配。我决定使用 OpenCV 来实现此目的,将
ORB_create
与 detectAndCompute
结合使用,并将提供的指纹提供给 BFMatcher
。然而,使用下面的代码,当尝试匹配图像时,它始终返回 True,而提供的图像并不相同,以及 print
声明“图像是相同的”。
def compared_fingerprint(image1, image2):
finger1 = cv2.imread(image1, cv2.IMREAD_GRAYSCALE)
finger2 = cv2.imread(image2, cv2.IMREAD_GRAYSCALE)
orb = cv2.ORB_create()
keypoints1, descriptors1 = orb.detectAndCompute(finger1, None)
keypoints2, descriptors2 = orb.detectAndCompute(finger2, None)
bf = cv2.BFMatcher()
matches = bf.match(descriptors1, descriptors2)
threshold = 0.7
similar = len(matches) > threshold * len(keypoints1)
if similar:
print('Images are the same')
return similar
else:
print('Images are not the same')
return similar
result = compared_fingerprint('c.jpg', 'a.jpg')
print(result)
使用提供的图像,该函数应该返回第二个语句,因为它们不一样,我想,这是
threshold
分配给 0.7
,当我将 threshold
增加到 1.7
时,它返回第二个语句:“图像不一样”False,但是当我尝试使图像相同时,我的意思是:result = compared_fingerprint('a.jpg', 'a.jpg')
,它仍然返回“图像不一样”False。
a.jpg
c.jpg
我只是在这里呼应@ChristophRackwitz,但特征匹配算法不适合这种情况,至少它本身不适合。它将一个图像的小子补丁与另一个图像匹配,并且由于所有指纹图像看起来相似,ORB也会找到足够多的相似补丁(因此,你的方法将始终决定它们确实相似) .
至少,您需要使用例如空间匹配两个图像
findHomography
(参见 https://pyimagesearch.com/2020/08/31/image-alignment-and-registration-with-opencv/ 作为示例),然后计算内部值与异常值之间的比率特征点和计算的单应性。 drawMatches
函数将有助于可视化这一点(如果匹配良好,您将主要看到平行线)。