The following example illustrates the steps described in section “HALCON Packages”. Assume, one wants to implement a new operator named user_thresh to extract all pixels with a gray value larger than a threshold value specified in an input parameter. The use of user_thresh within a C program is illustrated in figure 1.2; a more detailed example is provided in %HALCONEXAMPLES%\extension_package\halconuser\source\testthreshold.c.
main() { Hobject Image,Region; int Thresh; read_image(&Image,"monkey"); Thresh = 100; user_thresh(Image,&Region,Thresh); }
Figure 1.2: Calling the operator user_thresh in C.
To use user_thresh in this way within C programs4),
user_thresh <- CIPUserThreshold[Image:Region:Threshold$i:];
Figure 1.3: Defining a new operator user_thresh.
user_thresh <- CIPUserThreshold[Image:Region:Threshold:] short.german Schwellwertoperator.; short.english Selection of gray values by thresholding.; module foundation; chapter.german BenutzerErweiterungen; chapter.english UserExtensions; functionality image; parameter Image: input_object; sem_type: image; parameter Region: output_object; sem_type: region; parameter Threshold: input_control; default_type: integer; multivalue: false; sem_type: number;
Figure 1.4: An example for an extended def-file of the operator user_thresh (minimal form), cf. %HALCONEXAMPLES%\extension_package\halconuser\def\threshold.def.
First of all one has to create a definition file (file extension .def), as illustrated in figures 1.3 and figure 1.4. Figure 1.3 shows only the absolute minimum of information that a definition file must contain in order to call a new operator within C programs. When using HDevelop, C++, or C#, an extended version is necessary as described in section “Extended Operator Description”. The minimal form of such an extended definition is illustrated in figure 1.4. The definition file specifies:
The minimal form of the extended operator def-file includes: A short description (short), the HALCON module (module) the operator belongs to, the mapping to a chapter (chapter) of the manual, the mapping to an iconic object (functionality), and for every parameter its semantic type (sem_type). In addition, the type (default_type) and number (multivalue) must be specified for control parameters.
Our operator user_thresh has got the following parameters:
hcomp -u -H -phalconuser threshold.def hcomp -u -C -phalconuser threshold.def
Figure 1.5: Calls of hcomp for HALCON/C.
The operator definition within the def-file now is used by hcomp to generate the appropriate interface code. figure 1.5 shows the calls of hcomp. Note, that within this manual halconuser is used as name of the HALCON package to be created. By calling hcomp with these options5) the files Hhalconuser.c (HALCON interface code), HChalconuser.c (C-specific interface code) and HChalconuser.h (prototype of the new HALCON operator) are created.
In the next step one has to program the new HALCON operator. This is done by implementing the supply and action procedure. This results in the typical structure of HALCON operators as illustrated in figure 1.6.
/* action procedure */ Herror IPBThreshold(proc_handle,region,image, width,threshold,region_out) { ... } /* supply procedure - always with this parameter! */ Herror CIPUserThreshold(Hproc_handle proc_handle) { HGetCPar(..,&Threshold,..); /* get control */ /* parameter */ HAllSegm(proc_handle,®ion,&image,1,i) /* work on all */ /* input images */ { HCkP(proc_handle,IPBThreshold(...)); /* call action */ /* procedure */ HNewRegion(proc_handle,region_out); /* store (result) */ /* region */ } return(H_MSG_TRUE); }
Figure 1.6: Sample source code of supply CIPUserThreshold() and action IPBThreshold() procedure.
Compiling the source code generates an object file (in the example cipuserthreshold.o) that should be integrated into the user extension library (halconuser). Moreover, the generated interface code must be converted to the corresponding language dependent interface library: In case of a C application, this is halconuserc containing HChalconuser.o. The file makefile in the directory %HALCONEXAMPLES%\extension_package\halconuser contains a makefile makefile for generating the example package itself under Linux. makefile.win is the corresponding file to be used via nmake under Windows. The makefiles makefile_testprog and makefile_testprog.win generate example applications using the new operators.
Now we can use our new operators within C programs. But they are not yet available within HDevelop, because the online help files have not been generated so far. There is also still missing any kind of documentation, such as the HTML manual pages.
The online help files (as they are used e.g., by HDevelop) can also be generated with the help of the HALCON compiler hcomp, see figure 1.7. However, to do so an extended version of the def-file must be available. The generated help files must be placed in the subdirectory help of the package.
DEF_SOURCES = def/threshold.def HCOMP = $(HALCONROOT)/bin/$(HALCONARCH)/hcomp all: html html: $(DEF_SOURCES) $(HCOMP) -R $(DEF_SOURCES) mv *.html doc/html/reference
Figure 1.8: Makefile for generating HTML references.
The help files generated by now are used to access information about the new operators by calling specific HALCON operators like get_operator_info. This important especially for constructing graphical user interfaces like HDevelop. For the user a more convenient way to access information online is to browse through HTML documents. The HTML documentation of HALCON operators can be generated by hcomp as well, see figure 1.8. The HTML files have to be placed in the corresponding subdirectory doc\html\reference of the package.
If you now add the package path to HALCONEXTENSIONS and start HDevelop the user extensions, e.g. user_thresh, are automatically available in the specified menu (i.e., chapter). In our example this menu is called UserExtensions as specified in %HALCONEXAMPLES%\extension_package\halconuser\def\threshold.def.6) You can select and execute the user-defined operators like all the built-in operators of the HALCON system. The HTML documentation is available via the Help button within the operator window.
Examples of how to use the user defined extensions in HDevelop can be found in the subdirectory examples of %HALCONEXAMPLES%\extension_package\halconuser (*.dev). Furthermore, the *.dev programs have been exported as C++ and C# programs. The *.cpp and *.cs files can be found in the subdirectory source. You can compile them using the already mentioned makefiles. These *.cpp and *.cs files also serve as an example for the integration of user defined operators into the host languages C++ and C#. Note, that before compiling the exported C++ and C# code containing operators from the package halconuser, the corresponding user extension libraries must have been created. This can be done via the makefile in %HALCONEXAMPLES%\extension_package\halconuser which will also generate the needed user extension C++ library halconusercpp and .NET assembly halconuserdotnet.
Please note, that on some systems not all users have write permissions in the directories mentioned above. To experiment with the example package we recommend to create a private copy in your working directory. In such a case you must of course use the actual path to your copy when modifying environment variables.
| 4) To use it
within C++, C#, or Visual Basic basically the same steps are necessary, see
chapter “Creating a New HALCON Package”.
5) see section “The HALCON Compiler hcomp” for a complete description. 6) UserExtensions is integrated into the menu Operator of HDevelop. |