halcon 基于样本的识别 C#语言

本文档详细介绍了如何使用HALCON进行图像处理和识别任务。首先,通过dev_open_window_fit_image函数适配窗口大小并显示图像。接着,利用disp_message显示提示信息,并设置dev_update_off以提高性能。然后,list_image_files函数用于获取指定目录下的图像文件。最后,set_display_font设置字体样式,实现跨平台的文本显示。整个过程涵盖了HALCON的基本操作,为实际应用提供了清晰的步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//
// File generated by HDevelop for HALCON/.NET (C#) Version 18.11.0.1
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp950).
// 
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
// 
// Source files with different encoding should not be mixed in one project.
//

using HalconDotNet;

public partial class HDevelopExport
{
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
  public HDevelopExport()
  {
    // Default settings used in HDevelop
    HOperatorSet.SetSystem("width", 512);
    HOperatorSet.SetSystem("height", 512);
    if (HalconAPI.isWindows)
      HOperatorSet.SetSystem("use_window_thread","true");
    action();
  }
#endif

  // Procedures 
  // External procedures 
  // Chapter: Develop
  // Short Description: Open a new graphics window that preserves the aspect ratio of the given image. 
  public void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column, 
      HTuple hv_WidthLimit, HTuple hv_HeightLimit, out HTuple hv_WindowHandle)
  {




    // Local iconic variables 

    // Local control variables 

    HTuple hv_MinWidth = new HTuple(), hv_MaxWidth = new HTuple();
    HTuple hv_MinHeight = new HTuple(), hv_MaxHeight = new HTuple();
    HTuple hv_ResizeFactor = new HTuple(), hv_ImageWidth = new HTuple();
    HTuple hv_ImageHeight = new HTuple(), hv_TempWidth = new HTuple();
    HTuple hv_TempHeight = new HTuple(), hv_WindowWidth = new HTuple();
    HTuple hv_WindowHeight = new HTuple();
    // Initialize local and output iconic variables 
    hv_WindowHandle = new HTuple();
    try
    {
      //This procedure opens a new graphics window and adjusts the size
      //such that it fits into the limits specified by WidthLimit
      //and HeightLimit, but also maintains the correct image aspect ratio.
      //
      //If it is impossible to match the minimum and maximum extent requirements
      //at the same time (f.e. if the image is very long but narrow),
      //the maximum value gets a higher priority,
      //
      //Parse input tuple WidthLimit
      if ((int)((new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(
          0))).TupleOr(new HTuple(hv_WidthLimit.TupleLess(0)))) != 0)
      {
        hv_MinWidth.Dispose();
        hv_MinWidth = 500;
        hv_MaxWidth.Dispose();
        hv_MaxWidth = 800;
      }
      else if ((int)(new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(
          1))) != 0)
      {
        hv_MinWidth.Dispose();
        hv_MinWidth = 0;
        hv_MaxWidth.Dispose();
        hv_MaxWidth = new HTuple(hv_WidthLimit);
      }
      else
      {
        hv_MinWidth.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_MinWidth = hv_WidthLimit.TupleSelect(
            0);
        }
        hv_MaxWidth.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_MaxWidth = hv_WidthLimit.TupleSelect(
            1);
        }
      }
      //Parse input tuple HeightLimit
      if ((int)((new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(
          0))).TupleOr(new HTuple(hv_HeightLimit.TupleLess(0)))) != 0)
      {
        hv_MinHeight.Dispose();
        hv_MinHeight = 400;
        hv_MaxHeight.Dispose();
        hv_MaxHeight = 600;
      }
      else if ((int)(new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(
          1))) != 0)
      {
        hv_MinHeight.Dispose();
        hv_MinHeight = 0;
        hv_MaxHeight.Dispose();
        hv_MaxHeight = new HTuple(hv_HeightLimit);
      }
      else
      {
        hv_MinHeight.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_MinHeight = hv_HeightLimit.TupleSelect(
            0);
        }
        hv_MaxHeight.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_MaxHeight = hv_HeightLimit.TupleSelect(
            1);
        }
      }
      //
      //Test, if window size has to be changed.
      hv_ResizeFactor.Dispose();
      hv_ResizeFactor = 1;
      hv_ImageWidth.Dispose();hv_ImageHeight.Dispose();
      HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);
      //First, expand window to the minimum extents (if necessary).
      if ((int)((new HTuple(hv_MinWidth.TupleGreater(hv_ImageWidth))).TupleOr(new HTuple(hv_MinHeight.TupleGreater(
          hv_ImageHeight)))) != 0)
      {
        hv_ResizeFactor.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_ResizeFactor = (((((hv_MinWidth.TupleReal()
            )/hv_ImageWidth)).TupleConcat((hv_MinHeight.TupleReal())/hv_ImageHeight))).TupleMax()
            ;
        }
      }
      hv_TempWidth.Dispose();
      using (HDevDisposeHelper dh = new HDevDisposeHelper())
      {
      hv_TempWidth = hv_ImageWidth*hv_ResizeFactor;
      }
      hv_TempHeight.Dispose();
      using (HDevDisposeHelper dh = new HDevDisposeHelper())
      {
      hv_TempHeight = hv_ImageHeight*hv_ResizeFactor;
      }
      //Then, shrink window to maximum extents (if necessary).
      if ((int)((new HTuple(hv_MaxWidth.TupleLess(hv_TempWidth))).TupleOr(new HTuple(hv_MaxHeight.TupleLess(
          hv_TempHeight)))) != 0)
      {
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_ResizeFactor = hv_ResizeFactor*((((((hv_MaxWidth.TupleReal()
            )/hv_TempWidth)).TupleConcat((hv_MaxHeight.TupleReal())/hv_TempHeight))).TupleMin()
            );
        hv_ResizeFactor.Dispose();
        hv_ResizeFactor = ExpTmpLocalVar_ResizeFactor;
        }
        }
      }
      hv_WindowWidth.Dispose();
      using (HDevDisposeHelper dh = new HDevDisposeHelper())
      {
      hv_WindowWidth = hv_ImageWidth*hv_ResizeFactor;
      }
      hv_WindowHeight.Dispose();
      using (HDevDisposeHelper dh = new HDevDisposeHelper())
      {
      hv_WindowHeight = hv_ImageHeight*hv_ResizeFactor;
      }
      //Resize window
      HOperatorSet.SetWindowAttr("background_color","black");
      HOperatorSet.OpenWindow(hv_Row,hv_Column,hv_WindowWidth,hv_WindowHeight,0,"visible","",out hv_WindowHandle);
      HDevWindowStack.Push(hv_WindowHandle);
      if (HDevWindowStack.IsOpen())
      {
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_ImageHeight-1, 
            hv_ImageWidth-1);
        }
      }

      hv_MinWidth.Dispose();
      hv_MaxWidth.Dispose();
      hv_MinHeight.Dispose();
      hv_MaxHeight.Dispose();
      hv_ResizeFactor.Dispose();
      hv_ImageWidth.Dispose();
      hv_ImageHeight.Dispose();
      hv_TempWidth.Dispose();
      hv_TempHeight.Dispose();
      hv_WindowWidth.Dispose();
      hv_WindowHeight.Dispose();

      return;
    }
    catch (HalconException HDevExpDefaultException)
    {

      hv_MinWidth.Dispose();
      hv_MaxWidth.Dispose();
      hv_MinHeight.Dispose();
      hv_MaxHeight.Dispose();
      hv_ResizeFactor.Dispose();
      hv_ImageWidth.Dispose();
      hv_ImageHeight.Dispose();
      hv_TempWidth.Dispose();
      hv_TempHeight.Dispose();
      hv_WindowWidth.Dispose();
      hv_WindowHeight.Dispose();

      throw HDevExpDefaultException;
    }
  }

  // Chapter: Develop
  // Short Description: Switch dev_update_pc, dev_update_var and dev_update_window to 'off'. 
  public void dev_update_off ()
  {

    // Initialize local and output iconic variables 
    //This procedure sets different update settings to 'off'.
    //This is useful to get the best performance and reduce overhead.
    //
    // dev_update_pc(...); only in hdevelop
    // dev_update_var(...); only in hdevelop
    // dev_update_window(...); only in hdevelop


    return;
  }

  // Chapter: Graphics / Text
  // Short Description: This procedure displays 'Click 'Run' to continue' in the lower right corner of the screen. 
  public void disp_continue_message (HTuple hv_WindowHandle, HTuple hv_Color, HTuple hv_Box)
  {



    // Local iconic variables 

    // Local control variables 

    HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new HTuple();
    HTuple hv_ContinueMessage = new HTuple();
    HTuple   hv_Color_COPY_INP_TMP = new HTuple(hv_Color);

    // Initialize local and output iconic variables 
    try
    {
      //This procedure displays 'Press Run (F5) to continue' in the
      //lower right corner of the screen.
      //It uses the procedure disp_message.
      //
      //Input parameters:
      //WindowHandle: The window, where the text shall be displayed
      //Color: defines the text color.
      //   If set to '' or 'auto', the currently set color is used.
      //Box: If set to 'true', the text is displayed in a box.
      //
      //Convert the parameter Box to generic parameters.
      hv_GenParamName.Dispose();
      hv_GenParamName = new HTuple();
      hv_GenParamValue.Dispose();
      hv_GenParamValue = new HTuple();
      if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(0))) != 0)
      {
        if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleEqual("false"))) != 0)
        {
          //Display no box
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
              "box");
          hv_GenParamName.Dispose();
          hv_GenParamName = ExpTmpLocalVar_GenParamName;
          }
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
              "false");
          hv_GenParamValue.Dispose();
          hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
          }
          }
        }
        else if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleNotEqual(
            "true"))) != 0)
        {
          //Set a color other than the default.
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
              "box_color");
          hv_GenParamName.Dispose();
          hv_GenParamName = ExpTmpLocalVar_GenParamName;
          }
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
              hv_Box.TupleSelect(0));
          hv_GenParamValue.Dispose();
          hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
          }
          }
        }
      }
      if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(1))) != 0)
      {
        if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleEqual("false"))) != 0)
        {
          //Display no shadow.
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
              "shadow");
          hv_GenParamName.Dispose();
          hv_GenParamName = ExpTmpLocalVar_GenParamName;
          }
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
              "false");
          hv_GenParamValue.Dispose();
          hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
          }
          }
        }
        else if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleNotEqual(
            "true"))) != 0)
        {
          //Set a shadow color other than the default.
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
              "shadow_color");
          hv_GenParamName.Dispose();
          hv_GenParamName = ExpTmpLocalVar_GenParamName;
          }
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
              hv_Box.TupleSelect(1));
          hv_GenParamValue.Dispose();
          hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
          }
          }
        }
      }
      //
      if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(""))) != 0)
      {
        //disp_text does not accept an empty string for Color.
        hv_Color_COPY_INP_TMP.Dispose();
        hv_Color_COPY_INP_TMP = new HTuple();
      }
      //
      //Display the message.
      hv_ContinueMessage.Dispose();
      hv_ContinueMessage = "Press Run (F5) to continue";
      HOperatorSet.DispText(hv_WindowHandle, hv_ContinueMessage, "window", "bottom", 
          "right", hv_Color_COPY_INP_TMP, hv_GenParamName, hv_GenParamValue);

      hv_Color_COPY_INP_TMP.Dispose();
      hv_GenParamName.Dispose();
      hv_GenParamValue.Dispose();
      hv_ContinueMessage.Dispose();

      return;
    }
    catch (HalconException HDevExpDefaultException)
    {

      hv_Color_COPY_INP_TMP.Dispose();
      hv_GenParamName.Dispose();
      hv_GenParamValue.Dispose();
      hv_ContinueMessage.Dispose();

      throw HDevExpDefaultException;
    }
  }

  // Chapter: Graphics / Text
  // Short Description: This procedure writes a text message. 
  public void disp_message (HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem, 
      HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
  {



    // Local iconic variables 

    // Local control variables 

    HTuple hv_GenParamName = new HTuple(), hv_GenParamValue = new HTuple();
    HTuple   hv_Color_COPY_INP_TMP = new HTuple(hv_Color);
    HTuple   hv_Column_COPY_INP_TMP = new HTuple(hv_Column);
    HTuple   hv_CoordSystem_COPY_INP_TMP = new HTuple(hv_CoordSystem);
    HTuple   hv_Row_COPY_INP_TMP = new HTuple(hv_Row);

    // Initialize local and output iconic variables 
    try
    {
      //This procedure displays text in a graphics window.
      //
      //Input parameters:
      //WindowHandle: The WindowHandle of the graphics window, where
      //   the message should be displayed
      //String: A tuple of strings containing the text message to be displayed
      //CoordSystem: If set to 'window', the text position is given
      //   with respect to the window coordinate system.
      //   If set to 'image', image coordinates are used.
      //   (This may be useful in zoomed images.)
      //Row: The row coordinate of the desired text position
      //   A tuple of values is allowed to display text at different
      //   positions.
      //Column: The column coordinate of the desired text position
      //   A tuple of values is allowed to display text at different
      //   positions.
      //Color: defines the color of the text as string.
      //   If set to [], '' or 'auto' the currently set color is used.
      //   If a tuple of strings is passed, the colors are used cyclically...
      //   - if |Row| == |Column| == 1: for each new textline
      //   = else for each text position.
      //Box: If Box[0] is set to 'true', the text is written within an orange box.
      //     If set to' false', no box is displayed.
      //     If set to a color string (e.g. 'white', '#FF00CC', etc.),
      //       the text is written in a box of that color.
      //     An optional second value for Box (Box[1]) controls if a shadow is displayed:
      //       'true' -> display a shadow in a default color
      //       'false' -> display no shadow
      //       otherwise -> use given string as color string for the shadow color
      //
      //It is possible to display multiple text strings in a single call.
      //In this case, some restrictions apply:
      //- Multiple text positions can be defined by specifying a tuple
      //  with multiple Row and/or Column coordinates, i.e.:
      //  - |Row| == n, |Column| == n
      //  - |Row| == n, |Column| == 1
      //  - |Row| == 1, |Column| == n
      //- If |Row| == |Column| == 1,
      //  each element of String is display in a new textline.
      //- If multiple positions or specified, the number of Strings
      //  must match the number of positions, i.e.:
      //  - Either |String| == n (each string is displayed at the
      //                          corresponding position),
      //  - or     |String| == 1 (The string is displayed n times).
      //
      //
      //Convert the parameters for disp_text.
      if ((int)((new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
          new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(new HTuple())))) != 0)
      {

        hv_Color_COPY_INP_TMP.Dispose();
        hv_Column_COPY_INP_TMP.Dispose();
        hv_CoordSystem_COPY_INP_TMP.Dispose();
        hv_Row_COPY_INP_TMP.Dispose();
        hv_GenParamName.Dispose();
        hv_GenParamValue.Dispose();

        return;
      }
      if ((int)(new HTuple(hv_Row_COPY_INP_TMP.TupleEqual(-1))) != 0)
      {
        hv_Row_COPY_INP_TMP.Dispose();
        hv_Row_COPY_INP_TMP = 12;
      }
      if ((int)(new HTuple(hv_Column_COPY_INP_TMP.TupleEqual(-1))) != 0)
      {
        hv_Column_COPY_INP_TMP.Dispose();
        hv_Column_COPY_INP_TMP = 12;
      }
      //
      //Convert the parameter Box to generic parameters.
      hv_GenParamName.Dispose();
      hv_GenParamName = new HTuple();
      hv_GenParamValue.Dispose();
      hv_GenParamValue = new HTuple();
      if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(0))) != 0)
      {
        if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleEqual("false"))) != 0)
        {
          //Display no box
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
              "box");
          hv_GenParamName.Dispose();
          hv_GenParamName = ExpTmpLocalVar_GenParamName;
          }
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
              "false");
          hv_GenParamValue.Dispose();
          hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
          }
          }
        }
        else if ((int)(new HTuple(((hv_Box.TupleSelect(0))).TupleNotEqual(
            "true"))) != 0)
        {
          //Set a color other than the default.
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
              "box_color");
          hv_GenParamName.Dispose();
          hv_GenParamName = ExpTmpLocalVar_GenParamName;
          }
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
              hv_Box.TupleSelect(0));
          hv_GenParamValue.Dispose();
          hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
          }
          }
        }
      }
      if ((int)(new HTuple((new HTuple(hv_Box.TupleLength())).TupleGreater(1))) != 0)
      {
        if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleEqual("false"))) != 0)
        {
          //Display no shadow.
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
              "shadow");
          hv_GenParamName.Dispose();
          hv_GenParamName = ExpTmpLocalVar_GenParamName;
          }
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
              "false");
          hv_GenParamValue.Dispose();
          hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
          }
          }
        }
        else if ((int)(new HTuple(((hv_Box.TupleSelect(1))).TupleNotEqual(
            "true"))) != 0)
        {
          //Set a shadow color other than the default.
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamName = hv_GenParamName.TupleConcat(
              "shadow_color");
          hv_GenParamName.Dispose();
          hv_GenParamName = ExpTmpLocalVar_GenParamName;
          }
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_GenParamValue = hv_GenParamValue.TupleConcat(
              hv_Box.TupleSelect(1));
          hv_GenParamValue.Dispose();
          hv_GenParamValue = ExpTmpLocalVar_GenParamValue;
          }
          }
        }
      }
      //Restore default CoordSystem behavior.
      if ((int)(new HTuple(hv_CoordSystem_COPY_INP_TMP.TupleNotEqual("window"))) != 0)
      {
        hv_CoordSystem_COPY_INP_TMP.Dispose();
        hv_CoordSystem_COPY_INP_TMP = "image";
      }
      //
      if ((int)(new HTuple(hv_Color_COPY_INP_TMP.TupleEqual(""))) != 0)
      {
        //disp_text does not accept an empty string for Color.
        hv_Color_COPY_INP_TMP.Dispose();
        hv_Color_COPY_INP_TMP = new HTuple();
      }
      //
      HOperatorSet.DispText(hv_WindowHandle, hv_String, hv_CoordSystem_COPY_INP_TMP, 
          hv_Row_COPY_INP_TMP, hv_Column_COPY_INP_TMP, hv_Color_COPY_INP_TMP, hv_GenParamName, 
          hv_GenParamValue);

      hv_Color_COPY_INP_TMP.Dispose();
      hv_Column_COPY_INP_TMP.Dispose();
      hv_CoordSystem_COPY_INP_TMP.Dispose();
      hv_Row_COPY_INP_TMP.Dispose();
      hv_GenParamName.Dispose();
      hv_GenParamValue.Dispose();

      return;
    }
    catch (HalconException HDevExpDefaultException)
    {

      hv_Color_COPY_INP_TMP.Dispose();
      hv_Column_COPY_INP_TMP.Dispose();
      hv_CoordSystem_COPY_INP_TMP.Dispose();
      hv_Row_COPY_INP_TMP.Dispose();
      hv_GenParamName.Dispose();
      hv_GenParamValue.Dispose();

      throw HDevExpDefaultException;
    }
  }

  // Chapter: File / Misc
  // Short Description: Get all image files under the given path 
  public void list_image_files (HTuple hv_ImageDirectory, HTuple hv_Extensions, HTuple hv_Options, 
      out HTuple hv_ImageFiles)
  {



    // Local iconic variables 

    // Local control variables 

    HTuple hv_ImageDirectoryIndex = new HTuple();
    HTuple hv_ImageFilesTmp = new HTuple(), hv_CurrentImageDirectory = new HTuple();
    HTuple hv_HalconImages = new HTuple(), hv_OS = new HTuple();
    HTuple hv_Directories = new HTuple(), hv_Index = new HTuple();
    HTuple hv_Length = new HTuple(), hv_NetworkDrive = new HTuple();
    HTuple hv_Substring = new HTuple(), hv_FileExists = new HTuple();
    HTuple hv_AllFiles = new HTuple(), hv_i = new HTuple();
    HTuple hv_Selection = new HTuple();
    HTuple   hv_Extensions_COPY_INP_TMP = new HTuple(hv_Extensions);

    // Initialize local and output iconic variables 
    hv_ImageFiles = new HTuple();
    try
    {
      //This procedure returns all files in a given directory
      //with one of the suffixes specified in Extensions.
      //
      //Input parameters:
      //ImageDirectory: Directory or a tuple of directories with images.
      //   If a directory is not found locally, the respective directory
      //   is searched under %HALCONIMAGES%/ImageDirectory.
      //   See the Installation Guide for further information
      //   in case %HALCONIMAGES% is not set.
      //Extensions: A string tuple containing the extensions to be found
      //   e.g. ['png','tif',jpg'] or others
      //If Extensions is set to 'default' or the empty string '',
      //   all image suffixes supported by HALCON are used.
      //Options: as in the operator list_files, except that the 'files'
      //   option is always used. Note that the 'directories' option
      //   has no effect but increases runtime, because only files are
      //   returned.
      //
      //Output parameter:
      //ImageFiles: A tuple of all found image file names
      //
      if ((int)((new HTuple((new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
          new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(""))))).TupleOr(new HTuple(hv_Extensions_COPY_INP_TMP.TupleEqual(
          "default")))) != 0)
      {
        hv_Extensions_COPY_INP_TMP.Dispose();
        hv_Extensions_COPY_INP_TMP = new HTuple();
        hv_Extensions_COPY_INP_TMP[0] = "ima";
        hv_Extensions_COPY_INP_TMP[1] = "tif";
        hv_Extensions_COPY_INP_TMP[2] = "tiff";
        hv_Extensions_COPY_INP_TMP[3] = "gif";
        hv_Extensions_COPY_INP_TMP[4] = "bmp";
        hv_Extensions_COPY_INP_TMP[5] = "jpg";
        hv_Extensions_COPY_INP_TMP[6] = "jpeg";
        hv_Extensions_COPY_INP_TMP[7] = "jp2";
        hv_Extensions_COPY_INP_TMP[8] = "jxr";
        hv_Extensions_COPY_INP_TMP[9] = "png";
        hv_Extensions_COPY_INP_TMP[10] = "pcx";
        hv_Extensions_COPY_INP_TMP[11] = "ras";
        hv_Extensions_COPY_INP_TMP[12] = "xwd";
        hv_Extensions_COPY_INP_TMP[13] = "pbm";
        hv_Extensions_COPY_INP_TMP[14] = "pnm";
        hv_Extensions_COPY_INP_TMP[15] = "pgm";
        hv_Extensions_COPY_INP_TMP[16] = "ppm";
        //
      }
      hv_ImageFiles.Dispose();
      hv_ImageFiles = new HTuple();
      //Loop through all given image directories.
      for (hv_ImageDirectoryIndex=0; (int)hv_ImageDirectoryIndex<=(int)((new HTuple(hv_ImageDirectory.TupleLength()
          ))-1); hv_ImageDirectoryIndex = (int)hv_ImageDirectoryIndex + 1)
      {
        hv_ImageFilesTmp.Dispose();
        hv_ImageFilesTmp = new HTuple();
        hv_CurrentImageDirectory.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_CurrentImageDirectory = hv_ImageDirectory.TupleSelect(
            hv_ImageDirectoryIndex);
        }
        if ((int)(new HTuple(hv_CurrentImageDirectory.TupleEqual(""))) != 0)
        {
          hv_CurrentImageDirectory.Dispose();
          hv_CurrentImageDirectory = ".";
        }
        hv_HalconImages.Dispose();
        HOperatorSet.GetSystem("image_dir", out hv_HalconImages);
        hv_OS.Dispose();
        HOperatorSet.GetSystem("operating_system", out hv_OS);
        if ((int)(new HTuple(((hv_OS.TupleSubstr(0,2))).TupleEqual("Win"))) != 0)
        {
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_HalconImages = hv_HalconImages.TupleSplit(
              ";");
          hv_HalconImages.Dispose();
          hv_HalconImages = ExpTmpLocalVar_HalconImages;
          }
          }
        }
        else
        {
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_HalconImages = hv_HalconImages.TupleSplit(
              ":");
          hv_HalconImages.Dispose();
          hv_HalconImages = ExpTmpLocalVar_HalconImages;
          }
          }
        }
        hv_Directories.Dispose();
        hv_Directories = new HTuple(hv_CurrentImageDirectory);
        for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_HalconImages.TupleLength()
            ))-1); hv_Index = (int)hv_Index + 1)
        {
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_Directories = hv_Directories.TupleConcat(
              ((hv_HalconImages.TupleSelect(hv_Index))+"/")+hv_CurrentImageDirectory);
          hv_Directories.Dispose();
          hv_Directories = ExpTmpLocalVar_Directories;
          }
          }
        }
        hv_Length.Dispose();
        HOperatorSet.TupleStrlen(hv_Directories, out hv_Length);
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_NetworkDrive.Dispose();
        HOperatorSet.TupleGenConst(new HTuple(hv_Length.TupleLength()), 0, out hv_NetworkDrive);
        }
        if ((int)(new HTuple(((hv_OS.TupleSubstr(0,2))).TupleEqual("Win"))) != 0)
        {
          for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_Length.TupleLength()
              ))-1); hv_Index = (int)hv_Index + 1)
          {
            if ((int)(new HTuple(((((hv_Directories.TupleSelect(hv_Index))).TupleStrlen()
                )).TupleGreater(1))) != 0)
            {
              using (HDevDisposeHelper dh = new HDevDisposeHelper())
              {
              hv_Substring.Dispose();
              HOperatorSet.TupleStrFirstN(hv_Directories.TupleSelect(hv_Index), 1, 
                  out hv_Substring);
              }
              if ((int)((new HTuple(hv_Substring.TupleEqual("//"))).TupleOr(new HTuple(hv_Substring.TupleEqual(
                  "\\\\")))) != 0)
              {
                if (hv_NetworkDrive == null)
                  hv_NetworkDrive = new HTuple();
                hv_NetworkDrive[hv_Index] = 1;
              }
            }
          }
        }
        hv_ImageFilesTmp.Dispose();
        hv_ImageFilesTmp = new HTuple();
        for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_Directories.TupleLength()
            ))-1); hv_Index = (int)hv_Index + 1)
        {
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          hv_FileExists.Dispose();
          HOperatorSet.FileExists(hv_Directories.TupleSelect(hv_Index), out hv_FileExists);
          }
          if ((int)(hv_FileExists) != 0)
          {
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
            hv_AllFiles.Dispose();
            HOperatorSet.ListFiles(hv_Directories.TupleSelect(hv_Index), (new HTuple("files")).TupleConcat(
                hv_Options), out hv_AllFiles);
            }
            hv_ImageFilesTmp.Dispose();
            hv_ImageFilesTmp = new HTuple();
            for (hv_i=0; (int)hv_i<=(int)((new HTuple(hv_Extensions_COPY_INP_TMP.TupleLength()
                ))-1); hv_i = (int)hv_i + 1)
            {
              using (HDevDisposeHelper dh = new HDevDisposeHelper())
              {
              hv_Selection.Dispose();
              HOperatorSet.TupleRegexpSelect(hv_AllFiles, (((".*"+(hv_Extensions_COPY_INP_TMP.TupleSelect(
                  hv_i)))+"$")).TupleConcat("ignore_case"), out hv_Selection);
              }
              using (HDevDisposeHelper dh = new HDevDisposeHelper())
              {
              {
              HTuple 
                ExpTmpLocalVar_ImageFilesTmp = hv_ImageFilesTmp.TupleConcat(
                  hv_Selection);
              hv_ImageFilesTmp.Dispose();
              hv_ImageFilesTmp = ExpTmpLocalVar_ImageFilesTmp;
              }
              }
            }
            {
            HTuple ExpTmpOutVar_0;
            HOperatorSet.TupleRegexpReplace(hv_ImageFilesTmp, (new HTuple("\\\\")).TupleConcat(
                "replace_all"), "/", out ExpTmpOutVar_0);
            hv_ImageFilesTmp.Dispose();
            hv_ImageFilesTmp = ExpTmpOutVar_0;
            }
            if ((int)(hv_NetworkDrive.TupleSelect(hv_Index)) != 0)
            {
              {
              HTuple ExpTmpOutVar_0;
              HOperatorSet.TupleRegexpReplace(hv_ImageFilesTmp, (new HTuple("//")).TupleConcat(
                  "replace_all"), "/", out ExpTmpOutVar_0);
              hv_ImageFilesTmp.Dispose();
              hv_ImageFilesTmp = ExpTmpOutVar_0;
              }
              using (HDevDisposeHelper dh = new HDevDisposeHelper())
              {
              {
              HTuple 
                ExpTmpLocalVar_ImageFilesTmp = "/"+hv_ImageFilesTmp;
              hv_ImageFilesTmp.Dispose();
              hv_ImageFilesTmp = ExpTmpLocalVar_ImageFilesTmp;
              }
              }
            }
            else
            {
              {
              HTuple ExpTmpOutVar_0;
              HOperatorSet.TupleRegexpReplace(hv_ImageFilesTmp, (new HTuple("//")).TupleConcat(
                  "replace_all"), "/", out ExpTmpOutVar_0);
              hv_ImageFilesTmp.Dispose();
              hv_ImageFilesTmp = ExpTmpOutVar_0;
              }
            }
            break;
          }
        }
        //Concatenate the output image paths.
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_ImageFiles = hv_ImageFiles.TupleConcat(
            hv_ImageFilesTmp);
        hv_ImageFiles.Dispose();
        hv_ImageFiles = ExpTmpLocalVar_ImageFiles;
        }
        }
      }

      hv_Extensions_COPY_INP_TMP.Dispose();
      hv_ImageDirectoryIndex.Dispose();
      hv_ImageFilesTmp.Dispose();
      hv_CurrentImageDirectory.Dispose();
      hv_HalconImages.Dispose();
      hv_OS.Dispose();
      hv_Directories.Dispose();
      hv_Index.Dispose();
      hv_Length.Dispose();
      hv_NetworkDrive.Dispose();
      hv_Substring.Dispose();
      hv_FileExists.Dispose();
      hv_AllFiles.Dispose();
      hv_i.Dispose();
      hv_Selection.Dispose();

      return;
    }
    catch (HalconException HDevExpDefaultException)
    {

      hv_Extensions_COPY_INP_TMP.Dispose();
      hv_ImageDirectoryIndex.Dispose();
      hv_ImageFilesTmp.Dispose();
      hv_CurrentImageDirectory.Dispose();
      hv_HalconImages.Dispose();
      hv_OS.Dispose();
      hv_Directories.Dispose();
      hv_Index.Dispose();
      hv_Length.Dispose();
      hv_NetworkDrive.Dispose();
      hv_Substring.Dispose();
      hv_FileExists.Dispose();
      hv_AllFiles.Dispose();
      hv_i.Dispose();
      hv_Selection.Dispose();

      throw HDevExpDefaultException;
    }
  }

  // Chapter: Graphics / Text
  // Short Description: Set font independent of OS 
  public void set_display_font (HTuple hv_WindowHandle, HTuple hv_Size, HTuple hv_Font, 
      HTuple hv_Bold, HTuple hv_Slant)
  {



    // Local iconic variables 

    // Local control variables 

    HTuple hv_OS = new HTuple(), hv_Fonts = new HTuple();
    HTuple hv_Style = new HTuple(), hv_Exception = new HTuple();
    HTuple hv_AvailableFonts = new HTuple(), hv_Fdx = new HTuple();
    HTuple hv_Indices = new HTuple();
    HTuple   hv_Font_COPY_INP_TMP = new HTuple(hv_Font);
    HTuple   hv_Size_COPY_INP_TMP = new HTuple(hv_Size);

    // Initialize local and output iconic variables 
    try
    {
      //This procedure sets the text font of the current window with
      //the specified attributes.
      //
      //Input parameters:
      //WindowHandle: The graphics window for which the font will be set
      //Size: The font size. If Size=-1, the default of 16 is used.
      //Bold: If set to 'true', a bold font is used
      //Slant: If set to 'true', a slanted font is used
      //
      hv_OS.Dispose();
      HOperatorSet.GetSystem("operating_system", out hv_OS);
      if ((int)((new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(new HTuple()))).TupleOr(
          new HTuple(hv_Size_COPY_INP_TMP.TupleEqual(-1)))) != 0)
      {
        hv_Size_COPY_INP_TMP.Dispose();
        hv_Size_COPY_INP_TMP = 16;
      }
      if ((int)(new HTuple(((hv_OS.TupleSubstr(0,2))).TupleEqual("Win"))) != 0)
      {
        //Restore previous behaviour
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_Size = ((1.13677*hv_Size_COPY_INP_TMP)).TupleInt()
            ;
        hv_Size_COPY_INP_TMP.Dispose();
        hv_Size_COPY_INP_TMP = ExpTmpLocalVar_Size;
        }
        }
      }
      else
      {
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_Size = hv_Size_COPY_INP_TMP.TupleInt()
            ;
        hv_Size_COPY_INP_TMP.Dispose();
        hv_Size_COPY_INP_TMP = ExpTmpLocalVar_Size;
        }
        }
      }
      if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("Courier"))) != 0)
      {
        hv_Fonts.Dispose();
        hv_Fonts = new HTuple();
        hv_Fonts[0] = "Courier";
        hv_Fonts[1] = "Courier 10 Pitch";
        hv_Fonts[2] = "Courier New";
        hv_Fonts[3] = "CourierNew";
        hv_Fonts[4] = "Liberation Mono";
      }
      else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("mono"))) != 0)
      {
        hv_Fonts.Dispose();
        hv_Fonts = new HTuple();
        hv_Fonts[0] = "Consolas";
        hv_Fonts[1] = "Menlo";
        hv_Fonts[2] = "Courier";
        hv_Fonts[3] = "Courier 10 Pitch";
        hv_Fonts[4] = "FreeMono";
        hv_Fonts[5] = "Liberation Mono";
      }
      else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("sans"))) != 0)
      {
        hv_Fonts.Dispose();
        hv_Fonts = new HTuple();
        hv_Fonts[0] = "Luxi Sans";
        hv_Fonts[1] = "DejaVu Sans";
        hv_Fonts[2] = "FreeSans";
        hv_Fonts[3] = "Arial";
        hv_Fonts[4] = "Liberation Sans";
      }
      else if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual("serif"))) != 0)
      {
        hv_Fonts.Dispose();
        hv_Fonts = new HTuple();
        hv_Fonts[0] = "Times New Roman";
        hv_Fonts[1] = "Luxi Serif";
        hv_Fonts[2] = "DejaVu Serif";
        hv_Fonts[3] = "FreeSerif";
        hv_Fonts[4] = "Utopia";
        hv_Fonts[5] = "Liberation Serif";
      }
      else
      {
        hv_Fonts.Dispose();
        hv_Fonts = new HTuple(hv_Font_COPY_INP_TMP);
      }
      hv_Style.Dispose();
      hv_Style = "";
      if ((int)(new HTuple(hv_Bold.TupleEqual("true"))) != 0)
      {
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_Style = hv_Style+"Bold";
        hv_Style.Dispose();
        hv_Style = ExpTmpLocalVar_Style;
        }
        }
      }
      else if ((int)(new HTuple(hv_Bold.TupleNotEqual("false"))) != 0)
      {
        hv_Exception.Dispose();
        hv_Exception = "Wrong value of control parameter Bold";
        throw new HalconException(hv_Exception);
      }
      if ((int)(new HTuple(hv_Slant.TupleEqual("true"))) != 0)
      {
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_Style = hv_Style+"Italic";
        hv_Style.Dispose();
        hv_Style = ExpTmpLocalVar_Style;
        }
        }
      }
      else if ((int)(new HTuple(hv_Slant.TupleNotEqual("false"))) != 0)
      {
        hv_Exception.Dispose();
        hv_Exception = "Wrong value of control parameter Slant";
        throw new HalconException(hv_Exception);
      }
      if ((int)(new HTuple(hv_Style.TupleEqual(""))) != 0)
      {
        hv_Style.Dispose();
        hv_Style = "Normal";
      }
      hv_AvailableFonts.Dispose();
      HOperatorSet.QueryFont(hv_WindowHandle, out hv_AvailableFonts);
      hv_Font_COPY_INP_TMP.Dispose();
      hv_Font_COPY_INP_TMP = "";
      for (hv_Fdx=0; (int)hv_Fdx<=(int)((new HTuple(hv_Fonts.TupleLength()))-1); hv_Fdx = (int)hv_Fdx + 1)
      {
        hv_Indices.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_Indices = hv_AvailableFonts.TupleFind(
            hv_Fonts.TupleSelect(hv_Fdx));
        }
        if ((int)(new HTuple((new HTuple(hv_Indices.TupleLength())).TupleGreater(
            0))) != 0)
        {
          if ((int)(new HTuple(((hv_Indices.TupleSelect(0))).TupleGreaterEqual(0))) != 0)
          {
            hv_Font_COPY_INP_TMP.Dispose();
            using (HDevDisposeHelper dh = new HDevDisposeHelper())
            {
            hv_Font_COPY_INP_TMP = hv_Fonts.TupleSelect(
                hv_Fdx);
            }
            break;
          }
        }
      }
      if ((int)(new HTuple(hv_Font_COPY_INP_TMP.TupleEqual(""))) != 0)
      {
        throw new HalconException("Wrong value of control parameter Font");
      }
      using (HDevDisposeHelper dh = new HDevDisposeHelper())
      {
      {
      HTuple 
        ExpTmpLocalVar_Font = (((hv_Font_COPY_INP_TMP+"-")+hv_Style)+"-")+hv_Size_COPY_INP_TMP;
      hv_Font_COPY_INP_TMP.Dispose();
      hv_Font_COPY_INP_TMP = ExpTmpLocalVar_Font;
      }
      }
      HOperatorSet.SetFont(hv_WindowHandle, hv_Font_COPY_INP_TMP);

      hv_Font_COPY_INP_TMP.Dispose();
      hv_Size_COPY_INP_TMP.Dispose();
      hv_OS.Dispose();
      hv_Fonts.Dispose();
      hv_Style.Dispose();
      hv_Exception.Dispose();
      hv_AvailableFonts.Dispose();
      hv_Fdx.Dispose();
      hv_Indices.Dispose();

      return;
    }
    catch (HalconException HDevExpDefaultException)
    {

      hv_Font_COPY_INP_TMP.Dispose();
      hv_Size_COPY_INP_TMP.Dispose();
      hv_OS.Dispose();
      hv_Fonts.Dispose();
      hv_Style.Dispose();
      hv_Exception.Dispose();
      hv_AvailableFonts.Dispose();
      hv_Fdx.Dispose();
      hv_Indices.Dispose();

      throw HDevExpDefaultException;
    }
  }

  // Local procedures 
  public void get_object_images (HTuple hv_Directory, out HTuple hv_ObjectNames, 
      out HTuple hv_ObjectIDs, out HTuple hv_ImageFileNames, out HTuple hv_TrainingObjectNames, 
      out HTuple hv_TrainingObjectIDs, out HTuple hv_TrainingImageFileNames)
  {



    // Local iconic variables 

    // Local control variables 

    HTuple hv_Matches = new HTuple(), hv_UniqObjectNames = new HTuple();
    HTuple hv_Index = new HTuple(), hv_Indices = new HTuple();
    HTuple hv_ObjectIDsUniq = new HTuple(), hv_ObjectID = new HTuple();
    HTuple hv_I = new HTuple();
    // Initialize local and output iconic variables 
    hv_ObjectNames = new HTuple();
    hv_ObjectIDs = new HTuple();
    hv_ImageFileNames = new HTuple();
    hv_TrainingObjectNames = new HTuple();
    hv_TrainingObjectIDs = new HTuple();
    hv_TrainingImageFileNames = new HTuple();
    try
    {
      //Get all images in the directory
      hv_ImageFileNames.Dispose();
      list_image_files(hv_Directory, "default", new HTuple(), out hv_ImageFileNames);
      using (HDevDisposeHelper dh = new HDevDisposeHelper())
      {
      {
      HTuple 
        ExpTmpLocalVar_ImageFileNames = hv_ImageFileNames.TupleSort()
          ;
      hv_ImageFileNames.Dispose();
      hv_ImageFileNames = ExpTmpLocalVar_ImageFileNames;
      }
      }
      hv_Matches.Dispose();
      HOperatorSet.TupleRegexpMatch(hv_ImageFileNames, ".*/(.*)\\..{3}$", out hv_Matches);
      hv_ObjectNames.Dispose();
      HOperatorSet.TupleRegexpReplace(hv_Matches, "_[0-9]+", "", out hv_ObjectNames);
      hv_UniqObjectNames.Dispose();
      HOperatorSet.TupleUniq(hv_ObjectNames, out hv_UniqObjectNames);
      hv_ObjectIDs.Dispose();
      hv_ObjectIDs = new HTuple();
      for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_ObjectNames.TupleLength()
          ))-1); hv_Index = (int)hv_Index + 1)
      {
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_Indices.Dispose();
        HOperatorSet.TupleFind(hv_UniqObjectNames, hv_ObjectNames.TupleSelect(hv_Index), 
            out hv_Indices);
        }
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_ObjectIDs = hv_ObjectIDs.TupleConcat(
            hv_Indices);
        hv_ObjectIDs.Dispose();
        hv_ObjectIDs = ExpTmpLocalVar_ObjectIDs;
        }
        }
      }
      //Extract the training images
      hv_TrainingObjectNames.Dispose();
      hv_TrainingObjectNames = new HTuple();
      hv_TrainingObjectIDs.Dispose();
      hv_TrainingObjectIDs = new HTuple();
      hv_TrainingImageFileNames.Dispose();
      hv_TrainingImageFileNames = new HTuple();
      hv_ObjectIDsUniq.Dispose();
      HOperatorSet.TupleUniq(hv_ObjectIDs, out hv_ObjectIDsUniq);
      for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_ObjectIDsUniq.TupleLength()
          ))-1); hv_Index = (int)hv_Index + 1)
      {
        hv_ObjectID.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_ObjectID = hv_ObjectIDsUniq.TupleSelect(
            hv_Index);
        }
        hv_Indices.Dispose();
        HOperatorSet.TupleFind(hv_ObjectIDs, hv_ObjectID, out hv_Indices);
        //Arbitrarily chose the first image of the same ID
        //as the training image
        hv_I.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_I = hv_Indices.TupleSelect(
            0);
        }
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_TrainingObjectNames = hv_TrainingObjectNames.TupleConcat(
            hv_ObjectNames.TupleSelect(hv_I));
        hv_TrainingObjectNames.Dispose();
        hv_TrainingObjectNames = ExpTmpLocalVar_TrainingObjectNames;
        }
        }
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_TrainingObjectIDs = hv_TrainingObjectIDs.TupleConcat(
            hv_ObjectIDs.TupleSelect(hv_I));
        hv_TrainingObjectIDs.Dispose();
        hv_TrainingObjectIDs = ExpTmpLocalVar_TrainingObjectIDs;
        }
        }
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        {
        HTuple 
          ExpTmpLocalVar_TrainingImageFileNames = hv_TrainingImageFileNames.TupleConcat(
            hv_ImageFileNames.TupleSelect(hv_I));
        hv_TrainingImageFileNames.Dispose();
        hv_TrainingImageFileNames = ExpTmpLocalVar_TrainingImageFileNames;
        }
        }
      }

      hv_Matches.Dispose();
      hv_UniqObjectNames.Dispose();
      hv_Index.Dispose();
      hv_Indices.Dispose();
      hv_ObjectIDsUniq.Dispose();
      hv_ObjectID.Dispose();
      hv_I.Dispose();

      return;
    }
    catch (HalconException HDevExpDefaultException)
    {

      hv_Matches.Dispose();
      hv_UniqObjectNames.Dispose();
      hv_Index.Dispose();
      hv_Indices.Dispose();
      hv_ObjectIDsUniq.Dispose();
      hv_ObjectID.Dispose();
      hv_I.Dispose();

      throw HDevExpDefaultException;
    }
  }

