Skip to content
Search

Cv2.minAreaRect

lastmod 2025-01-06created 2022-12-2114 edits

Finds a rotated "rectangle" of the minimum area enclosing the input 2D point set (Contour).

This function calculates and returns the minimum-area bounding rectangle (possibly rotated) for the specified point set.

Developers should keep in mind that the returned RotatedRect can contain negative indices when the data is close to the containing Mat element boundary ("Mat element boundary").1 <- What does this mean?

Return Value

((CenterX, CenterY), (Width, Height), AngleOfRotation) = cv2.minAreaRect(points)

Angle of Rotation

Cv2_minAreaRect_sample_.gif

To track the rectangle's vertex indices, you need to use the Nearest neighbor search method.

Problems with rectangle inference

If it is not an exact rectangular shape and the area near a vertex is slightly tilted, it is captured as a rhombus rather than a rectangle, as follows.

53-20240229_091836-preview.png
54-20240229_091840-preview.png

Solved by using MinAreaQuadrilateral, an algorithm improved with the Hough transform (Korean: 허프 변환). Applied in the ddrm project.

Also uses the QuadrilateralRotateTracer algorithm to track rotation.

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.

See also

  • MinAreaQuadrilateral - An algorithm that addresses the problems of cv2.minAreaRect using the Hough transform (Korean: 허프 변환) (used in ddrm).
  • QuadrilateralRotateTracer - Quadrilateral rotation tracker (used in ddrm).

Favorite site

References


  1. The original text is "Developer should keep in mind that the returned RotatedRect can contain negative indices when data is close to the containing Mat element boundary." ↩

author Zer0 sitemap