We have previously introduced cv2.approxPolyDP(), which can be used to approximate one contour with another with fewer points using the Douglas-Peucker algorithm. A key parameter in this function is epsilon, which sets the approximation accuracy. In contours_shape_recognition.py, we will make use of cv2.approxPolyDP() in order to recognize the contours (for example, triangle, square, rectangle, pentagon, or hexagon, among others) based on the number of detected vertices in the decimated contour (the output of cv2.approxPolyDP()). In order to decimate the number of points, given a certain contour, we first compute the perimeter of the contour. Based on the perimeter, the epsilon parameter is established. This way, the decimated contour is invariant to scale. The epsilon parameter is calculated as follows:
epsilon = 0.03 * perimeter
The constant 0.03 is established after several tests. For example, if this constant is bigger (for example, 0.1), the epsilon parameter will also be bigger and, hence, the approximation accuracy will be decreased.
This results in a contour with fewer points, and missing vertices are obtained. Therefore, recognition of the contours is performed incorrectly because it is based on the number of detected vertices. On the other hand, if this constant is smaller (for example, 0.001), the epsilon parameter will also be smaller and, hence, the approximation accuracy will increased, resulting in an approximation contour with more points. In this situation, the recognition of the contours is also performed incorrectly because false vertices are obtained.
The output of the contours_shape_recognition.py script can be seen in the next screenshot:
In the previous screenshot, the key steps (thresholding, contour approximation, and contour recognition) are shown.