Skip to content

Quick start🔗

This section demonstrates how to create a simple HALCON application with each of the available interfaces – HALCON/C++, HALCON/.NET, HALCON/Python, and HALCON/C.
For a more comprehensive description, see Basics of the interfaces and Deploying applications.


Task: Read an image and compute the number of connected regions in it, as illustrated in Figure 1.

Figure 1: Input image of a printed circuit board (left) – Regions found by threshold, colored red (middle) – Connected regions, a result of connection (right)


Solution: Depending on the used interface, the following steps are required to create the example program.

  1. Install HALCON 26.11.

  2. Install a C++11 (or newer) toolchain on your system.

  3. Set up your C++ development environment of choice.

  4. Run the following commands in a shell:

    mkdir region_example
    cd region_example
    
  5. Create a file named main.cpp and add the following content:

    #include <iostream>
    #include <HalconCpp.h>
    
    using HalconCpp::HImage;
    using HalconCpp::HRegion;
    
    int main()
    {
        HImage img{"pcb"};
    
        HRegion region = img.Threshold(0, 122);
        Hlong numRegions = region.Connection().CountObj();
    
        std::cout << "Number of Regions: " << numRegions << '\n';
    }
    
  6. Compile the program.
    For more information, see Creating an application.

  7. To run the application, enter the following command in the same shell:

    ./region_example
    
    As a result, you should see the following output:
    'Number of Regions: 43'
  1. Install HALCON 26.11.

  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 26110
    
  4. Change the content of Program.cs to the following:

    using System;
    
    using HalconDotNet;
    
    namespace region_example
    {
        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, enter the following command in the same shell:

    dotnet run
    
    As a result, you should see the following output:
    'Number of Regions: 43'
  1. Install HALCON 26.11.

  2. Install Python 3.8 (or later) on your system.

  3. Set up your Python environment of choice, for example:

    python -m venv path_to_new_virtual_environment
    
  4. Run the following commands in a shell:

    mkdir region_example
    cd region_example
    pip install mvtec-halcon==26110
    
  5. Create a file named program.py and add the following content:

    import halcon as ha
    
    if __name__ == '__main__':
        img = ha.read_image('pcb')
    
        region = ha.threshold(img, 0, 122)
        num_regions = ha.count_obj(ha.connection(region))
    
        print(f'Number of Regions: {num_regions}')
    
  6. To run the application, enter the following command in the same shell:

    python program.py
    
    As a result, you should see the following output:
    'Number of Regions: 43'
  1. Install HALCON 26.11.

  2. Install a C99 (or newer) toolchain on your system.

  3. Set up your C development environment of choice.

  4. Run the following commands in a shell:

    mkdir region_example
    cd region_example
    
  5. Create a file named main.c and add the following content:

    #include <stdio.h>
    #include <inttypes.h>
    
    #include <HalconC.h>
    
    int main()
    {
        Hobject img;
        read_image(&img, "pcb");
    
        Hobject region;
        threshold(img, &region, 0, 122);
    
        Hobject connected_regions;
        connection(region, &connected_regions);
    
        Hlong num_regions = 0;
        count_obj(connected_regions, &num_regions);
    
        printf("Number of Regions: \%" PRIdPTR "\n", num_regions);
    }
    
  6. Compile the program.
    For more information, see Creating an application.

  7. To run the application, enter the following command in the same shell:

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