Hello,

I have a project where I need to implement a measurement caliper tool in OpenCV C++ which should replicate a measuring tool in Halcon. It uses mainly the functions ‘gen_measure_rectangle2’ and ‘measure_pairs’ from HALCON. If any of you could tell me if at all this is possible or if there is something in OpenCV that can help me achieve this, I would be really grateful.

I have checked general edge detection operators already in OpenCV. But I want something exactly like in HALCON. Any help or suggestions would be much appreciated.

The HALCON documentation for gen_measure_rectangle2 is :

**## Signature**

**gen_measure_rectangle2** ( : : [ *Row* ] [ *Column* ], [ *Phi* ], [ *Length1* ], [ *Length2* ], [ *Width* ], [ *Height* ], [ *Interpolation* ] : [ *MeasureHandle* ]

**## Description**

gen_measure_rectangle2 prepares the extraction of *straight* edges which lie *perpendicular* to the major axis of a rectangle. The center of the rectangle is passed in the parameters [ *Row* ] and [ *Column* ], the direction of the major axis of the rectangle in [ *Phi* ], and the length of the two axes, i.e., half the diameter of the rectangle, in [ *Length1* ] and [ *Length2* ].

The edge extraction algorithm is described in the documentation of the operator [measure_pos]. As discussed there, different types of interpolation can be used for the calculation of the one-dimensional gray value profile. For [ *Interpolation* ] = *‘nearest_neighbor’* , the gray values in the measurement are obtained from the gray values of the closest pixel, i.e., by constant interpolation. For [ *Interpolation* ] = *‘bilinear’* , bilinear interpolation is used, while for [ *Interpolation* ] = *‘bicubic’* , bicubic interpolation is used.

To perform the actual measurement at optimal speed, all computations that can be used for multiple measurements are already performed in the operator gen_measure_rectangle2. For this, an optimized data structure, a so-called measure object, is constructed and returned in [ *MeasureHandle* ]. The size of the images in which measurements will be performed must be specified in the parameters [ *Width* ] and [ *Height* ]

The system parameter *‘int_zooming’* (see [set_system] affects the accuracy and speed of the calculations used to construct the measure object. If *‘int_zooming’* is set to *‘true’* , the internal calculations are performed using fixed-point arithmetic, leading to much shorter execution times. However, the geometric accuracy is slightly lower in this mode. If *‘int_zooming’* is set to *‘false’*, the internal calculations are performed using floating point arithmetic, leading to the maximum geometric accuracy, but also to significantly increased execution times.

measure_pairs :

**##Signature**

**measure_pairs**([Image] : : [MeasureHandle], [Sigma], [Threshold], [Transition], [Select] : [RowEdgeFirst], [ColumnEdgeFirst], [AmplitudeFirst], [RowEdgeSecond], [ColumnEdgeSecond], [AmplitudeSecond], [IntraDistance], [InterDistance])

**##Description**

measure_pairs serves to extract straight edge pairs which lie perpendicular to the major axis of a rectangle or annular arc.

The extraction algorithm is identical to [measure_pos]. In addition the edges are grouped to pairs: If [Transition] = ‘positive’, the edge points with a dark-to-light transition in the direction of the major axis of the rectangle are returned in [RowEdgeFirst] and [ColumnEdgeFirst]. In this case, the corresponding edges with a light-to-dark transition are returned in [RowEdgeSecond] and [ColumnEdgeSecond]. If [Transition] = ‘negative’, the behavior is exactly opposite. If [Transition] = ‘all’, the first detected edge defines the transition for [RowEdgeFirst] and [ColumnEdgeFirst]. I.e., dependent on the positioning of the measure object, edge pairs with a light-dark-light transition or edge pairs with a dark-light-dark transition are returned. This is suited, e.g., to measure objects with different brightness relative to the background.

If more than one consecutive edge with the same transition is found, the first one is used as a pair element. This behavior may cause problems in applications in which the threshold [Threshold] cannot be selected high enough to suppress consecutive edges of the same transition. For these applications, a second pairing mode exists that only selects the respective strongest edges of a sequence of consecutive rising and falling edges. This mode is selected by appending ‘_strongest’ to any of the above modes for [Transition], e.g., ‘negative_strongest’. Finally, it is possible to select which edge pairs are returned. If [Select] is set to ‘all’, all edge pairs are returned. If it is set to ‘first’, only the first of the extracted edge pairs is returned, while it is set to ‘last’, only the last one is returned.

The extracted edges are returned as single points which lie on the major axis of the rectangle. The corresponding edge amplitudes are returned in [AmplitudeFirst] and [AmplitudeSecond]. In addition, the distance between each edge pair is returned in [IntraDistance] and the distance between consecutive edge pairs is returned in [InterDistance]. Here, IntraDistance[i] corresponds to the distance between EdgeFirst[i] and EdgeSecond[i], while InterDistance[i] corresponds to the distance between EdgeSecond[i] and EdgeFirst[i+1], i.e., the tuple [InterDistance] contains one element less than the tuples of the edge pairs.

Thank you!!!