Template matching is an image processing technique which is used to find the location of small-parts/template of a large image. This technique is widely used for object detection projects, like product quality, vahicle tracking, robotics etc.
In this article, we will learn how to use template matching for detecting the related fields in a document image.
Solution:
Above task can be achieved using template matching. Clip out the field images and apply template matching using clipped field images and the document image. The algorithm is simple yet reproducible into complex versions to solve the problem of field detection and localization for document images belonging to specific domains.
Approach:
- Clip/Crop field images from the main document and use them as separate templates.
- Define/tune thresholds for different fields.
- Apply template matching for each cropped field template using OpenCV function
cv2.matchTemplate() - Draw bounding boxes using the coordinates of rectangles fetched from template matching.
- Optional: Augment field templates and fine tune threshold to improve result for different document images.
Input Image:

Output Image:

Below is the Python code:
# importing libraries import numpy as np import imutils import cv2 field_threshold = { "prev_policy_no" : 0.7, "address" : 0.6, } # Function to Generate bounding # boxes around detected fields def getBoxed(img, img_gray, template, field_name = "policy_no"): w, h = template.shape[::-1] # Apply template matching res = cv2.matchTemplate(img_gray, template, cv2.TM_CCOEFF_NORMED) hits = np.where(res >= field_threshold[field_name]) # Draw a rectangle around the matched region. for pt in zip(*hits[::-1]): cv2.rectangle(img, pt, (pt[0] + w, pt[1] + h), (0, 255, 255), 2) y = pt[1] - 10 if pt[1] - 10 > 10 else pt[1] + h + 20 cv2.putText(img, field_name, (pt[0], y), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 1) return img # Driver Function if __name__ == '__main__': # Read the original document image img = cv2.imread('doc.png') # 3-d to 2-d conversion img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Field templates template_add = cv2.imread('doc_address.png', 0) template_prev = cv2.imread('doc_prev_policy.png', 0) img = getBoxed(img.copy(), img_gray.copy(), template_add, 'address') img = getBoxed(img.copy(), img_gray.copy(), template_prev, 'prev_policy_no') cv2.imshow('Detected', img) |
Advantages of using template matching:
- Computationally inexpensive.
- Easy to use and modifiable for different use-cases.
- Gives good results in case of document data scarcity.
Disadvantages:
- Result are not highly accurate as compared to segmentation techniques using deep learning.
- Lacks overlapping pattern problem resolution.
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
Recommended Posts:
- Real-Time Edge Detection using OpenCV in Python | Canny edge detection method
- Python | Corner detection with Harris Corner Detection method using OpenCV
- Python | Corner Detection with Shi-Tomasi Corner Detection Method using OpenCV
- Template matching using OpenCV in Python
- Object Detection with Detection Transformer (DERT) by Facebook
- Mahotas - Template Matching
- Prefix matching in Python using pytrie module
- Feature matching using ORB algorithm in Python-OpenCV
- MongoDB - Insert Single Document Using MongoShell
- MongoDB - Insert Multiple Document Using MongoShell
- MongoDB - Update Single Document Using MongoShell
- MongoDB - Delete Single Document Using MongoShell
- Detection of a specific color(blue here) using OpenCV with Python
- Python Program to detect the edges of an image using OpenCV | Sobel edge detection method
- Face Detection using Python and OpenCV with webcam
- Python | Real time weather detection using Tkinter
- Car driving using hand detection in Python
- Python | Smile detection using OpenCV
- Circle Detection using OpenCV | Python
- White and black dot detection using OpenCV | Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


