Skip to content

fit_rectangle2_contour_xldfit_rectangle2_contour_xldFitRectangle2ContourXldFitRectangle2ContourXldfit_rectangle2_contour_xldfit_rectangle2_contour_xld🔗

Short description🔗

fit_rectangle2_contour_xldfit_rectangle2_contour_xldFitRectangle2ContourXldFitRectangle2ContourXldfit_rectangle2_contour_xldfit_rectangle2_contour_xld — Fit rectangles to XLD contours.

Signature🔗

fit_rectangle2_contour_xld( xld_cont Contours, string Algorithm, integer MaxNumPoints, real MaxClosureDist, integer ClippingEndPoints, integer Iterations, real ClippingFactor, out rectangle2.center.y Row, out rectangle2.center.x Column, out rectangle2.angle.rad Phi, out rectangle2.hwidth Length1, out rectangle2.hheight Length2, out string PointOrder )fit_rectangle2_contour_xld( Contours : : Algorithm, MaxNumPoints, MaxClosureDist, ClippingEndPoints, Iterations, ClippingFactor : Row, Column, Phi, Length1, Length2, PointOrder )void FitRectangle2ContourXld( const HObject& Contours, const HTuple& Algorithm, const HTuple& MaxNumPoints, const HTuple& MaxClosureDist, const HTuple& ClippingEndPoints, const HTuple& Iterations, const HTuple& ClippingFactor, HTuple* Row, HTuple* Column, HTuple* Phi, HTuple* Length1, HTuple* Length2, HTuple* PointOrder )static void HOperatorSet.FitRectangle2ContourXld( HObject contours, HTuple algorithm, HTuple maxNumPoints, HTuple maxClosureDist, HTuple clippingEndPoints, HTuple iterations, HTuple clippingFactor, out HTuple row, out HTuple column, out HTuple phi, out HTuple length1, out HTuple length2, out HTuple pointOrder )def fit_rectangle2_contour_xld( contours: HObject, algorithm: str, max_num_points: int, max_closure_dist: float, clipping_end_points: int, iterations: int, clipping_factor: float ) -> Tuple[Sequence[float], Sequence[float], Sequence[float], Sequence[float], Sequence[float], Sequence[str]]

def fit_rectangle2_contour_xld_s( contours: HObject, algorithm: str, max_num_points: int, max_closure_dist: float, clipping_end_points: int, iterations: int, clipping_factor: float ) -> Tuple[float, float, float, float, float, str]Herror fit_rectangle2_contour_xld( const Hobject Contours, const char* Algorithm, const Hlong MaxNumPoints, double MaxClosureDist, const Hlong ClippingEndPoints, const Hlong Iterations, double ClippingFactor, double* Row, double* Column, double* Phi, double* Length1, double* Length2, char* PointOrder )

Herror T_fit_rectangle2_contour_xld( const Hobject Contours, const Htuple Algorithm, const Htuple MaxNumPoints, const Htuple MaxClosureDist, const Htuple ClippingEndPoints, const Htuple Iterations, const Htuple ClippingFactor, Htuple* Row, Htuple* Column, Htuple* Phi, Htuple* Length1, Htuple* Length2, Htuple* PointOrder )

void HXLDCont::FitRectangle2ContourXld( const HString& Algorithm, Hlong MaxNumPoints, double MaxClosureDist, Hlong ClippingEndPoints, Hlong Iterations, double ClippingFactor, HTuple* Row, HTuple* Column, HTuple* Phi, HTuple* Length1, HTuple* Length2, HTuple* PointOrder ) const

void HXLDCont::FitRectangle2ContourXld( const HString& Algorithm, Hlong MaxNumPoints, double MaxClosureDist, Hlong ClippingEndPoints, Hlong Iterations, double ClippingFactor, double* Row, double* Column, double* Phi, double* Length1, double* Length2, HString* PointOrder ) const

void HXLDCont::FitRectangle2ContourXld( const char* Algorithm, Hlong MaxNumPoints, double MaxClosureDist, Hlong ClippingEndPoints, Hlong Iterations, double ClippingFactor, double* Row, double* Column, double* Phi, double* Length1, double* Length2, HString* PointOrder ) const

void HXLDCont::FitRectangle2ContourXld( const wchar_t* Algorithm, Hlong MaxNumPoints, double MaxClosureDist, Hlong ClippingEndPoints, Hlong Iterations, double ClippingFactor, double* Row, double* Column, double* Phi, double* Length1, double* Length2, HString* PointOrder ) const (Windows only)

void HXLDCont.FitRectangle2ContourXld( string algorithm, int maxNumPoints, double maxClosureDist, int clippingEndPoints, int iterations, double clippingFactor, out HTuple row, out HTuple column, out HTuple phi, out HTuple length1, out HTuple length2, out HTuple pointOrder )

