Is there a function in OpenCV that is similar to 'gen_measure_rectangle2' in halcon?

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!!!

welcome.

could you show what this actually does?

in particular, how does the choice of length1 and length2 affect results? how do results vary when phi is changed a little? I’m not asking for words, I’m asking for pictures.

!

Unfortunately, since I am a new user, I am able to add only one picture!

I hope the picture gives an idea what the operator does. The values of length1, length2, row1, column1 and phi1 and the graphics window shows the result and depending on the changing values for the rectangle, the number of pins identified also changes. The values of the variables can be seen below the ic_pin image. Basically, gen_measure_rectangle2 creates a measure handle for the rectangle whose dimensions we provide. And using this measure handle, the measure_pairs operator counts the edges perpendicular to the longest side of the rectangle along with its coordinates. This is the operation I want to mimic with OpenCV in C++. If you need any more clarification, I’d be glad to provide it. Any help is appreciated. Thanks in advance.

Another example

!

okay, I see what it’s doing now. I also browsed their docs for some crumbs.

let’s stick to “rectangles”. you’d construct an affine transformation matrix (really just rotation and translation, getRotationMatrix2D) and then use warpAffine to take (interpolated) point samples from the source image, into that sample grid rectangle.

then you flatten it along the short dimension (sum or mean). that gives you the 1-D profile they talk about in their documentation.

then you’ll need to calculate the gradient of that profile.

then you find the local extrema (maxima/minima) of that gradient, and add sub-pixel precision using another interpolation, e.g. “fitting” a parabola to the extremum and its neighboring values, and take the apex of that parabola for the “true” sub-pixel extremum.

when you have those points/values, they’re all relative to the rectangle, so you’d map them back into the source coordinate frame.

1 Like

Hello, Thank you so much for your response. But could you be a little more elaborate on what you mean by “use warpAffine to take (interpolated) point samples from the source image, into that sample grid rectangle”.

We use warpAffine to shift the image based on the translation and rotation matrix correct?? So do you mean we first construct a rectangle and then construct a warpmatrix based on a rectangle and the source image and do affine transform on a source image?? Or am I missing something here. ?

yes. the purpose of using warpAffine here is to extract a resampled patch in the shape of the defined rectangle, like so:

image

related:

1 Like