How a HALCON operator can be called via the HALCON/C++ interface is described in detail in the HALCON operator reference manual. As an example, figure 5.1 shows parts of the entry for the operator MeanImage.
|
|
| Image (input_object) | (multichannel-)image(-array) → HImage (byte / int2 / uint2 / int4 / int8 / real / vector_field) |
| ImageMean (output_object) | (multichannel-)image(-array) → HImage (byte / int2 / uint2 / int4 / int8 / real / vector_field) |
| MaskWidth (input_control) | extent.x → HTuple (Hlong) |
| MaskHeight (input_control) | extent.y → HTuple (Hlong) |
Figure 5.1: The head and parts of the parameter section of the reference manual entry for mean_image.
Please note that the reference manual does not list all possible signatures of the operators. A complete list can be found in the file include\halconcpp\HOperatorSet.h.
Below, we
HALCON distinguishes two types of parameters: iconic and control parameters. Iconic parameters are related to the original image (images, regions, XLD objects), whereas control parameters are values such as integers, floating-point numbers, strings, or handles.
A special form of control parameters are the so-called handles. A well-known representative of this type is the window handle, which provides access to an opened HALCON window, e.g., to display an image in it. Besides, handles are used when operators share complex data, e.g., the operators for shape-based matching which create and then use the model data, or for accessing input/output devices, e.g., image acquisition devices. Classes encapsulating handles are described in detail in section “Classes Encapsulating Handles”.
Both iconic and control parameters can appear as input and output parameters of a HALCON operator. For example, the operator MeanImage expects one iconic input parameter, one iconic output parameter, and two input control parameters (see figure 5.1); figure 5.2 shows an operator which has all four parameter types. Note how some parameters “disappear” from within the parentheses if you call an operator via a class; this mechanism is described in more detail in section “Calling Operators via Classes”.
An important concept of HALCON's philosophy regarding parameters is that input parameters are not modified by an operator. As a consequence, they are passed by value (e.g., Hlong MaskWidth in figure 5.1) or via a constant reference (e.g., const HObject& Image). This philosophy also holds if an operator is called via a class, with the calling instance acting as an input parameter. Thus, in the following example code the original image is not modified by the call to MeanImage; the operator's result, i.e., the smoothed image, is provided via the return value instead:
HImage original_image("monkey");
HImage smoothed_image = original_image.MeanImage(11, 11);
|
|
| Image (input_object) | singlechannelimage → HImage (byte / uint2) |
| SymbolRegions (output_object) | region(-array) → HRegion |
| BarCodeHandle (input_control) | barcode → HTuple (HHandle) |
| CodeType (input_control) | string(-array) → HTuple (HString) |
| DecodedDataStrings (output_control) | string(-array) → HTuple (HString) |
Figure 5.2: The head and parts of the parameter section of the reference manual entry for find_bar_code.
In contrast to input parameters, output parameters are always modified, thus they must be passed by reference. Note that operators expect a pointer to an already existing variable or class instance! For example, when calling the operator FindBarCode as in the following lines of code, variables of the class HTuple are declared before passing the corresponding pointers using the operator &.
HImage image("barcode/ean13/ean1301");
HBarCode barcode(HTuple(), HTuple());
HString result;
HRegion code_region = barcode.FindBarCode(image, "EAN-13", &result);
The above example shows another interesting aspect of output parameters: When calling operators via classes, one output parameter may become the return value (see section “Calling Operators via Classes” for more details); in the example, FindBarCode returns the bar code region.
Many HALCON operators accept more than one value for certain parameters. For example, you can call the operator MeanImage with an array of images (see figure 5.1); then, an array of smoothed images is returned. This is called the tuple mode; see section “The Tuple Mode” for more information.
|
|
| Name (input_control) | string → HTuple (HString) |
| Query (input_control) | string → HTuple (HString) |
| Information (output_control) | string → HTuple (HString) |
| ValueList (output_control) | string-array → HTuple (HString / Hlong / double) |
Figure 5.3: The head and parts of the parameter section of the reference manual entry for info_framegrabber.
Regardless of the encoding of the HALCON library (set_system('filename_encoding', ...)) the HALCON/C++ interface expects raw char pointer strings that are passed to HALCON operators and to HTuple or HString instances to be UTF-8 encoded.
Output strings are always of type HString with automatic memory management. These strings are by default also UTF-8 encoded. The encoding of the HALCON/C++ interface (interface encoding) can be changed to local-8-bit encoding via a call of HalconCpp::SetHcppInterfaceStringEncodingIsUtf8(false). The current interface encoding can be requested via HalconCpp::IsHcppInterfaceStringEncodingUtf8(). It is not recommended to switch the interface encoding back and forth. The setting should be adjusted only once at the very beginning of the program (before the first HALCON operator or assignment), because HTuple instances can not store in which encoding the contained strings are present, i.e, for all write and read accesses, the same encoding must be set. Furthermore, the interface encoding is set globally and is therefore not suitable for multithreading programs: Changing the setting in one thread has an effect on other threads.
In the following example code, the operator InfoFramegrabber (see also figure 5.3) is called with two output string parameters to query the currently installed image acquisition board:
HString sInfo, sValue; InfoFramegrabber(FGName, "info_boards", &sInfo, &sValue);
Note that it is also not necessary to allocate memory for multiple output string parameters returned as HTuple:
HTuple tInfo, tValues; InfoFramegrabber(FGName, "info_boards", &tInfo, &tValues);
As already described in the previous section, the HALCON/C++ reference manual shows via which classes an operator can be called. For example, FindBarCode can be called via objects of the class HImage or HBarCode (see figure 5.2). In both cases, the corresponding input parameter (Image or BarCodeHandle, respectively) does not appear within the parentheses anymore as it is replaced by the calling instance of the class (this).
There is a further difference to the procedural operator signature: The first output parameter (in the example the bar code region SymbolRegions) also disappears from within the parentheses and becomes the return value instead of the error code (more about error handling can be found in section “Error Handling”).
HImage image("barcode/ean13/ean1301");
HBarCode barcode(HTuple(), HTuple());
HString result;
HRegion code_region = barcode.FindBarCode(image, "EAN-13", &result);
HRegion code_region = image.FindBarCode(barcode, "EAN-13", &result);
HObject image; HTuple barcode; HObject code_region; HTuple result; ReadImage(&image, "barcode/ean13/ean1301"); CreateBarCodeModel(HTuple(), HTuple(), &barcode); FindBarCode(image, &code_region, barcode, "EAN-13", &result);
Figure 5.4: Using FindBarCode via HBarCode, via HImage, or in the procedural approach.
Figure 5.4 depicts code examples for the three ways to call FindBarCode. When comparing the object-oriented and the procedural approach, you can see that the calls to the operators ReadImage and CreateBarCodeModel are replaced by special constructors for the classes HImage and HBarCode, respectively. This topic is discussed in more detail below.
As can be seen in figure 5.4, the HALCON/C++ parameter classes provide additional constructors, which are based on suitable HALCON operators. The constructors for HImage and HBarCode used in the example are based on ReadImage and CreateBarCodeModel, respectively.
As a rule of thumb: If a class appears only as an output parameter in an operator, there automatically exists a constructor based on this operator. Thus, instances of HBarCode can be constructed based on CreateBarCodeModel as shown in figure 5.4, instances of HShapeModel based on CreateShapeModel, instances of HFramegrabber based on OpenFramegrabber and so on. Note that for classes where many such operators exist (e.g., HImage), only a subset of commonly used operators with unambiguous parameter list are actually used as constructor.
In addition, all classes have empty constructors to create an uninitialized object. For example, you can create an instance of HBarCode with the default constructor and then initialize it using CreateBarCodeModel as follows:
HBarCode barcode; barcode.CreateBarCodeModel(HTuple(), HTuple());
If the instance was already initialized, the corresponding data structures are automatically destroyed before constructing and initializing them anew (see also section “Destructors and Halcon Operators”). The handle classes are described in more detail in section “Other Handle Classes”.
HImage image; // still uninitialized image.ReadImage("clip");
Below we take a brief look at the most important classes. A complete and up-to-date list of available constructors can be found in the HALCON operator reference and the corresponding header files in %HALCONROOT%\include\cpp.
Of course, you can close a window using CloseWindow and then open it again using OpenWindow. In contrast to the iconic parameter classes, you can call the “constructor-like” operator OpenWindow via an instance of HWindow in the intuitive way, i.e., the calling instance is modified; in addition the corresponding handle is returned. HWindow is described in more detail in section “Windows”.
All HALCON/C++ classes provide default destructors which automatically free the corresponding memory.
The default destructors of classes encapsulating handles, e.g., HShapeModel or HFramegrabber, work similar to members like ClearShapeModel or CloseFramegrabber, respectively.
There is no need to call these operators as you can initialize instances anew as described in section “Constructors and Halcon Operators”.
Basically, we differentiate between destroying a handle and destroying the underlying data structure. The data structure can be destroyed in two ways: Automatically as soon as the last reference to the data structure has been deleted. Explicitly by calling an operator, e.g., CloseWindow. Explicit destruction invalidates references, but access is secure.
As already mentioned in section “A Closer Look at Parameters”, many HALCON operators can be called in the so-called tuple mode. In this mode, you can, e.g., apply an operator to multiple images or regions with a single call. The standard case, e.g., calling the operator with a single image, is called the simple mode. Whether or not an operator supports the tuple mode can be checked in the reference manual. For example, take a look at figure 5.5, which shows an extract of the reference manual entry for the operator CharThreshold: In the parameter section, the parameter Image is described as an image(-array); this signals that you can apply the operator to multiple images at once.
|
|
| Image (input_object) | singlechannelimage(-array) → HImage (byte) |
| HistoRegion (input_object) | region → HRegion |
| Characters (output_object) | region(-array) → HRegion |
| Sigma (input_control) | number → HTuple (double) |
| Percent (input_control) | number → HTuple (double / Hlong) |
| Threshold (output_control) | integer(-array) → HTuple (Hlong) |
Figure 5.5: The head and parts of the parameter section of the reference manual entry for CharThreshold.
If you call CharThreshold with multiple images, i.e., with an image tuple, the output parameters automatically become tuples as well. Consequently, the parameters Characters and Threshold are described as region(-array) and integer(-array), respectively.
Note that the class HTuple can also contain arrays (tuples) of control parameters of mixed type; please refer to section “Tuples” for more information about this class. In contrast to the control parameters, the iconic parameters remain instances of the class HObject in both modes, as this class can contain both single objects and object arrays.
In the object-oriented approach, control parameters can be of a basic type (simple mode only) or instances of HTuple (simple and tuple mode).
After this rather theoretic introduction, let us take a look at two examples which are both realized in the object-oriented and in the procedural approach. The examples highlight some interesting points:
Note that object indexes start with 1 (as used by SelectObj).
The first example shows how CharThreshold is applied in simple mode, i.e., to a single image:
// object-oriented approach HImage image("alpha1"); HRegion region; Hlong threshold; region = image.CharThreshold(image.GetDomain(), 2, 95, &threshold); image.DispImage(window); region.DispRegion(window); cout << "Threshold for 'alpha1': " << threshold;
// procedural approach HObject image; HObject region; HTuple threshold; ReadImage(&image, "alpha1"); CharThreshold(image, image, ®ion, 2, 95, &threshold); DispObj(image, window); DispObj(region, window); cout << "Threshold for 'alpha1': " << threshold.ToString();
The second example shows how CharThreshold is applied in tuple mode, i.e., to two images at once:
// object-oriented approach HImage images; HRegion regions; HTuple thresholds; images.GenEmptyObj(); for (int i = 1; i <= 2; i++) { images = images.ConcatObj(HImage(HTuple("alpha") + i)); } regions = images.CharThreshold(images.GetDomain()[1], 2, 95, &thresholds); for (int i = 1; i <= images.CountObj(); i++) { images[i].DispImage(window); regions[i].DispRegion(window); cout << "Threshold for 'alpha" << i << "': " << thresholds[i - 1].L(); window.Click();
// procedural approach HObject images, image; HObject regions, region; HTuple num; HTuple thresholds; GenEmptyObj(&images); for (int i = 1; i <= 2; i++) { ReadImage(&image, HTuple("alpha") + i); ConcatObj(images, image, &images); } CharThreshold(images, image, ®ions, 2, 95, &thresholds); CountObj(images, &num); for (int i = 0; i < num; i++) { SelectObj(images, &image, i + 1); DispObj(image, window); SelectObj(regions, ®ion, i + 1); DispObj(region, window); cout << "Threshold for 'alpha" << i + 1 << "': " << thresholds[i].L(); }