Contour Features Boundary characteristics

Source: Internet
Author: User

Find Contour findcontours
1 cv2.findcontours (image, Mode, method[, contours[, hierarchy[, offset]]) →image, contours, hierarchy

Parameter interpretation
    • Image: Original images, can be obtained by compare (), InRange (), Threshold () and other binary image image
    • Mode: Contour Retrieval modes
    • Method: Contour Approximation methods
The mode parameter has the desired value
    1. Cv_retr_external retrieves only external outlines.
    2. Cv_retr_list retrieves all outlines but does not establish hierarchical relationships.
    3. Cv_retr_ccomp retrieves all the outlines and establishes a level two hierarchy.
    4. Cv_retr_tree retrieves all the outlines and establishes a nested outline hierarchy.
The method parameter is preferable
    1. Cv_chain_approx_none stores all contour points.
    2. Cv_chain_approx_simple compresses horizontal, vertical, and diagonal lines, leaving only their endpoints.
    3. Cv_chain_approx_tc89_l1,cv_chain_approx_tc89_kcos applied a style of the teh-chin-chain approximation algorithm
Calculate the contour area of an image Cv2.contourarea ()
1 Cv2.contourarea (contour[, oriented]) →retval

calculate the moment of the image cv2.moments ()
1 cv2.moments (array[, binaryimage]) →retval

See code for details
1 ImportCv22 ImportNumPy as NP3  4img = Cv2.imread ('star.jpg', 0)5Ret,thresh = Cv2.threshold (img,127,255, 0)6Contours,hierarchy =cv2.findcontours (Thresh, CV. Retr_external, CV. Chain_approx_simple)7  8CNT =Contours[0]9M =cv2.moments (CNT)Ten Print(Cv2.contourarea (CNT)) One Print(M) A   - #get {' m00 ': 0.5, ' M10 ': 53.83333333333333, ' m01 ': 367.3333333333333, ' M20 ': 5796.083333333333, ' M11 ': 39549.541666666664, ' m02 ': 269867.5833333333, ' M30 ': 624050.9500000001, ' M21 ': 4258186.233333333, ' M12 ': 29055722.733333334, ' m03 ': 198262758.70000002, ' mu20 ': 0.027777777778283053, ' mu11 ': -0.01388888888322981, ' mu02 ': 0.027777777810115367, ' mu30 ': -0.003703703638166189, ' mu21 ': 0.0018518519221615293, ' mu12 ': 0.001851847569924292, ' Mu03 ': -0.0037037134170532227, ' nu20 ': 0.11111111111313221, ' nu11 ': -0.05555555553291924, ' nu02 ': 0.11111111124046147 , ' nu30 ': -0.020951311664420796, ' nu21 ': 0.01047565641531008, ' nu12 ': 0.010475631795338369, ' nu03 ':- 0.020951366982159467} -   the #we use this to get the center of gravity . -CX = Int (m['M10']/m['m00']) -cy = Int (m['M01']/m['m00']) - #the results of Contourarea are consistent with the results of m00. +  

Contour Circumference Calculation cv2.arclength ()
1 perimeter = cv2.arclength (cnt,true)

    • The first parameter is a contour
    • The second parameter specifies whether the shape is a closed contour, true is closed, otherwise the curve
Contour Approximation
1 cv2.approxpolydp (curve, Epsilon, closed[, Approxcurve]) →approxcurve

Parameter interpretation
    • Curve: Input 2D points, such as Findcontours obtained contour
    • Epsilon: Accuracy
    • Closed: Is it closed, as it was said before?
The output is approximate after the contour Contour Line Fitting
1 cv2.polylines (IMG, pts, isClosed, color[, thickness[, linetype[, Shift]]) →img

Parameter interpretation
    • IMG: Input Image
    • PTS: A collection of outlines to fit, for example [contours[1],contours[2]]
    • IsClosed: Is it closed, as it was said before?
    • Color: Colors, for example (0,0,255)
    • Thickness: thickness, 1 2 3, etc.
    • Linetype: Type of line
    • Shift: Number of decimal digits in fixed-point coordinates
1 ImportCv2 as CV2 ImportNumPy as NP3img = Cv.imread ("test.jpg", 0)4_,contours,_ = Cv.findcontours (img,2,1)5CNT =Contours[0]6Epsilon = 0.01 * Cv.arclength (cnt,true)#here, use arclength to get contour circumference or curve length .7approx =CV.APPROXPOLYDP (cnt,epsilon,true)8out_img = Cv.polylines (Img,[approx],true, (0,0,255), 2)9Cv.imshow ("Image", out_img)TenK = Cv.waitkey (1) & 0xFF One ifk== 27: ACv.destroyallwindows ()

Convex Packet Inspection
1 cv2.convexhull (points[, hull[, clockwise[, Returnpoints]]) →hull


parameter Interpretation
    • Points: Input 2D point set, such as findcontours obtained contour
    • Hull: Output Convex package
    • clockwise: If true, the output convex hull order is clockwise, otherwise counterclockwise
The function returns a convex hull (point set)
1 ImportCv2 as CV2 ImportNumPy as NP3img = Cv.imread ("test.jpg", 0)4_,contours,_ = Cv.findcontours (img,2,1)5CNT = contours[1]6Hull =cv.convexhull (CNT)7out_img = Cv.polylines (Img,[hull],true, (0,255,255), 2)8Cv.imshow ("Image", out_img)9Cv.waitkey (0)

Bounding Rectangle get the straight bounding rectangle
1 cv2.boundingrect (points) →retval

parameter Interpretation
    • Points: The set of points given to determine the boundary, such as contour
The function returns the four vertex coordinates of the resulting bounding rectangle get the rotated rectangle
1 cv2.minarearect (points) →retval

Parameter description:
    • Points: Findcountours got the Contour
Use
Import Cv2 as CV Import  = Cv.imread ("test.jpg"== = = Cv.minarearect (CNT)# here is the rotation rectangle box = cv.cv.BoxPoints (rect)# Gets the endpoint box = Np.int0 (Box)# rounding down

Minimum closed Circle  
1 cv2.minenclosingcircle (points) →center, radius


 Parameter interpretation
    • Points: Input point set, such as contour
Output to Circle center point coordinates and RADIUS   Ellipse Fitting  
1 cv2.fitellipse (points) →retval


  parameter Interpretation
    • Points: Input point set, such as contour
 The output is an ellipse whose properties have a center point coordinate, a two-axis length, and a deflection angle   Use
1 ImportCv2 as CV2 ImportNumPy as NP3img = Cv.imread ("test.jpg", 0)4_,contours,_ =cv.findcontours (IMG,CV. RETR_LIST,CV. Chain_approx_simple)5CNT =Contours[0]6Circle =cv.minenclosingcircle (CNT)7Ellipse =cv.fitellipse (CNT)8out_1 = Cv.circle (Img,circle, (0,255,255), 2)9Out_2 =cv.ellipse (Img,ellipse, (0,255,255), 2)TenCv.imshow ("IMG1", out_1) OneCv.imshow ("Img2", Out_2) ACv.waitkey (0)

Contour Features Boundary characteristics

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.