How to do template matching without OpenCV?

I have an order invoice of documents belonging to Amazon, eBay, Flipkart, SnapDeal, and I want to extract less information from the order invoice. Since the fields like the order number, customer name, order details will be present at different positions in these 4 templates,

I need to first classify to which of these 4 templates the input image will belong to and after identifying the template I can do my next work of text extraction using tesseract and regex buy writing code for specific templates.

When I searched I found the only OpenCV had this feature I was not able to find any convolution neural network model . Are there any such neural network models available for template matching and classification?

I cannot use the standard model available for classifying dogs and cats because here I'm dealing with the image of an invoice templates which has only text with rows and columns in some format.


1 Answer 1

import numpy as np
import matplotlib.image as img
import matplotlib.pyplot as plt
from skimage.metrics import structural_similarity as ssim

def rgb2gray(rgb):
    return np.dot(rgb[...,:3], [0.299, 0.587, 0.144])

full_image = rgb2gray(img.imread("img/full_image.png"))
sub_image = rgb2gray(img.imread("img/sub_image.png"))

full_w,full_h = full_image.shape[:2]
sub_w,sub_h = sub_image.shape[:2]


winW = 0
found = False
while winW < full_w - sub_w and found == False:
    winH = 0
    while winH < full_h - sub_h:
        window = full_image[winW:winW+sub_w, winH:winH+sub_h]
        if ssim(sub_image, window) > 0.80:
            found = True
            print("found", ssim(sub_image, window))
        winH += 2
    winW += 2
  • $\begingroup$ Change the last increments by 2 to adjust the step-size of the window. The code will be too slow as it uses loops. Use threading to make it a bit faster $\endgroup$ Jan 26, 2021 at 10:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.