Contour Matching
In this section, we will learn how to numerically find the difference between the shapes of two different contours. This difference is found based on Hu moments. Hu moments (also known as Hu moment invariants) of a contour are seven numbers that describe the shape of a contour.
Note
Visit https://docs.opencv.org/2.4/modules/imgproc/doc/structural_analysis_and_shape_descriptors.html for more details.
Hu moments can be computed using OpenCV in the following way:
Image moments can be computed with the following command:
img_moments= cv2.moments(image)
The cv2.HuMoments
OpenCV function is given the following moments as input:
hu_moments= cv2.HuMoments(img_moments)
This array is then flattened to get the feature vector for the Hu moments:
hu_moments= hu_moments.flatten()
This feature vector has a row with seven columns (seven numeric values).
Hu moment vectors of some sample contour shapes are shown in the following table: