Skip to content

About visualization🔗

Note

While this section is about .NET applications, its content is also applicable to application development in other languages.

Applications can use an instance of HSmartWindowControl to display results. However, this is only available for .NET Framework and the MVTec.HalconDotNet-Windows .NET Core packages. How to configure this control is described in Customizing HSmartWindowControl for visualization.

The actual display operators do not use the control but the HALCON graphics window (class HWindow) encapsulated inside. You can access the graphics window via the property HalconWindow of HSmartWindowControl:

private HWindow            window;

private void Form1_Load(object sender, System.EventArgs e)
{
  window = windowControl.HalconWindow;
}

In the code above, the variable for the instance of HWindow was declared globally and initialized in the event Load of the form.

You can configure the display parameters like pen color or line width with the operators in the operator reference section Graphics > Parameters:

window.SetDraw("margin");
window.SetLineWidth(3);

Images and other iconic objects are displayed with the operator DispObj, which can be called via the object to display with the window as parameter or vice versa:

img.DispObj(window);

More display operators, for example, to display lines or circles, can be found in the operator reference section Graphics > Output.

Instead of (or in addition to) using HSmartWindowControl, you can also open a HALCON graphics windows directly with the operator OpenWindow:

HWindow ZoomWindow = new HWindow(0, 0, width, height, 0, "visible", "");

In the code above, the window was opened “free-floating” on the display. You can also open it within another GUI element by passing its handle in the parameter fatherWindow.

Before displaying anything in the graphics window, you should set the image part to display with the operator SetPart. In the example code below, the opened window is used to display a zoomed part of the image:

ZoomWindow.SetPart(row1, col1, row1+height-1, col1+width-1);

Window controls for visualization🔗

The following documentation uses Visual Studio and .NET Framework, the same application functionality is possible in .NET Core.

Depending on which graphical subsystem a project is based on, a specific window control is available in the Visual Studio toolbox.

  • HSmartWindowControl (Windows Forms)
  • HSmartWindowControlWPF (WPF)

See also Using other GUI libraries.

Smart window control vs. HWindowControl🔗

The smart window control provides several advantages over HWindowControl:

  • It is used like any other control (for example, it can be embedded in TabControls or ScrollViewers, or overlaid with other controls).
  • Predefined mouse interaction is provided (moving of the window contents and zooming using the mouse wheel). The view can be reset by double-clicking the window.
  • The control automatically rescales without flickering.

Mouse usage with HSmartWindowControl🔗

In contrast to HSmartWindowControlWPF, a callback is required for HSmartWindowControl to enable zooming using the mouse wheel:

private void WindowControl_Load(object sender, EventArgs e)
{
  this.MouseWheel += my_MouseWheel;
}

In addition, you need to transform the mouse coordinates, so that they are relative to the upper left corner of the HSmartWindowControl:

private void my_MouseWheel(object sender, MouseEventArgs e)
{
    Point pt = windowControl.Location;
    MouseEventArgs newe = new MouseEventArgs(e.Button, e.Clicks, 
                                                 e.X - pt.X, e.Y - pt.Y, e.Delta);
    windowControl.HSmartWindowControl_MouseWheel(sender, newe);
}

Smart window control events🔗

Using the smart window control, the following events are triggered and can be reacted to:

  • Click
  • GotFocus, LostFocus
  • MouseEnter, MouseLeave, MouseHover
  • Resize, SizeChanged
  • KeyDown, KeyUp, KeyPress

Unsupported operators🔗

Not all operators can be used with the smart window control. The following operators are not supported:

draw_nurbs, draw_nurbs_mod, draw_nurbs_interp, draw_nurbs_interp_mod, drag_region1, drag_region2, drag_region3

Instead, use drawing objects.

Customizing HSmartWindowControl for visualization🔗

Note

In the following, the term HSmartWindowControl is used for simplicity. Read it as HSmartWindowControlWPF if your project is based on WPF. The same applies to the older HWindowControl correspondingly.

