Skip to content
Search

OpenCV:Contours

lastmod 2024-03-07created 2022-06-1414 edits

ROI to Contour

from numpy import array, int32, ndarray

assert data is not None
assert isinstance(data, (list, tuple))
assert len(data) == 4
x1 = data[0]
y1 = data[1]
x2 = data[2]
y2 = data[3]
points = (x1, y1), (x2, y1), (x2, y2), (x1, y2), (x1, y1)
raw = [p for xy in points for p in xy]
contour = array(raw, dtype=int32).reshape((5, 1, 2))

assert isinstance(contour, ndarray)
assert len(contour.shape) == 3
assert contour.shape[0] == 5
assert contour.shape[1] == 1
assert contour.shape[2] == 2

Contour intersection

How to find the intersection area of two contours.

Drawing onto images bit by bit and then computing with a Locical AND operation

import numpy as np # just for matrix manipulation, C/C++ use cv::Mat
# find contours.
contours,h = findContours( img, mode=RETR_LIST, method=CHAIN_APPROX_SIMPLE )
# Suppose this has the contours of just the car and the obstacle.

# create an image filled with zeros, single-channel, same size as img.
blank = np.zeros( img.shape[0:2] )

# copy each of the contours (assuming there's just two) to its own image. 
# Just fill with a '1'.
img1 = drawContours( blank.copy(), contours, 0, 1 )
img2 = drawContours( blank.copy(), contours, 1, 1 )

# now AND the two together
intersection = np.logical_and( img1, img2 )

# OR we could just add img1 to img2 and pick all points that sum to 2 (1+1=2):
intersection2 = (img1+img2)==2

The method above is not exact, since floating-point precision is lost.

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 up-right bounding rectangle of a point set or non-zero pixels of a gray-scale 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 and initializes a smart pointer to the cv2.GeneralizedHoughBallard class.
  • cv2.createGeneralizedHoughGuil - Creates and initializes a smart pointer to the cv2.GeneralizedHoughGuil class.
  • 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 contour 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 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 - Finds out if there is any intersection between two rotated rectangles.

Convex Hull

  • cv2.convexHull - Returns the convex hull (connecting the outermost points) from the given points.
  • cv2.convexityDefects - Returns the convexity defects from the given points and convex hull.
  • cv2.isContourConvex - Checks whether it is convex.

See also

Favorite site

author Zer0 sitemap