Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cv2.approxPolyDP() , cv2.arcLength() How these works

How do these function works? I am using Python3.7 and OpenCv 4.2.0. Thanks in Advance.

approx = cv2.approxPolyDP(cnt, 0.01*cv2.arcLength(cnt, True), True)
like image 766
Aditya Anand Avatar asked Jun 09 '20 03:06

Aditya Anand


People also ask

How to perform contour approximation using epsilon in AutoCAD?

Accordingly, we start looping epsilon’s ( eps) value over a range to feed it to the contour approximation function ( Line 45 ). On Line 47, the perimeter of the contour is calculated using cv2.arcLength. We then use the cv2.approxPolyDP function and initiate the contour approximation process ( Line 48 ).

How to determine the shape of polygons in OpenCV image using Python?

OpenCV program in python to determine the shape of the polygons in a given image by using approxPolyDP () function name the detected shapes and then display the resulting image as the output on the screen: The output of the given program is shown in the snapshot below:

How to perform an approximation of a shape of a contour?

We make use of a function in OpenCV called approxPolyDP () function to perform an approximation of a shape of a contour. The image of a polygon whose shape of a contour must be approximated is read using the imread () function.

Is it possible to remove redundant vertices from a road map?

Considering your size is small enough to ignore redundant turns (parts where you can go straight without following the exact curves of the road), it would be easier for you if you could remove some redundant vertices. Something like Figure 3: Figure 3: Contour Approximation in action.


1 Answers

If you are looking for a example snippet, below is one:

import cv2
import imutils

# edged is the edge detected image
cnts = cv2.findContours(edged, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]
# loop over the contours
for c in cnts:
    # approximate the contour
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    # if our approximated contour has four points, then we
    # can assume that we have found our screen
    if len(approx) == 4:
        screenCnt = approx
        break

In the above snippet, first it finds the contours from a edge detected image, then it sorts the contours to find the five largest contours. Finally it loop over the contours and used cv2.approxPolyDP function to smooth and approximate the quadrilateral. cv2.approxPolyDP works for the cases where there are sharp edges in the contours like a document boundary.

like image 189
amras Avatar answered Nov 04 '22 10:11

amras