In most applications, you want to visualize at least some results.
Start by adding HSmartWindowControl to the form by double-clicking the corresponding icon in the toolbar. An empty (black) window appears; see Figure 1. Note that some properties are only available for HSmartWindowControlWPF elements. All properties of the HSmartWindowControlWPF support data binding.

Figure 1: Adapting the properties of HSmartWindowControlWPF

Important

Do not modify the Brush properties in Visual Studio. They are disabled for HSmartWindowControlWPF to prevent undesired side-effects.

HColor
Specifies the color of HRegion and HXLDCont objects
HColored
Specifies the colors for displaying multiple HRegion or HXLDCont objects in different colors
HDisableAutoResize
If set to true, images are not automatically scaled when they are displayed.
HDisplayCurrentObject
Displays the assigned HImage or HObject
HDoubleClickToFitContent
If set to true (the default), double clicking resizes the content of the HSmartWindowControl to fit the size of the control.
If HKeepAspectRatio is also set to true, the contents are rescaled so that the aspect ratio is maintained.
HDraw
Specifies the fill mode of HRegion objects.
If HDraw is set to ‘fill’, output regions are filled, if set to ‘margin’, only contours are displayed.
HDrawingObjectsModifier
Specifies the modifier key to interact with drawing objects.
If a modifier key is set, the user can only interact with drawing objects while keeping the modifier key pressed. This is especially useful when interacting with XLD drawing objects. By default, it is set to None. Other possible values are Alt, Ctrl, or Shift.
HFont
Specifies the font for displaying messages in the HSmartWindowControlWPF
HImagePart
Specifies the image part of the corresponding HALCON window.
Note that the part is specified with the values X, Y, Width, and Height, whereas the corresponding operator SetPart expects the four corner points. You can modify the displayed part in your application at any time, for example, to display a zoomed part of the image.
HKeepAspectRatio
If set to true (the default), the content of the HSmartWindowControl keeps its aspect ratio when the control is resized or zoomed. The aspect ratio is the quotient Width/Height set at design time with HImagePart.
HLineStyle
Specifies the contour pattern of HRegion and HXLDCont objects
HLineWidth
Specifies the contour thickness of HRegion and HXLDCont objects
HMoveContent
If set to true (the default), the contents of the HALCON window can be dragged using the mouse.
HZoomContent
Specifies the behavior of the mouse wheel.
If set to WheelForwardZoomsIn (the default), the contents of the HALCON window is zoomed in and out when moving the mouse wheel forwards and backwards, respectively. Setting the property to WheelBackwardZoomsIn reverses the behavior. If set to Off, zooming using the mouse wheel is disabled.
HZoomFactor
Specifies the step size when zooming with the mouse wheel.
The default is 1.41. Values must be greater than 1 and less or equal to 100. A higher value leads to faster zooming.

HSmartWindowControlWPF can be used to implement the Model-View-Viewmodel (MVVM) pattern, as all properties of the control support data binding. By adding objects to the Items collection or setting the ItemsSource property, you can specify what the control should display, also in pure XAML code.

Example

The following example illustrates how to show an image, color a region in “magenta”, and display a text message:

<ha:HSmartWindowControlWPF HDraw="fill">
  <!--Iconic items can be added using HIconicDisplayObjectWPFs-->
  <ha:HIconicDisplayObjectWPF IconicObject="{Binding DisplayImage}"/>
  <!--Also with individual drawing properties-->
  <ha:HIconicDisplayObjectWPF IconicObject="{Binding DisplayRegion}"
  HDraw="margin" HColor="magenta"/>
  <!--Messages can be displayed using HMessageDisplayObjectWPFs-->
  <ha:HMessageDisplayObjectWPF HMessageText="{Binding ImageName}"/>
</ha:HSmartWindowControlWPF>

DisplayImage, DisplayRegion and ImageName have to exist in the DataContext of HSmartWindowControlWPF.