Ayush Sahu
Ayush Sahu

Reputation: 69

How can I find the signature in an image?

Currently, I am working on a project which requires to extract the signature from a doctor's prescription. I am stuck in one place, I have used contours in opencv to find the patterns in the prescription and I can't figure out how to compare the contours to match the signature pattern.

This is the signature pattern: Image of signature I want to find in the prescription

This is the cropped prescription: Image of the cropped prescription

After using contours in cropped prescription, I found these patterns: Image of contours in cropped prescription

Upvotes: 0

Views: 1979

Answers (1)

Shubham Jaiswal
Shubham Jaiswal

Reputation: 359

#import necessary packages
import cv2
import numpy as np
import matplotlib.pyplot as plt
#read the query and the template image in gray_sacle
ref_img = cv2.imread("ref_img.jpg",0)
template_img = cv2.imread("temp_img.jpg",0)
w, h = template_img.shape[::-1]
#the methods to be used for template matching
methods = ['cv2.TM_CCOEFF_NORMED','cv2.TM_CCORR_NORMED']
#set a threshold to qualify for a match
threshold = 0.9
#loop overboth the methods and do template matching
#plot the results if the threshold is qualified
for meth in methods:
    img = ref_img.copy()

    method = eval(meth)

    # Apply template Matching
    res = cv2.matchTemplate(img,template_img,method)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

    top_left = max_loc
    bottom_right = (top_left[0] + w, top_left[1] + h)

    if max_val >= threshold:
        print(min_val,max_val)
        cv2.rectangle(img,top_left, bottom_right, 0, 2)

        plt.subplot(121),plt.imshow(res,cmap = 'gray')
        plt.title('Matching Result'), plt.xticks([]), plt.yticks([])
        plt.subplot(122),plt.imshow(img,cmap = 'gray')
        plt.title('Detected Point'), plt.xticks([]), plt.yticks([])
        plt.suptitle(meth)

        plt.show()

Output for cv2.TM_CCOEFF_NORMED

Output for cv2.TM_CCORR_NORMED

Upvotes: 1

Related Questions