The output of the CV2.Approxpolydp () function is a vertex coordinates of approximate polygon

tags: Computer vision  opencv  python

The original image is

import cv2
import numpy as np
import matplotlib.pyplot as plt

def cv_show(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()


# Read the image
img = cv2.imread('contours.png')
cv_show('contours', img)

 # Gray degree and dual value
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)[1]
cv_show('thresh', thresh)

 #
cnt = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)[1]

After outline detection, the value of the output is CNT, the type is list, and the number of elements is 11, which contains 11 contour

C0 is indexed in CNT as 0, which represents the first outline, that is, the triangular outline

c0 = cnt[0]
res = cv2.drawContours(draw, [c0], -1, (0, 0, 255), 2)
cv_show('res', res)

 

The outline is similar below

epsilon = 0.1 * cv2.arcLength(c0, True)
approx = cv2.approxPolyDP(c0, epsilon, True)
draw_img = img.copy()
res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2)
cv_show('res', res)

CV2.APAPROXPLOYDP function output APPROX, the number of elements is 3, that is, the three vertices of the triangle

 

 

Intelligent Recommendation

Python traverse the vertex coordinates of the line or surface in the output arcgis

Use python to traverse and output the vertex coordinates of the arcgis line or surface Code first, then detailed explanation operation result: import arcpy Import the module, because arcpy is used, so...

Use arcpy to achieve batch calculation of maps in arcgis to coordinates (polygon vertex calculation, range acquisition, pyhon class creation)

First, the problem and solution The inspiration for this method came from yesterday yesterday, let's talk about implementing the function. There has always been a need for four-to-coordinate extractio...

Polygon outline surrounded approxPolyDP

approxPolyDP method: Function effect: Polygonal approximation curve specified precision   parameter: curve: a collection of contour point input approxCurve: output set of contour points. Save the...

C# Polygon Vertex Sorting

Method principle: According to the definition of vector cross product, the cross product of vectors OA and OB is greater than 0, then vector OB is in the counterclockwise direction of vector OA, that ...

More Recommendation

cv2.approxPolyDP()

Function of CV2.Approxpolydp ():...

Approximate calculation of vertex cover problem

The algorithm analysis and the design teacher's placement test assignments, one of which is the approximate calculation of the vertex cover problem, thinks for a long time, thinks of a train of though...

Output element --- OpenGL polygon filling function

2019 Unicorn Enterprise Heavy Recruitment Python Engineer Standard >>> OpenGL polygon filling function The function GlvertEX is used to enter a vertex coordinate of the polygon, and the compl...

approxPolyDP function

1, approxPolyDP function The function of the function: Fit polygons of image outline points 2. The calling form of the function C++: void approxPolyDP (InputArray curve, OutputArray approxCurve, doubl...

OpenGL vertex coordinates and sampler2D texture coordinates

When using non-fixed pipelines for texture rendering, the mapping relationship between texture and vertex coordinates is often used. Here, the two-dimensional texture coordinate mapping is introduced ...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top