In HALCON/.NET, you call HALCON operators via instances of classes. The following code grabs the first image of an image sequence and displays it in the graphics window of HSmartWindowControl:
private HWindow Window; private HFramegrabber Framegrabber; private HImage Img;
Window = WindowControl.HalconWindow;
Framegrabber = new HFramegrabber("File", 1, 1, 0, 0, 0, 0, "default",
-1, "default", -1, "default",
"board/board.seq", "default", 1, -1);
Img = Framegrabber.GrabImage();
Img.GetImagePointer1(out ImgType, out ImgWidth, out ImgHeight);
Window.SetPart(0, 0, ImgHeight - 1, ImgWidth - 1);
Img.DispObj(Window);
The operator GrabImage is called via an instance of HFramegrabber. As an experienced HALCON user you will perhaps have identified the constructor of HFramegrabber as a call to the operator OpenFramegrabber.
Below, we take a closer look at:
But first, we give you an overview of the provided online help.
The main source of information about HALCON/.NET operators and classes is the reference manual, which is available as HTML and PDF version (note that the latter is only provided in HDevelop syntax). Under Windows, you can open both versions via the Start Menu. Under Linux, open index.html in the directory $HALCONROOT/doc/html/reference/operators, and reference_hdevelop.pdf in the directory $HALCONROOT/doc/pdf/reference, respectively. You can access them also via HDevelop's Help Browser.
The Reference Manual describes the functionality of each HALCON operator and its signatures, i.e., via which classes it can be called with which parameters. Furthermore, it gives an overview of the provided classes (which does not list all methods, however, only the HALCON operators).
Online help is also available in Visual Studio:
Via which classes you can call a HALCON operator is listed in the reference manual. Figure 11.1 shows the corresponding part of the description of the operator GrabImage:
|
|
| image (output_object) | image → HImage (byte / uint2) |
| acqHandle (input_control) | framegrabber → HFramegrabber / HTuple (IntPtr) |
Figure 11.1: The head and parts of the parameter section of the reference manual entry for GrabImage.
As you can see, the operator can be called via three classes: HOperatorSet, HImage, and HFramegrabber. The first variant, via HOperatorSet, is mainly used for the export of HDevelop programs (see section “Using HDevelop Programs”).
For normal applications, we recommend calling operators via the other classes, in the example HImage and HFramegrabber as in the following code example:
HImage Image1;
HImage Image4 = new HImage();
HFramegrabber Framegrabber = new HFramegrabber("File", 1, 1, 0, 0, 0, 0, "default", -1,
"default", -1, "default", "board/board.seq", "default", -1, -1);
Image1 = Framegrabber.GrabImage();
HImage Image3 = null;
Note that in the call via HFramegrabber the grabbed image is the return value of the method, whereas the call via HImage has no return value and the calling class instance is modified instead. Usually, calling class instances are not modified by an operator call - with the exception of “constructor-like” operator calls as in the example above.
Some operators like CountSeconds are available as class methods, i.e., you can call them directly via the class and do not need an instance:
double S1, S2; S1 = HSystem.CountSeconds();
In the reference manual, these operator calls start with the keyword static:
|
|
Figure 11.2: The head of the reference manual entry for CountSeconds.
During the lifecycle of an object, i.e., from declaration to finalization, different amounts of memory are allocated and released.
The following declaration just declares a variable of the class HImage that does not yet refer to any object:
HImage Image1;
In this state, you cannot use the variable to call operators; depending on the programming language, you might not even be able to use it as an output parameter (e.g., in Visual Basic 2005). However, you can assign image objects to the variable, e.g., from the return value of an operator:
Image1 = Framegrabber.GrabImage();
You can also initialize a variable when declaring it:
HImage Image2 = Framegrabber.GrabImage(); HImage Image3 = null;
Note that you can check the initialization state of a class instance with the method IsInitialized.
In contrast, the following declaration calls the “empty” constructor of the class HImage, which creates an unititialized class instance:
HImage Image4 = new HImage();
This class instance can be used to call “constructor-like” operators like GrabImage, which initializes it with a grabbed image:
Image4.GrabImage(Framegrabber);
Besides the empty constructor, most HALCON/.NET classes provide one or more constructors that initialize the created object based on HALCON operators. For example, HImage provides a constructor based on the operator ReadImage:
HImage Image5 = new HImage("fuse");
You can check which constructors are provided via the online help:
The main idea behind memory management in .NET is that the programmer does not worry about it and lets the garbage collector delete all objects that are not used anymore. HALCON/.NET fully complies to this philosophy by providing corresponding finalizers for all classes so that even unmanaged resources, e.g., a connection to an image acquisition device, are deleted correctly and automatically.
For most classes, the finalizer automatically calls suitable operators like CloseFramegrabber to free resources. Which operator is called is listed in the reference manual page of a class (for example, see the entry for HFramegrabber). This operator cannot be called via the class, as can be seen in the corresponding reference manual entry:
|
|
Figure 11.3: The head of the reference manual entry for CloseFramegrabber.
You do not even need to call such an operator if you, e.g., want to re-open the connection with different parameters, because this is done automatically.
Please don't call Close or Clear operators via HOperatorSet when using the normal classes like HFramegrabber.
As remarked above, the .NET philosophy is to let the garbage collector remove unused objects. However, because the garbage collector deletes unused objects only from time to time, the used memory increases in the meantime. Even more important is that, to the garbage collector, HALCON's iconic variables (images, regions, ...) seem to be rather “small”, because they only contain a reference to the (in many cases rather large) iconic objects in the database. Thus, the garbage collector may not free such variables even if they are not used anymore.
Therefore, you might need to force the removal of (unused) objects. There are two ways to do this:
private void Timer_Tick(object sender, System.EventArgs e)
{
Action();
GC.Collect();
GC.WaitForPendingFinalizers();
}
C++ Applications: The code for calling the Garbage Collector in a managed C++ application is
GC::Collect(); GC::WaitForPendingFinalizers();
HImage Image = new HImage("fuse");
...
Image.Dispose();
Besides reducing memory consumption, another reason to manually dispose of objects is to free resources, e.g., close a connection to an image acquisition device or a serial interface.
HTuple instances that contain handles also need to be disposed if the referenced resource is to be released in a deterministic way.
Please note that HALCON operators always create a new object instance for output parameters and return values (but not in the “constructor-like” operator calls that modify the calling instance). If the variable was already initialized, its old content (and the memory allocated for it) still exists until the garbage collector removes it. If you want to remove it manually, you must call Dispose before assigning an object to it.
Some classes overload standard operators like + (addition) to call HALCON operators. The following line, e.g., adds two images by internally calling AddImage:
Image5 = Image1 + Image2;
Please note that operator overloads are not available in Visual Basic .NET!
The following tables list the currently available operator overloads.
| Operator overloads for HImage | |
| - (unary) | inverts an image |
| + (image) | adds two images |
| - (image) | subtracts image2 from image1 |
| ∗ (image) | multiplies two images |
| + (scalar) | adds a constant gray value offset |
| - (scalar) | subtracts a constant gray value offset |
| ∗ (scalar) | scales an image by the specified factor |
| / (scalar) | scales an image by the specified divisor |
| >= (image) | segments an image using dynamic threshold |
| <= (image) | segments an image using dynamic threshold |
| >= (scalar) | segments an image using constant threshold |
| <= (scalar) | segments an image using constant threshold |
| & (region) | reduces the domain of an image |
| Operator overloads for HRegion | |
| & (region) | returns the intersection of regions |
| | (region) | returns the union of regions |
| / (region) | returns the difference of regions |
| ! (unary) | returns the complement of the region (may be infinite) |
| & (image) | returns the intersection of the region and the image domain |
| + (region) | returns the Minkowski addition of regions |
| - (region) | returns the Minkowski subtraction of regions |
| + (scalar) | dilates the region by the specified radius |
| - (scalar) | erodes the region by the specified radius |
| + (point) | translates the region |
| ∗ (scalar) | zooms the region |
| - (unary) | transposes the region |
| Operator overloads for HFunction1D | |
| + (scalar) | adds a constant offset to the function's Y values |
| - (scalar) | subtracts a constant offset from the function's Y values |
| - (unary) | negates the Y values of the function |
| ∗ (scalar) | scales the function's Y values |
| / (scalar) | scales the function's Y values |
| ∗ (function) | composes two functions (not a point-wise multiplication) |
| ! (unary) | calculates the inverse of the function |