11.2   Working with Tuples

A strength of HALCON is that most operators automatically work with multiple input values (tuple values). For example, you can call the operator AreaCenter with a single or with multiple input regions; the operator automatically returns the area and center coordinates of all passed regions. Analogously, if you call GenRectangle1 with multiple values for the rectangle coordinates, it creates multiple regions.

The following sections provide more detailed information about

11.2.1   Calling HALCON Operators with Single or Multiple Values

You can check whether an operator also works with tuples in the reference manual. Below, e.g., we show the relevant parts of the operators AreaCenter and GenRectangle1.

As you see, the iconic classes like HRegion automatically handle multiple values; whether such a parameter accepts / returns multiple values is not visible from the signature but only in the parameter section: Here, an appended (-array) (in the example: HRegion(-array)) signals that the parameter can contain a single or multiple values.

static void HOperatorSet.AreaCenter
(HObject regions, out HTuple area, out HTuple row, out HTuple column)

HTuple HRegion.AreaCenter
(out HTuple row, out HTuple column)

int HRegion.AreaCenter
(out double row, out double column)

regions (input_object)region(-array) → HRegion
area (output_control)integer(-array) → HTuple (int / long)
row (output_control)point.y(-array) → HTuple (double)
column (output_control)point.x(-array) → HTuple (double)

static void HOperatorSet.GenRectangle1
(out HObject rectangle, HTuple row1, HTuple column1, HTuple row2, HTuple column2)

public HRegion
(HTuple row1, HTuple column1, HTuple row2, HTuple column2)

public HRegion
(double row1, double column1, double row2, double column2)

void HRegion.GenRectangle1
(HTuple row1, HTuple column1, HTuple row2, HTuple column2)

void HRegion.GenRectangle1
(double row1, double column1, double row2, double column2)

rectangle (output_object)region(-array) → HRegion
row1 (input_control)rectangle.origin.y(-array) → HTuple (double / int / long)
column1 (input_control)rectangle.origin.x(-array) → HTuple (double / int / long)
row2 (input_control)rectangle.corner.y(-array) → HTuple (double / int / long)
column2 (input_control)rectangle.corner.x(-array) → HTuple (double / int / long)

In contrast, control parameters show by their data type whether they contain a single or multiple values: In the first case, they use basic data types like double, in the second case the HALCON/.NET class HTuple. Thus, you can call GenRectangle1 via HRegion in two ways, either by passing doubles or HTuple s (here using the constructor form):

HRegion SingleRegion = new HRegion(10.0, 10.0, 50.0, 50.0);
HRegion MultipleRegions = new HRegion(new HTuple(20.0, 30.0), new HTuple(20.0, 30.0),
                                      new HTuple(60.0, 70.0), new HTuple(60.0, 70.0));

Similarly, AreaCenter can be called in two ways:

double Area, Row, Column;
HTuple Areas, Rows, Columns;

Area = SingleRegion.AreaCenter(out Row, out Column);
Areas = MultipleRegions.AreaCenter(out Rows, out Columns);

Below, we provide additional information about iconic tuples (section “Iconic Tuples”) and control tuples (section “Control Tuples and the Class HTuple”).

11.2.2   Iconic Tuples

The iconic classes HImage, HRegion, and HXLD can contain single or multiple objects. To process all elements of a tuple you first query the number of elements with the operator CountObj

int NumRegions = MultipleRegions.CountObj();

