Background
A friend of mine reach out and asked me whether I could write a program to detect the number of Rubber stamps in an image. Apparently, these invoice receipts will be categorized based on the number of stamps on them. Initially, I thought of building a Deep Learning Segmentation model, but soon I realized that it is not worth the effort.
The images are generated in a controlled environment so few computer vision algorithms should do the trick. To illustrate the computer vision algorithms used in detecting the stamps, I will be using a sample image downloaded from Google as the original image is company property. The goal is to identify two stamps in the sample image.
Solution
High level solution steps are:
- Read the image.
- Blur & detect the edges.
- Find all contours and remove the smaller contours.
- Fill the area inside contours & Close the blobs.
- Filter the stamps.
Before we start let us import the necessary packages.
1. Read the image
Read the color image using imread function. To display the image we will use Matplotlib. Matplotlib expects the color image channels to be of the order RGB, but OpenCV reads the image as BGR, so we will write a helper function for the conversion.
2. Blur & detect the edges
First, we need to convert the image to grayscale using cvtColor function. Then, we will use bilateralFilter to reduce noise in the image. Bilateral filter is preferred over Gaussian because it preserves the edges much better. Finally, we will use canny edge detector to threshold the image and detect edges. The normal canny edge detector requires two threshold parameters which is hard to tune so we will use the one from Zero-parameter, automatic Canny edge detection with Python and OpenCV
3. Find all contours and remove the smaller contours
findContours function can find all contours in the image. The outer most contours are good enough for our use case so we will use the retrieval mode RETR_EXTERNAL. CHAIN_APPROX_NONE mode is preferred as we don't want to lose any point on the contour due to approximation. To remove the unwanted smaller contours, we can filter the contours by area.
Total nr of contours found: 408
4. Fill the area inside contours & Close the blobs
Instead of working on the original binary image, we will draw the top contours on a image with black background and use this as base. Any disconnect in the contours are easier to identify when fill the area inside the contours using drawContours function.
As suspected, the top stamp has a thin black line passing through it. We need to close this blob so that the top stamp is considered as one contour instead of two different ones. Morphological Closing operation is perfect for achieving this.
5. Filter the stamps
To isolate the stamp contours, we can identify all the contours from the latest binary image and filter for contours with more than 5 points as the stamp is an ellipse.
Bonus - Highlight the identified stamps
For demo of this program, wouldn't it be cool if we can highlight only the stamped area of the image? Since we agree that it is indeed cool, let us see how we can achieve that. The steps involved are:
- Duplicate the original image and blur the entire image.
- Loop through the blurred image and for the points on or inside the image (using pointPolygonTest to check) we replace it with pixel values from the output image. We are using pixel values from the output image because we need the blue lines drawn over the stamps.
Conclusion
Yep, that's it for this post. You can access this notebook from here.
Top comments (4)
Nice one!
Impressive!
I'm curious how you'd handle a couple of plausible additional cases, however:
A round or oval company logo on the form.
A square or rectangular stamp.
With some sample images this is not impossible, but would require some trail and error.
Most probably the company logo would not change so we can filter out the logo contour by one of the options:
We can find the center of the contour using the moments and when combined with the extreme points of the contour it should be easy to identify square or rectangle stamps.
Hope you find this useful.
This is really one of the best guide on stamp detection. I am going to use for website beststampguide.com/ . Thanks lot for writing this detailed one on stamp detection using python.