void HXLDCont.FitRectangle2ContourXld( string algorithm, int maxNumPoints, double maxClosureDist, int clippingEndPoints, int iterations, double clippingFactor, out double row, out double column, out double phi, out double length1, out double length2, out string pointOrder )

Description🔗

fit_rectangle2_contour_xldFitRectangle2ContourXld fits rectangles to the rectangular XLD contours given by Contourscontourscontours and returns the parameters of the rectangles in Rowrowrow, Columncolumncolumn (center), Phiphiphi (orientation), as well as Length1length1length_1 and Length2length2length_2 (half edge lengths). The angle Phiphiphi is returned in radians and specifies the angle between the horizontal axis and the edge with the half length Length1length1length_1 in the mathematically positive direction (counterclockwise). In addition, the point order of the contour is returned in PointOrderpointOrderpoint_order. PointOrderpointOrderpoint_order \(=\) 'positive'"positive" means that the contour is traversed in the mathematically positive direction (counterclockwise).

The algorithm used for the fitting of the rectangles can be selected via Algorithmalgorithmalgorithm:

  • 'regression'"regression" Standard least-squares line fitting.

  • 'huber'"huber" Weighted least-squares line fitting, where the impact of outliers is decreased based on the approach of Huber (see below).

  • 'tukey'"tukey" Weighted least-squares line fitting, where outliers are ignored based on the approach of Tukey(see below).

For 'huber'"huber" and 'tukey'"tukey", a robust error statistics is used to estimate the standard deviation of the distances of the contour points from the approximating sides of the rectangle while ignoring outliers. The standard deviation is computed separately for each side of the rectangle to allow the processing of rectangles whose sides are not exactly perpendicular to each other. The parameter ClippingFactorclippingFactorclipping_factor (a scaling factor relative to the standard deviation) controls the amount of outliers: The smaller the value chosen for ClippingFactorclippingFactorclipping_factor the more outliers are detected. The detection of outliers is iterated. The parameter Iterationsiterationsiterations specifies the number of iterations. For Algorithmalgorithmalgorithm \(=\) 'regression'"regression", the values of the last two parameters are ignored. Note that in the approach of Tukey ('tukey'"tukey"), the outliers are removed before performing the approximation and all other points are weighted, whereas in the approach of Huber ('huber'"huber"), the outliers still have a small influence. Particularly, for outliers the optimization is influenced linearly and for points with a smaller distance it is influenced to the square. For the algebraic approach, all distances of the points influence the optimization to the square and thus are not robust against outliers. In practice, the approach of Tukey is recommended.

To reduce the computational load, the fitting of rectangles can be restricted to a subset of the contour points: If a value other than -1-1 is assigned to MaxNumPointsmaxNumPointsmax_num_points, only up to MaxNumPointsmaxNumPointsmax_num_points points, uniformly distributed across the contour, are used.

Depending on the processing used to create Contourscontourscontours, the start and end points of a contour may contain positional errors. Therefore, it is possible to exclude ClippingEndPointsclippingEndPointsclipping_end_points points at the beginning and at the end of a contour from the rectangle fitting.

Contours, for which the distance between their start points and their end points is <= MaxClosureDistmaxClosureDistmax_closure_dist are considered to be closed. For closed contours, the end point of the contour is not used for the rectangle fitting because it would receive twice the weight of the remaining points in the fit.

The fitting of the rectangle to the contour is based on finding the correspondence between the contour points and the four sides of the rectangle. To enable a successful fit, there must be at least one point that lies in the interior of the line segment that represents the respective rectangle side, i.e., the point must not lie at the ends of the line segment. Because of this, at least eight contour points are necessary to fit the rectangle. A point is internally assigned to the side of the rectangle to which it has the minimum distance. For this, the currently optimal rectangle parameters, i.e., the parameters used for the current iteration step, are used internally. If no corresponding points are found for at least one side of the rectangle, the rectangle parameters cannot be determined uniquely. In this case, the error 3266 is returned. Because of this, the caller of fit_rectangle2_contour_xldFitRectangle2ContourXld must ensure that the input contours sufficiently resemble a rectangle. In particular, none of the interior angles of the contour, if the contour was approximated by four lines, should be smaller than 45 degrees or larger than 135 degrees. Because of the assignment of contour points to the closest side of the rectangle this would mean that at least one side of the rectangle would have no corresponding points. Furthermore, ClippingFactorclippingFactorclipping_factor should not be chosen too small to avoid that the outlier suppression creates rectangle sides without corresponding contour points. This can only happen for Algorithmalgorithmalgorithm \(=\) 'tukey'"tukey". If the above conditions are observed, fit_rectangle2_contour_xldFitRectangle2ContourXld returns highly accurate rectangle parameters. If the outlier suppression according to Tukey is used, fit_rectangle2_contour_xldFitRectangle2ContourXld can be used to robustly fit rectangles, e.g., to rectangular contours with rounded corners.

