Handwritten signature similarity match in python

1.9k Views Asked by At

I am trying to solve problem where i have two digital handwritten signature i have to find similarity percentage between them

Below is the code

import cv2
from skimage.metrics import structural_similarity as ssim
import numpy as np
# TODO add contour detection for enhanced accuracy

def removeWhiteSpace(img):
    # img = cv2.imread('ws.png') # Read in the image and convert to grayscale
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    gray = 255*(gray < 128).astype(np.uint8) # To invert the text to white
    coords = cv2.findNonZero(gray) # Find all non-zero points (text)
    x, y, w, h = cv2.boundingRect(coords) # Find minimum spanning bounding box
    rect = img[y:y+h, x:x+w]
    return rect
    
def match(path1, path2):
    # read the images
    img1 = cv2.imread(path1)
    img2 = cv2.imread(path2)
    img1 = removeWhiteSpace(img1)
    img2 = removeWhiteSpace(img2)
    # turn images to grayscale
    img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
    img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
    # resize images for comparison
    img1 = cv2.resize(img1, (300, 300))
    img2 = cv2.resize(img2, (300, 300))
    # display both images
    cv2.imshow("One", img1)
    cv2.imshow("Two", img2)
    cv2.waitKey(0)
    cv2.destroyAllWindows()
    similarity_value = "{:.2f}".format(ssim(img1, img2,gaussian_weights = True,sigma= 1.2,use_sample_covariance = False)*100)
    # print("answer is ", float(similarity_value),
    #       "type=", type(similarity_value))
    return float(similarity_value)



path1 = 'sample_images\img3.jpeg'
path2 = 'sample_images\img4.jpeg'
similarity_value = match(path1,path2)
print(similarity_value)

Example#1

Below are the two images

Image1: enter image description here Image2: enter image description here

when execute the above code i am getting 77.93 similarity which is good percentage

Example#2

Below are the two images

Image1: enter image description here Image2: enter image description here

when execute the above code i am getting 66.36 similarity match

Ask

How can i improve the similarity match percentage in example#2 as both images look similar?

0

There are 0 best solutions below