Mastering OpenCV 4 with Python by Fernández Villán Alberto;

Mastering OpenCV 4 with Python by Fernández Villán Alberto;

Author:Fernández Villán, Alberto;
Language: eng
Format: epub
Tags: COM051360 - COMPUTERS / Programming Languages / Python, COM016000 - COMPUTERS / Computer Vision and Pattern Recognition, COM051010 - COMPUTERS / Programming Languages / General
Publisher: Packt Publishing, Limited
Published: 2019-03-28T10:26:14+00:00


As you can see, in the upper part of the screenshot the original image is shown, while in the bottom part of the screenshot the original image has been modified to include the ordering number in the center of each contour.

Recognizing contours

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:



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.