Execution information🔗

Execution information
  • Multithreading type: reentrant (runs in parallel with non-exclusive operators).

  • Multithreading scope: global (may be called from any thread).

  • Automatically parallelized on tuple level.

Parameters🔗

Contourscontourscontours (input_object) xld_cont(-array) → objectobjectHObjectHXLDContHObjectHobject

Input contours.

Algorithmalgorithmalgorithm (input_control) string → (string)(string)HTuple (HString)HTuple (string)strHtuple (char*)

Algorithm for fitting the rectangles.

Default: 'regression'"regression"
List of values: 'huber', 'regression', 'tukey'"huber", "regression", "tukey"

MaxNumPointsmaxNumPointsmax_num_points (input_control) integer → (integer)(integer)HTuple (Hlong)HTuple (int / long)intHtuple (Hlong)

Maximum number of contour points used for the computation (-1 for all points).

Default: -1-1
Restriction: MaxNumPoints == -1 || MaxNumPoints >= 8

MaxClosureDistmaxClosureDistmax_closure_dist (input_control) real → (real)(real)HTuple (double)HTuple (double)floatHtuple (double)

Maximum distance between the end points of a contour to be considered as closed.

Default: 0.00.0
Restriction: MaxClosureDist >= 0.0

ClippingEndPointsclippingEndPointsclipping_end_points (input_control) integer → (integer)(integer)HTuple (Hlong)HTuple (int / long)intHtuple (Hlong)

Number of points at the beginning and at the end of the contours to be ignored for the fitting.

Default: 00
Suggested values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Restriction: ClippingEndPoints >= 0

Iterationsiterationsiterations (input_control) integer → (integer)(integer)HTuple (Hlong)HTuple (int / long)intHtuple (Hlong)

Maximum number of iterations (not used for 'regression'"regression").

Default: 33
Restriction: Iterations >= 0

ClippingFactorclippingFactorclipping_factor (input_control) real → (real)(real)HTuple (double)HTuple (double)floatHtuple (double)

Clipping factor for the elimination of outliers (typical values: 1.01.0 for 'huber'"huber" and 2.02.0 for 'tukey'"tukey").

Default: 2.02.0
Suggested values: 1.0, 1.5, 2.0, 2.5, 3.01.0, 1.5, 2.0, 2.5, 3.0
Restriction: ClippingFactor > 0

Rowrowrow (output_control) rectangle2.center.y(-array) → (real)(real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

Row coordinate of the center of the rectangle.

Columncolumncolumn (output_control) rectangle2.center.x(-array) → (real)(real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

Column coordinate of the center of the rectangle.

Phiphiphi (output_control) rectangle2.angle.rad(-array) → (real)(real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

Orientation of the main axis of the rectangle [rad].

Length1length1length_1 (output_control) rectangle2.hwidth(-array) → (real)(real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

First radius (half length) of the rectangle.

Length2length2length_2 (output_control) rectangle2.hheight(-array) → (real)(real)HTuple (double)HTuple (double)Sequence[float]Htuple (double)

Second radius (half width) of the rectangle.

PointOrderpointOrderpoint_order (output_control) string(-array) → (string)(string)HTuple (HString)HTuple (string)Sequence[str]Htuple (char*)

Point order of the contour.

List of values: 'negative', 'positive'"negative", "positive"

Result🔗

fit_rectangle2_contour_xldFitRectangle2ContourXld returns 2 (H_MSG_TRUE) if all parameter values are correct, and rectangles could be fitted to the input contours. If the input is empty the behavior can be set via set_system('no_object_result',<Result>). If necessary, an exception is raised. If the parameter ClippingFactorclippingFactorclipping_factor is chosen too small, i.e., all points are classified as outliers, the error 3266 is raised. If no points could be found for at least one side of the rectangle, the error 3266 is raised as well.

Combinations with other operators🔗

Combinations

Possible predecessors

gen_contours_skeleton_xldGenContoursSkeletonXld, lines_gaussLinesGauss, lines_facetLinesFacet, edges_sub_pixEdgesSubPix, smooth_contours_xldSmoothContoursXld, union_collinear_contours_xldUnionCollinearContoursXld, union_collinear_contours_ext_xldUnionCollinearContoursExtXld, union_adjacent_contours_xldUnionAdjacentContoursXld

Possible successors

dist_rectangle2_contour_points_xldDistRectangle2ContourPointsXld, gen_rectangle2_contour_xldGenRectangle2ContourXld

Alternatives

smallest_rectangle2_xldSmallestRectangle2Xld

See also

fit_line_contour_xldFitLineContourXld, fit_circle_contour_xldFitCircleContourXld, fit_ellipse_contour_xldFitEllipseContourXld

Module🔗

Foundation