#if !NO_EXPORT_MAIN
  // Main procedure 
  private void action()
  {


    // Local iconic variables 

    HObject ho_Image;

    // Local control variables 

    HTuple hv_DoTraining = new HTuple(), hv_ImageDirectory = new HTuple();
    HTuple hv_IdentifierFileName = new HTuple(), hv_ObjectNames = new HTuple();
    HTuple hv_ObjectIDs = new HTuple(), hv_ImageFileNames = new HTuple();
    HTuple hv_TrainingObjectNames = new HTuple(), hv_TrainingObjectIDs = new HTuple();
    HTuple hv_TrainingImageFileNames = new HTuple(), hv_WindowHandle = new HTuple();
    HTuple hv_FileExists = new HTuple(), hv_SampleIdentifier = new HTuple();
    HTuple hv_Index = new HTuple(), hv_ObjectSampleIdx = new HTuple();
    HTuple hv_Errors = new HTuple(), hv_RandomIndices = new HTuple();
    HTuple hv_NumTests = new HTuple(), hv_I = new HTuple();
    HTuple hv_ImageFileName = new HTuple(), hv_ObjectID = new HTuple();
    HTuple hv_ObjectName = new HTuple(), hv_ObjectIdx = new HTuple();
    HTuple hv_Rating = new HTuple(), hv_ResultObjectName = new HTuple();
    HTuple hv_Color = new HTuple();
    // Initialize local and output iconic variables 
    HOperatorSet.GenEmptyObj(out ho_Image);
    try
    {
      //This program shows how to use the sample-based identification
      //to identify different objects.
      //First, the sample identifier is created, prepared, trained,
      //and written to disk. In later runs, the trained sample
      //identifier is read from disk to avoid the time-consuming
      //preparation step. In the run-time phase, the trained sample
      //identifier is used to identify new images of vegetables.
      //
      dev_update_off();
      //
      //Set DoTraining to true to force a new training
      hv_DoTraining.Dispose();
      hv_DoTraining = 0;
      //
      //Get the image file names, the object names and IDs
      hv_ImageDirectory.Dispose();
      hv_ImageDirectory = "food/wine_label";
      hv_IdentifierFileName.Dispose();
      hv_IdentifierFileName = "wine_label.sid";
      hv_ObjectNames.Dispose();hv_ObjectIDs.Dispose();hv_ImageFileNames.Dispose();hv_TrainingObjectNames.Dispose();hv_TrainingObjectIDs.Dispose();hv_TrainingImageFileNames.Dispose();
      get_object_images(hv_ImageDirectory, out hv_ObjectNames, out hv_ObjectIDs, 
          out hv_ImageFileNames, out hv_TrainingObjectNames, out hv_TrainingObjectIDs, 
          out hv_TrainingImageFileNames);
      //
      using (HDevDisposeHelper dh = new HDevDisposeHelper())
      {
      ho_Image.Dispose();
      HOperatorSet.ReadImage(out ho_Image, hv_TrainingImageFileNames.TupleSelect(
          0));
      }
      if (HDevWindowStack.IsOpen())
      {
        HOperatorSet.CloseWindow(HDevWindowStack.Pop());
      }
      hv_WindowHandle.Dispose();
      dev_open_window_fit_image(ho_Image, 0, 0, -1, -1, out hv_WindowHandle);
      set_display_font(hv_WindowHandle, 14, "mono", "true", "false");
      //
      hv_FileExists.Dispose();
      HOperatorSet.FileExists(hv_IdentifierFileName, out hv_FileExists);
      if ((int)(hv_FileExists.TupleAnd(hv_DoTraining.TupleNot())) != 0)
      {
        disp_message(hv_WindowHandle, "Read the trained sample identifier ...", "window", 
            12, 12, "black", "true");
        hv_SampleIdentifier.Dispose();
        HOperatorSet.ReadSampleIdentifier(hv_IdentifierFileName, out hv_SampleIdentifier);
      }
      else
      {
        disp_message(hv_WindowHandle, new HTuple("Create, prepare and train a new sample identifier"), 
            "window", 12, 12, "black", "true");
        disp_continue_message(hv_WindowHandle, "black", "true");
        // stop(...); only in hdevelop
        //If the trained sample identifier is not available yet we create
        //a new sample identifier. Because we have color images available
        //we want to use the color in addition to the texture information
        //for identification.
        hv_SampleIdentifier.Dispose();
        HOperatorSet.CreateSampleIdentifier("add_color_info", "true", out hv_SampleIdentifier);
        //In order to speed up this special application, we can reduce the
        //internal image size from the default (0.5 megapixels) to 0.1
        //megapixels without a decrease of the identification rate. Note
        //that for other applications, the optimum value for the internal
        //image size must be determined individually.
        HOperatorSet.SetSampleIdentifierParam(hv_SampleIdentifier, "image_resize_value", 
            0.1);
        //Prepare the sample identifier by adding some representive sample
        //images that show the different types of objects. For this,
        //we simply use the training images.
        for (hv_Index=0; (int)hv_Index<=(int)((new HTuple(hv_TrainingImageFileNames.TupleLength()
            ))-1); hv_Index = (int)hv_Index + 1)
        {
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          ho_Image.Dispose();
          HOperatorSet.ReadImage(out ho_Image, hv_TrainingImageFileNames.TupleSelect(
              hv_Index));
          }
          if (HDevWindowStack.IsOpen())
          {
            HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
          }
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          disp_message(hv_WindowHandle, "Add a sample of "+(hv_TrainingObjectNames.TupleSelect(
              hv_Index)), "window", 12, 12, "black", "true");
          }
          //Add the current image as preparation data. Because we know
          //the object ID, we can use it in order to label the preparation
          //data. Thus, we can later reuse it for training and do not need to
          //add additional training data.
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          hv_ObjectSampleIdx.Dispose();
          HOperatorSet.AddSampleIdentifierPreparationData(ho_Image, hv_SampleIdentifier, 
              hv_TrainingObjectIDs.TupleSelect(hv_Index), new HTuple(), new HTuple(), 
              out hv_ObjectSampleIdx);
          }
          //Optionally add the object name to the sample identifier for
          //convinience reasons.
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          HOperatorSet.SetSampleIdentifierObjectInfo(hv_SampleIdentifier, hv_TrainingObjectIDs.TupleSelect(
              hv_Index), "preparation_object_name", hv_TrainingObjectNames.TupleSelect(
              hv_Index));
          }
        }
        //Now, we can prepare the identifier
        if (HDevWindowStack.IsOpen())
        {
          HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
        }
        disp_message(hv_WindowHandle, (new HTuple("Prepare and train the sample identifier")).TupleConcat(
            "(this might take some seconds)"), "window", 12, 12, "black", "true");
        HOperatorSet.PrepareSampleIdentifier(hv_SampleIdentifier, "true", new HTuple(), 
            new HTuple());
        //Use the preparation data to train the identifier
        HOperatorSet.TrainSampleIdentifier(hv_SampleIdentifier, new HTuple(), new HTuple());
        //Write the trained identifier to disk
        HOperatorSet.WriteSampleIdentifier(hv_SampleIdentifier, hv_IdentifierFileName);
      }
      disp_continue_message(hv_WindowHandle, "black", "true");
      // stop(...); only in hdevelop
      //
      //Identify new objects
      hv_Errors.Dispose();
      hv_Errors = 0;
      hv_RandomIndices.Dispose();
      using (HDevDisposeHelper dh = new HDevDisposeHelper())
      {
      hv_RandomIndices = (HTuple.TupleRand(
          new HTuple(hv_ImageFileNames.TupleLength()))).TupleSortIndex();
      }
      //Choose how many images should be tested
      hv_NumTests.Dispose();
      hv_NumTests = 50;
      //Use the following line to test all images
      //NumTests := |RandomIndices|
      HTuple end_val77 = ((hv_NumTests.TupleConcat(
          new HTuple(hv_RandomIndices.TupleLength())))).TupleMin();
      HTuple step_val77 = 1;
      for (hv_I=1; hv_I.Continue(end_val77, step_val77); hv_I = hv_I.TupleAdd(step_val77))
      {
        //Randomly select a run-time image
        hv_Index.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_Index = hv_RandomIndices.TupleSelect(
            hv_I-1);
        }
        hv_ImageFileName.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_ImageFileName = hv_ImageFileNames.TupleSelect(
            hv_Index);
        }
        hv_ObjectID.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_ObjectID = hv_ObjectIDs.TupleSelect(
            hv_Index);
        }
        hv_ObjectName.Dispose();
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        hv_ObjectName = hv_ObjectNames.TupleSelect(
            hv_Index);
        }
        ho_Image.Dispose();
        HOperatorSet.ReadImage(out ho_Image, hv_ImageFileName);
        //Apply the sample identifier to identify the new objects
        hv_ObjectIdx.Dispose();hv_Rating.Dispose();
        HOperatorSet.ApplySampleIdentifier(ho_Image, hv_SampleIdentifier, 1, 0, new HTuple(), 
            new HTuple(), out hv_ObjectIdx, out hv_Rating);
        //Get the name of the identified object for visualization purposes
        hv_ResultObjectName.Dispose();
        HOperatorSet.GetSampleIdentifierObjectInfo(hv_SampleIdentifier, hv_ObjectIdx, 
            "training_object_name", out hv_ResultObjectName);
        if ((int)(new HTuple(hv_ObjectIdx.TupleNotEqual(hv_ObjectID))) != 0)
        {
          //Use red color for incorrect identifications
          hv_Color.Dispose();
          hv_Color = "red";
          using (HDevDisposeHelper dh = new HDevDisposeHelper())
          {
          {
          HTuple 
            ExpTmpLocalVar_Errors = hv_Errors+1;
          hv_Errors.Dispose();
          hv_Errors = ExpTmpLocalVar_Errors;
          }
          }
        }
        else
        {
          hv_Color.Dispose();
          hv_Color = "forest green";
        }
        if (HDevWindowStack.IsOpen())
        {
          HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
        }
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        disp_message(hv_WindowHandle, "Current object: "+hv_ObjectName, "window", 
            12, 12, "black", "true");
        }
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        disp_message(hv_WindowHandle, "Identified as:  "+hv_ResultObjectName, "window", 
            30, 12, hv_Color, "true");
        }
        using (HDevDisposeHelper dh = new HDevDisposeHelper())
        {
        disp_message(hv_WindowHandle, ((((("Error rate: ("+hv_Errors)+"/")+hv_I)+") = ")+((((100.0*hv_Errors)/hv_I)).TupleString(
            ".1f")))+"%", "window", 50, 12, "black", "true");
        }
      }
    }
    catch (HalconException HDevExpDefaultException)
    {
      ho_Image.Dispose();

      hv_DoTraining.Dispose();
      hv_ImageDirectory.Dispose();
      hv_IdentifierFileName.Dispose();
      hv_ObjectNames.Dispose();
      hv_ObjectIDs.Dispose();
      hv_ImageFileNames.Dispose();
      hv_TrainingObjectNames.Dispose();
      hv_TrainingObjectIDs.Dispose();
      hv_TrainingImageFileNames.Dispose();
      hv_WindowHandle.Dispose();
      hv_FileExists.Dispose();
      hv_SampleIdentifier.Dispose();
      hv_Index.Dispose();
      hv_ObjectSampleIdx.Dispose();
      hv_Errors.Dispose();
      hv_RandomIndices.Dispose();
      hv_NumTests.Dispose();
      hv_I.Dispose();
      hv_ImageFileName.Dispose();
      hv_ObjectID.Dispose();
      hv_ObjectName.Dispose();
      hv_ObjectIdx.Dispose();
      hv_Rating.Dispose();
      hv_ResultObjectName.Dispose();
      hv_Color.Dispose();

      throw HDevExpDefaultException;
    }
    ho_Image.Dispose();

    hv_DoTraining.Dispose();
    hv_ImageDirectory.Dispose();
    hv_IdentifierFileName.Dispose();
    hv_ObjectNames.Dispose();
    hv_ObjectIDs.Dispose();
    hv_ImageFileNames.Dispose();
    hv_TrainingObjectNames.Dispose();
    hv_TrainingObjectIDs.Dispose();
    hv_TrainingImageFileNames.Dispose();
    hv_WindowHandle.Dispose();
    hv_FileExists.Dispose();
    hv_SampleIdentifier.Dispose();
    hv_Index.Dispose();
    hv_ObjectSampleIdx.Dispose();
    hv_Errors.Dispose();
    hv_RandomIndices.Dispose();
    hv_NumTests.Dispose();
    hv_I.Dispose();
    hv_ImageFileName.Dispose();
    hv_ObjectID.Dispose();
    hv_ObjectName.Dispose();
    hv_ObjectIdx.Dispose();
    hv_Rating.Dispose();
    hv_ResultObjectName.Dispose();
    hv_Color.Dispose();

  }

