Document field detection using Template Matching in Python
Document field detection is an important task in today’s industry. Thousands of documents undergo processing for various purposes and these contain lots of unrelated information. So the detection of required fields in documented images is of utmost importance. Template matching is an image processing method that we employ quite often in such cases. Basically, template matching is used to locate a smaller part (or template) of an image in the whole image. In this tutorial, we shall learn how to facilitate document field detection using template matching in Python.
For this industry problem here, we shall take into account the necessary fields. Then we can clip them out from the original image. After that, we can detect and locate the specific field using the template matching technique.
In this tutorial, we shall try to detect a portion from the preamble of the constitution (the original one) of India. So we shall first take an image from the internet. You can download it from here by clicking on the image.
Now, we cut one piece from the image that you can download by clicking the below image
So we shall try to detect the document field in the whole original image in the following code.
Importing the necessary libraries and modules for document field detection using Template Matching in Python
import cv2 import numpy as np import matplotlib.pyplot as plt %matplotlib inline
Loading the images
When we try to read the images, the default system is BGR. If we simply just read it, we would see a bluish tint in the image. So, we need to convert BGR to RGB using the cv2.cvtColor(image, cv2.BGR2RGB) function.
# whole image to search doc = cv2.imread('./input/constitution.jpg') doc = cv2.cvtColor(doc, cv2.COLOR_BGR2RGB) # The Template to Match temp = cv2.imread('./input/constitution_template.jpg') temp = cv2.cvtColor(temp, cv2.COLOR_BGR2RGB)
Matching the template
There are six available methods for the function cv2.matchTemplate() about which you can learn from here. We are going to use the simplest method for sliding the template over the image: cv2.TM_CCOEFF. Then for localization, we use the cv2.minMaxLoc() function the usage of which can be found here. The only useful value we need here is the maxLoc, that gives the pointer to the returned maximum location. Now, this maxLoc value for the metric TM_CCOEFF gives the top left corner of the detected area. Using this, we find the position of overlap and mark the boundary of the detected region.
# Apply template Matching with the method match = cv2.matchTemplate(doc, temp, eval('cv2.TM_CCOEFF')) # Grab the Max and Min values, plus their locations _, _, _, maxLoc = cv2.minMaxLoc(match) top_left_corner = maxLoc bottom_right_corner = (top_left_corner + width, top_left_corner + height) # Draw the Red Rectangle cv2.rectangle(doc,top_left_corner, bottom_right_corner, 255, 10) plt.figure(figsize = (20,20)) plt.imshow(doc) plt.title('Detected Point') plt.show()
This concludes the tutorial on document field detection using template matching in Python. For a better visualization of this topic, you can go through the various methods and understand the underlying mathematics, all of which are outside the scope of this tutorial.
You may check out the following tutorials for a firm grip on object detection in Python: