Hu moment invariants can be used for both object matching and recognition. In this section, we are going to see how to match contours based on Hu moment invariants. OpenCV provides cv2.matchShapes(), which can be used to compare two contours using three comparison methods. All these methods use Hu moment invariants. The three implemented methods are cv2.CONTOURS_MATCH_I1, cv2.CONTOURS_MATCH_I2, and cv2.CONTOURS_MATCH_I3.
If A denotes the first object and B denotes the second object, then the following applies:
are the Hu moments of A and B, respectively.
Finally, see the following:
- cv2.CONTOURS_MATCH_I1:
- cv2.CONTOURS_MATCH_I2:
- cv2.CONTOURS_MATCH_I3:
In contours_matching.py, we are making use of cv2.matchShapes() to match several contours against a perfect circle contour.
First of all, we draw a perfect circle in an image by using the OpenCV function...