and then access elements either with the HALCON operator SelectObj or (when using C#) with the operator []:

for (int i=1; i<=NumRegions; i++)
{
  HRegion Region = MultipleRegions[i];
  ...
}

Note that in iconic tuples element indices start with 1!

You can create or extend iconic tuples with the HALCON operator ConcatObj:

HRegion ThreeRegions = SingleRegion.ConcatObj(MultipleRegions);

11.2.3   Control Tuples and the Class HTuple

For control tuples, HALCON/.NET provides the class HTuple. Instances of HTuple can contain elements of the types double, int, string, and HHandle. They can also contain a mixture of element types.

The following sections describe

11.2.3.1   Accessing Tuple Elements

To process all elements of a tuple, you first query its length via the property Length:

int TupleLength = Areas.Length;

You can access tuple elements with the operator []:

for (int i=0; i<TupleLength; i++)
{
  double Element = Areas[i];
  ...
}

Note that you get an exception if you try to read a non-existing tuple element or if you try to assign an element to a variable with a different type without cast.

11.2.3.2   Creating Tuples

The class HTuple provides many different constructors (see the Visual Studio's Object Browser for a list). The following line creates an int tuple with a single value:

HTuple Tuple1 = new HTuple(1);

In contrast, the following line creates a double tuple:

HTuple Tuple2 = new HTuple(1.0);

You can also pass multiple values to a constructor. Note that when mixing double and int values as in the following line, a double tuple is created:

HTuple Tuple3 = new HTuple(1.0, 2);

In contrast, when the list of values also contains a string, a mixed type tuple is created, in which the second value is stored as an int:

HTuple Tuple4 = new HTuple(1.0, 2, "s");

The type of a tuple or of a tuple element can be queried via its property Type:

HTupleType TupleType = Tuple4.Type;
HTupleType TupleElementType = Tuple4[1].Type;

You can concatenate tuples very simply by passing them in a constructor:

HTuple Tuple5 = new HTuple(Tuple2, Tuple3);

You can also append elements to a tuple by writing into a non-existing element:

Tuple3[2] = 3;

11.2.3.3   Casts, Ambiguities, Unexpected Results

The class HTuple provides many implicit cast methods so that you can intuitively use the basic data types in most places. For example, the line

double Element = Areas[i];

automatically casts the element, which is in fact an instance of the class HTupleElement, into a double.

Similarly, basic types are automatically casted into instances of HTuple. The drawback of the casts is that the compiler often cannot decide whether you want to use the simple or the tuple version of an operator and issues a corresponding error. For example, if you used the following line, the values can either be casted from int to double or to HTuple:

// HRegion SingleRegion = new HRegion(10, 10, 50, 50);

You can resolve the ambiguity very simply by appending .0 to the first parameter:

HRegion SingleRegion = new HRegion(10.0, 10.0, 50.0, 50.0);

The example Matching contains two other cases of ambiguities, both arising because basic-type and HTuple parameters are mixed in the same call. In the first, the ambiguity is solved by explicitly casting the double parameters into instances of HTuple:

private double                        Row, Column;
HTuple RowCheck, ColumnCheck, AngleCheck, Score;
HHomMat2D Matrix = new HHomMat2D();
Matrix.VectorAngleToRigid(new HTuple(Row), new HTuple(Column), new HTuple(0.0),
  RowCheck, ColumnCheck, AngleCheck);

In the second case, the instances of HTuple (which only contain single values) are explicitly casted into doubles by using the property D, which returns the value of the first element as a double (actually, it is a shortcut for tuple[0].D):

private double                        RectPhi, RectLength1, RectLength2;
HTuple Rect1RowCheck, Rect1ColCheck;
Rectangle1.GenRectangle2(Rect1RowCheck.D, Rect1ColCheck.D,
  RectPhi + AngleCheck.D,
  RectLength1, RectLength2);

With similar properties, you can cast tuple elements into the other basic types. Note, however, that you get an exception if you try to cast an element into a “wrong” type.

In contrast to input parameters, output parameters are not automatically casted. Sometimes, this leads to unexpected results. In the following code, e.g., doubles are used for the output parameters and the return value in a call to AreaCenter with a tuple of regions:

HRegion MultipleRegions = new HRegion(new HTuple(20.0, 30.0), new HTuple(20.0, 30.0),
                                      new HTuple(60.0, 70.0), new HTuple(60.0, 70.0));
double Area, Row, Column;
HTuple Areas, Rows, Columns;

Area = MultipleRegions.AreaCenter(out Row, out Column);

Consequently, only the area and the center of the first region are returned. The same happens if you assign the return value to an HTuple, but still pass doubles for the output parameters:

Areas = MultipleRegions.AreaCenter(out Row, out Column);

In contrast, if you pass HTuple s for the output parameters and assign the return value to a double, the operator returns the center coordinates of all regions but only the area of the first region:

Area = MultipleRegions.AreaCenter(out Rows, out Columns);

11.2.3.4   HALCON Operators for Processing Tuples

HALCON provides many operators for processing tuples. In the reference manual, these operators can be found in the chapter Tuple. An overview of these operators is given in the HDevelop User's Guide. Note that instead of the operator name, the name of the corresponding HDevelop function is used, which omits the Tuple and uses lowercase characters, e.g., rad instead of TupleRad.

11.2.3.5   Operator Overloads

For the basic arithmetic operations, HALCON/.NET provides operator overloads. For example, the operator + automatically calls the HALCON operator TupleAdd.