9.1   A First Example

This section demonstrates how to create a simple HALCON application with .NET Core. For a more comprehensive description, see section “Creating Applications With HALCON/.NET”.

The task is to read an image and compute the number of connected regions in it, as illustrated in figure 9.1

Figure 9.1: Left: Input image of a printed circuit board. Middle: Regions found by threshold, colored red. Right: Connected regions, a result of connection.

  1. Install HALCON 21.05
  2. Install the .NET Core SDK for your system.
  3. Run the following commands in a shell:

    
    dotnet new console -n region_example
    cd region_example
    dotnet add package MVTec.HalconDotNet -v 21050
    

  4. Change the content of Program.cs to:

    using System;
    
    using HalconDotNet;
    
    namespace ConsoleApp
    {
      class Program
      {
        static void Main(string[] args)
        {
          HImage img = new HImage("pcb");
    
          HRegion region = img.Threshold(0d, 122d);
          int numRegions = region.Connection().CountObj();
    
          Console.WriteLine("Number of Regions: " + numRegions);
        }
      }
    }
    

  5. To run the application, type the following command in the same shell:

    dotnet run
    

As a result, you should see the following output 'Number of Regions: 43'.