#endif


}
#if !(NO_EXPORT_MAIN || NO_EXPORT_APP_MAIN)
public class HDevelopExportApp
{
  static void Main(string[] args)
  {
    new HDevelopExport();
  }
}
#endif


### 使用Halcon进行OCR文字识别C#实现 对于希望在C#环境中利用Halcon执行光学字符识别(OCR)的任务,可以采用MVTec Halcon库中的.NET接口来完成这一目标[^1]。下面提供了一个简单的实例,展示了如何初始化Halcon环境并调用其功能来进行基本的文字识别操作。 #### 初始化与配置 为了使程序能够正常工作,首先需要确保已经安装了适用于Windows系统的Halcon SDK以及相应的Visual Studio扩展包。接着,在项目中添加对`halcondotnet.dll`文件的引用以便访问必要的类和方法。 ```csharp using System; using Mvtec.HalconDotNet; class Program { static void Main() { try { // 创建一个新的图像对象用于存储读取到的数据 HObject ho_Image = new HObject(); // 加载测试图片(这里假设是一张包含印刷体英文单词的手写样本) string imagePath = @"path\to\your\image.png"; ReadImage(out ho_Image, imagePath); // 接下来定义一些变量保存处理过程中的中间结果... HTuple hv_ResultString; // 调用函数执行实际的OCR任务 PerformOCROperation(ho_Image, out hv_ResultString); Console.WriteLine($"Recognized Text: {hv_ResultString}"); } catch (Exception ex){ Console.Error.WriteLine(ex.Message); } } private static void PerformOCROperation(HObject image, out HTuple resultText){ // 实现具体的OCR逻辑,比如设置参数、创建分类器模型等 // 这里简化为直接返回预设字符串作为演示用途 // 正常情况下应替换为此处代码以真正获取识别的结果: /* * ... * OCR_ClassifyDocument(image, ... ,out resultText); */ resultText = "Sample recognized text"; } } ``` 此段代码片段仅作为一个入门级的例子,并未深入探讨所有可能影响最终效果的因素,如字体样式支持范围、倾斜校正算法的选择或是多语言混合场景下的优化策略等问题。如果想要获得更精确可靠的性能表现,则建议进一步研究官方文档和技术白皮书内的高级特性介绍。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值