Cv2.findContours
lastmod 2025-02-18created 2022-07-125 edits
Detects contours.
OpenCV Contours APIs
- Contours
- OpenCV:Contours
- cv2.approxPolyDP - Approximates (simplifies) a polygonal curve with the specified precision.
- cv2.arcLength - Calculates a contour perimeter or a curve length.
- cv2.boundingRect - Calculates the upper-right bounding rectangle of a point set or the non-zero pixels of a grayscale image.
- cv2.boxPoints - Finds the four vertices of a rotated rectangle. Useful for drawing the rotated rectangle.
- cv2.connectedComponents - Computes the connected components labeled image of a boolean image.
- cv2.connectedComponentsWithStats - Computes the connected components labeled image of a boolean image and also produces a statistics output for each label.
- cv2.contourArea - Calculates a contour area.
- cv2.convexHull - Finds the convex hull of a point set.
- cv2.convexityDefects - Finds the convexity defects of a contour.
- cv2.createGeneralizedHoughBallard - Creates a smart pointer to the cv2.GeneralizedHoughBallard class and initializes it.
- cv2.createGeneralizedHoughGuil - Creates a smart pointer to the cv2.GeneralizedHoughGuil class and initializes it.
- cv2.drawContours - Draws contours.
- cv2.findContours - Finds contours in a binary image.
- cv2.fitEllipse - Fits an ellipse around a set of 2D points.
- cv2.fitEllipseAMS - Fits an ellipse around a set of 2D points.
- cv2.fitEllipseDirect - Fits an ellipse around a set of 2D points.
- cv2.fitLine - Fits a line to a 2D or 3D point set.
- cv2.HuMoments - Calculates seven Hu invariants.
- cv2.intersectConvexConvex - Finds the intersection of two convex polygons.
- cv2.isContourConvex - Tests a contour's convexity.
- cv2.matchShapes - Compares two shapes.
- cv2.minAreaRect - Finds a rotated rectangle of the minimum area enclosing the input 2D point set.
- cv2.minEnclosingCircle - Finds a circle of the minimum area enclosing a 2D point set.
- cv2.minEnclosingTriangle - Finds a triangle of the minimum area enclosing a 2D point set and returns its area.
- cv2.moments - Calculates all of the moments up to the third order of a polygon or rasterized shape.
- cv2.pointPolygonTest - Performs a point-in-contour test.
- cv2.rotatedRectangleIntersection - Checks whether there is any intersection between two rotated rectangles.
Parameters
-
mode - Used to specify what kind of contours to find or how to represent the result.
-
CV_RETR_EXTERNAL- Detects only the outermost contours. -
CV_RETR_LIST- Retrieves all contours and puts them in a list. -
CV_RETR_CCOMP- Retrieves all contours and organizes them into a two-level hierarchy. (The outer contours of the components are managed as a linked list, and the second level manages the inner contours of the components as a list.) -
CV_RETR_TREE- Retrieves all contours and builds the full hierarchy of the contours.
-
-
method - Specifies how to approximate the contours
-
CV_CHAIN_CODE- Generates a Freeman chain code. -
CV_CHAIN_APPROX_NONE- Converts every point of the chain code into a vertex. -
CV_CHAIN_APPROX_SIMPLE- Stores only the end points of horizontal, vertical, and diagonal segments. -
CV_CHAIN_APPROX_TC89_L1,CV_CHAIN_APPROX_TC89_KCOS- Applies one of the Teh-Chin chain approximation algorithms. -
CV_LINK_RUNS- ??
-
Example
import cv2
# 이미지를 그레이스케일로 읽어들입니다.
image = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
# 이미지를 이진화합니다.
ret, thresh = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
# 윤곽선을 찾습니다.
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# 찾은 윤곽선을 이미지에 그립니다.
cv2.drawContours(image, contours, -1, (0, 255, 0), 2)
# 결과 이미지를 출력합니다.
cv2.imshow('Contours', image)
cv2.waitKey(0)
cv2.destroyAllWindows()