top of page

Detect only red and green circles

Figure below shows the code used to detect the red and green circles. In the code, contour function is used. Contours are lines or curves that cover the edges of an object in an image. To find contours on the image, OpenCV has provided the function: cv2.findContours (mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE). cv2.RETR_EXTERNAL and cv2.CHAIN_APPROX_ SIMPLE are the inputs of the contour function. cv2.RETR_EXTERNAL retrieves only the extreme outer contours and cv2.CHAIN_APPROX_SIMPLE stores only the necessary corner points. In this code both inputs are substituted to see how data points were stored. To find circular objects, approxPolyDP is used. To find what kind of polygon the contour is, approxPolyDP gives the output as a number of vertices for the polygon. To understand how it works more, an example is provided. For triangle contour, it will give the output as 3points, square as 4points so on. But circles have higher number of points and on this code, it only detects any polygons with greater than 5 vertices form curvier shapes after approximation. Also, the function for approxPolyDP calculates the percentage of arcLength or perimeter of the contour. The higher the percentage, the lower the number of vertices. Therefore, using a very low percentage is used to find details of the contour.




9 views

Comments


bottom of page