• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

halcon 导出C++程序和visualstdio2019配置环境,在控制台显示一张图片

武飞扬头像
Bishop077
帮助2

1.halcon代码

  1.  
    read_image (DSC_0097, 'C:/Users/Administrator/Desktop/DSC_0097.JPG')
  2.  
    dev_open_window_fit_image(DSC_0097,0,0,-1,-1,WindowHandle)
  3.  
    dev_clear_window()
  4.  
    dev_display(DSC_0097)

 学新通

2.选择文件,导出

学新通

HALCON/C

 学新通

  1.  
    保存到目标程序
  2.  
     
  3.  
    ///
  4.  
    // File generated by HDevelop for HALCON/C Version 17.12
  5.  
    ///
  6.  
     
  7.  
    #ifndef __APPLE__
  8.  
    #  include "HalconCpp.h"
  9.  
    #  include "HDevThread.h"
  10.  
    #  if defined(__linux__) && !defined(__arm__) && !defined(NO_EXPORT_APP_MAIN)
  11.  
    #    include <X11/Xlib.h>
  12.  
    #  endif
  13.  
    #else
  14.  
    #  ifndef HC_LARGE_IMAGES
  15.  
    #    include <HALCONCpp/HalconCpp.h>
  16.  
    #    include <HALCONCpp/HDevThread.h>
  17.  
    #  else
  18.  
    #    include <HALCONCppxl/HalconCpp.h>
  19.  
    #    include <HALCONCppxl/HDevThread.h>
  20.  
    #  endif
  21.  
    #  include <stdio.h>
  22.  
    #  include <HALCON/HpThread.h>
  23.  
    #  include <CoreFoundation/CFRunLoop.h>
  24.  
    #endif
  25.  
     
  26.  
    using namespace HalconCpp;
  27.  
     
  28.  
    // Procedure declarations 
  29.  
    // Chapter: Develop
  30.  
    // Short Description: Open a new graphics window that preserves the aspect ratio of the given image. 
  31.  
    void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column, 
  32.  
        HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle);
  33.  
     
  34.  
    // Procedures 
  35.  
    // Chapter: Develop
  36.  
    // Short Description: Open a new graphics window that preserves the aspect ratio of the given image. 
  37.  
    void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column, 
  38.  
        HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle)
  39.  
    {
  40.  
     
  41.  
      // Local iconic variables
  42.  
     
  43.  
      // Local control variables
  44.  
      HTuple  hv_MinWidth, hv_MaxWidth, hv_MinHeight;
  45.  
      HTuple  hv_MaxHeight, hv_ResizeFactor, hv_ImageWidth, hv_ImageHeight;
  46.  
      HTuple  hv_TempWidth, hv_TempHeight, hv_WindowWidth, hv_WindowHeight;
  47.  
     
  48.  
      //This procedure opens a new graphics window and adjusts the size
  49.  
      //such that it fits into the limits specified by WidthLimit
  50.  
      //and HeightLimit, but also maintains the correct image aspect ratio.
  51.  
      //
  52.  
      //If it is impossible to match the minimum and maximum extent requirements
  53.  
      //at the same time (f.e. if the image is very long but narrow),
  54.  
      //the maximum value gets a higher priority,
  55.  
      //
  56.  
      //Parse input tuple WidthLimit
  57.  
      if (0 != (HTuple((hv_WidthLimit.TupleLength())==0).TupleOr(hv_WidthLimit<0)))
  58.  
      {
  59.  
        hv_MinWidth = 500;
  60.  
        hv_MaxWidth = 800;
  61.  
      }
  62.  
      else if (0 != ((hv_WidthLimit.TupleLength())==1))
  63.  
      {
  64.  
        hv_MinWidth = 0;
  65.  
        hv_MaxWidth = hv_WidthLimit;
  66.  
      }
  67.  
      else
  68.  
      {
  69.  
        hv_MinWidth = ((const HTuple&)hv_WidthLimit)[0];
  70.  
        hv_MaxWidth = ((const HTuple&)hv_WidthLimit)[1];
  71.  
      }
  72.  
      //Parse input tuple HeightLimit
  73.  
      if (0 != (HTuple((hv_HeightLimit.TupleLength())==0).TupleOr(hv_HeightLimit<0)))
  74.  
      {
  75.  
        hv_MinHeight = 400;
  76.  
        hv_MaxHeight = 600;
  77.  
      }
  78.  
      else if (0 != ((hv_HeightLimit.TupleLength())==1))
  79.  
      {
  80.  
        hv_MinHeight = 0;
  81.  
        hv_MaxHeight = hv_HeightLimit;
  82.  
      }
  83.  
      else
  84.  
      {
  85.  
        hv_MinHeight = ((const HTuple&)hv_HeightLimit)[0];
  86.  
        hv_MaxHeight = ((const HTuple&)hv_HeightLimit)[1];
  87.  
      }
  88.  
      //
  89.  
      //Test, if window size has to be changed.
  90.  
      hv_ResizeFactor = 1;
  91.  
      GetImageSize(ho_Image, &hv_ImageWidth, &hv_ImageHeight);
  92.  
      //First, expand window to the minimum extents (if necessary).
  93.  
      if (0 != (HTuple(hv_MinWidth>hv_ImageWidth).TupleOr(hv_MinHeight>hv_ImageHeight)))
  94.  
      {
  95.  
        hv_ResizeFactor = (((hv_MinWidth.TupleReal())/hv_ImageWidth).TupleConcat((hv_MinHeight.TupleReal())/hv_ImageHeight)).TupleMax();
  96.  
      }
  97.  
      hv_TempWidth = hv_ImageWidth*hv_ResizeFactor;
  98.  
      hv_TempHeight = hv_ImageHeight*hv_ResizeFactor;
  99.  
      //Then, shrink window to maximum extents (if necessary).
  100.  
      if (0 != (HTuple(hv_MaxWidth<hv_TempWidth).TupleOr(hv_MaxHeight<hv_TempHeight)))
  101.  
      {
  102.  
        hv_ResizeFactor = hv_ResizeFactor*((((hv_MaxWidth.TupleReal())/hv_TempWidth).TupleConcat((hv_MaxHeight.TupleReal())/hv_TempHeight)).TupleMin());
  103.  
      }
  104.  
      hv_WindowWidth = hv_ImageWidth*hv_ResizeFactor;
  105.  
      hv_WindowHeight = hv_ImageHeight*hv_ResizeFactor;
  106.  
      //Resize window
  107.  
      SetWindowAttr("background_color","black");
  108.  
      OpenWindow(hv_Row,hv_Column,hv_WindowWidth,hv_WindowHeight,0,"visible","",&(*hv_WindowHandle));
  109.  
      HDevWindowStack::Push((*hv_WindowHandle));
  110.  
      if (HDevWindowStack::IsOpen())
  111.  
        SetPart(HDevWindowStack::GetActive(),0, 0, hv_ImageHeight-1, hv_ImageWidth-1);
  112.  
      return;
  113.  
    }
  114.  
     
  115.  
    #ifndef NO_EXPORT_MAIN
  116.  
    // Main procedure 
  117.  
    void action()
  118.  
    {
  119.  
     
  120.  
      // Local iconic variables
  121.  
      HObject  ho_Img5131;
  122.  
     
  123.  
      // Local control variables
  124.  
      HTuple  hv_WindowHandle;
  125.  
     
  126.  
      ReadImage(&ho_Img5131, "C:/Users/Administrator/Desktop/DSC_0097.JPG");
  127.  
      dev_open_window_fit_image(ho_Img5131, 0, 0, -1, -1, &hv_WindowHandle);
  128.  
      if (HDevWindowStack::IsOpen())
  129.  
        ClearWindow(HDevWindowStack::GetActive());
  130.  
      if (HDevWindowStack::IsOpen())
  131.  
        DispObj(ho_Img5131, HDevWindowStack::GetActive());
  132.  
    }
  133.  
     
  134.  
     
  135.  
    #ifndef NO_EXPORT_APP_MAIN
  136.  
     
  137.  
    #ifdef __APPLE__
  138.  
    // On OS X systems, we must have a CFRunLoop running on the main thread in
  139.  
    // order for the HALCON graphics operators to work correctly, and run the
  140.  
    // action function in a separate thread. A CFRunLoopTimer is used to make sure
  141.  
    // the action function is not called before the CFRunLoop is running.
  142.  
    // Note that starting with macOS 10.12, the run loop may be stopped when a
  143.  
    // window is closed, so we need to put the call to CFRunLoopRun() into a loop
  144.  
    // of its own.
  145.  
    HTuple      gStartMutex;
  146.  
    H_pthread_t gActionThread;
  147.  
    HBOOL       gTerminate = FALSE;
  148.  
     
  149.  
    static void timer_callback(CFRunLoopTimerRef timer, void *info)
  150.  
    {
  151.  
      UnlockMutex(gStartMutex);
  152.  
    }
  153.  
     
  154.  
    static Herror apple_action(void **parameters)
  155.  
    {
  156.  
      // Wait until the timer has fired to start processing.
  157.  
      LockMutex(gStartMutex);
  158.  
      UnlockMutex(gStartMutex);
  159.  
     
  160.  
      try
  161.  
      {
  162.  
        action();
  163.  
      }
  164.  
      catch (HException &exception)
  165.  
      {
  166.  
        fprintf(stderr,"  Error #%u in %s: %s\n", exception.ErrorCode(),
  167.  
                (const char *)exception.ProcName(),
  168.  
                (const char *)exception.ErrorMessage());
  169.  
      }
  170.  
     
  171.  
      // Tell the main thread to terminate itself.
  172.  
      LockMutex(gStartMutex);
  173.  
      gTerminate = TRUE;
  174.  
      UnlockMutex(gStartMutex);
  175.  
      CFRunLoopStop(CFRunLoopGetMain());
  176.  
      return H_MSG_OK;
  177.  
    }
  178.  
     
  179.  
    static int apple_main(int argc, char *argv[])
  180.  
    {
  181.  
      Herror                error;
  182.  
      CFRunLoopTimerRef     Timer;
  183.  
      CFRunLoopTimerContext TimerContext = { 0, 0, 0, 0, 0 };
  184.  
     
  185.  
      CreateMutex("type","sleep",&gStartMutex);
  186.  
      LockMutex(gStartMutex);
  187.  
     
  188.  
      error = HpThreadHandleAlloc(&gActionThread);
  189.  
      if (H_MSG_OK != error)
  190.  
      {
  191.  
        fprintf(stderr,"HpThreadHandleAlloc failed: %d\n", error);
  192.  
        exit(1);
  193.  
      }
  194.  
     
  195.  
      error = HpThreadCreate(gActionThread,0,apple_action);
  196.  
      if (H_MSG_OK != error)
  197.  
      {
  198.  
        fprintf(stderr,"HpThreadCreate failed: %d\n", error);
  199.  
        exit(1);
  200.  
      }
  201.  
     
  202.  
      Timer = CFRunLoopTimerCreate(kCFAllocatorDefault,
  203.  
                                   CFAbsoluteTimeGetCurrent(),0,0,0,
  204.  
                                   timer_callback,&TimerContext);
  205.  
      if (!Timer)
  206.  
      {
  207.  
        fprintf(stderr,"CFRunLoopTimerCreate failed\n");
  208.  
        exit(1);
  209.  
      }
  210.  
      CFRunLoopAddTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);
  211.  
     
  212.  
      for (;;)
  213.  
      {
  214.  
        HBOOL terminate;
  215.  
     
  216.  
        CFRunLoopRun();
  217.  
     
  218.  
        LockMutex(gStartMutex);
  219.  
        terminate = gTerminate;
  220.  
        UnlockMutex(gStartMutex);
  221.  
     
  222.  
        if (terminate)
  223.  
          break;
  224.  
      }
  225.  
     
  226.  
      CFRunLoopRemoveTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);
  227.  
      CFRelease(Timer);
  228.  
     
  229.  
      error = HpThreadHandleFree(gActionThread);
  230.  
      if (H_MSG_OK != error)
  231.  
      {
  232.  
        fprintf(stderr,"HpThreadHandleFree failed: %d\n", error);
  233.  
        exit(1);
  234.  
      }
  235.  
     
  236.  
      ClearMutex(gStartMutex);
  237.  
      return 0;
  238.  
    }
  239.  
    #endif
  240.  
     
  241.  
    int main(int argc, char *argv[])
  242.  
    {
  243.  
      int ret = 0;
  244.  
     
  245.  
      try
  246.  
      {
  247.  
    #if defined(_WIN32)
  248.  
        SetSystem("use_window_thread", "true");
  249.  
    #elif defined(__linux__) && !defined(__arm__)
  250.  
        XInitThreads();
  251.  
    #endif
  252.  
     
  253.  
        // Default settings used in HDevelop (can be omitted) 
  254.  
        SetSystem("width", 512);
  255.  
        SetSystem("height", 512);
  256.  
     
  257.  
    #ifndef __APPLE__
  258.  
        action();
  259.  
    #else
  260.  
        ret = apple_main(argc,argv);
  261.  
    #endif
  262.  
      }
  263.  
      catch (HException &exception)
  264.  
      {
  265.  
        fprintf(stderr,"  Error #%u in %s: %s\n", exception.ErrorCode(),
  266.  
                (const char *)exception.ProcName(),
  267.  
                (const char *)exception.ErrorMessage());
  268.  
        ret = 1;
  269.  
      }
  270.  
      return ret;
  271.  
    }
  272.  
     
  273.  
    #endif
  274.  
     
  275.  
     
  276.  
    #endif
学新通

其中action和void dev_open_window_fit_image 中的内容是我们想要的

新建一个visualstdio2019控制台工程

将dev_open_window_fit_image函数复制到工程中action()函数中的内容复制到main函数中

3 配置vs2019环境

项目    属性

学新通

 VC 目录

学新通

包含目录增加

C:\Program Files\MVTec\HALCON-17.12-Progress\include

C:\Program Files\MVTec\HALCON-17.12-Progress\include\halconcpp

学新通

 库目录中选择C:\Program Files\MVTec\HALCON-17.12-Progress\lib\x64-win64

学新通

 c/c 常规附加包含目录中添加学新通

$(HALCONROOT)/include

$(HALCONROOT)/include/halconcpp

学新通

 链接器,附加库目录添加C:\Program Files\MVTec\HALCON-17.12-Progress\lib\x64-win64

学新通

 输入附加依赖项  添加  halconcpp.lib

学新通

 学新通

点击应用,环境就配置好了

4运行和解决黑屏的问题

  1.  
    using namespace HalconCpp;
  2.  
     
  3.  
    // Procedure declarations
  4.  
    // Chapter: Develop
  5.  
    // Short Description: Open a new graphics window that preserves the aspect ratio of the given image.
  6.  
    void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
  7.  
    HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle);
  8.  
     
  9.  
    // Procedures
  10.  
    // Chapter: Develop
  11.  
    // Short Description: Open a new graphics window that preserves the aspect ratio of the given image.
  12.  
    void dev_open_window_fit_image (HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
  13.  
    HTuple hv_WidthLimit, HTuple hv_HeightLimit, HTuple *hv_WindowHandle)
  14.  
    {
  15.  
     
  16.  
    // Local iconic variables
  17.  
     
  18.  
    // Local control variables
  19.  
    HTuple hv_MinWidth, hv_MaxWidth, hv_MinHeight;
  20.  
    HTuple hv_MaxHeight, hv_ResizeFactor, hv_ImageWidth, hv_ImageHeight;
  21.  
    HTuple hv_TempWidth, hv_TempHeight, hv_WindowWidth, hv_WindowHeight;
  22.  
     
  23.  
    //This procedure opens a new graphics window and adjusts the size
  24.  
    //such that it fits into the limits specified by WidthLimit
  25.  
    //and HeightLimit, but also maintains the correct image aspect ratio.
  26.  
    //
  27.  
    //If it is impossible to match the minimum and maximum extent requirements
  28.  
    //at the same time (f.e. if the image is very long but narrow),
  29.  
    //the maximum value gets a higher priority,
  30.  
    //
  31.  
    //Parse input tuple WidthLimit
  32.  
    if (0 != (HTuple((hv_WidthLimit.TupleLength())==0).TupleOr(hv_WidthLimit<0)))
  33.  
    {
  34.  
    hv_MinWidth = 500;
  35.  
    hv_MaxWidth = 800;
  36.  
    }
  37.  
    else if (0 != ((hv_WidthLimit.TupleLength())==1))
  38.  
    {
  39.  
    hv_MinWidth = 0;
  40.  
    hv_MaxWidth = hv_WidthLimit;
  41.  
    }
  42.  
    else
  43.  
    {
  44.  
    hv_MinWidth = ((const HTuple&)hv_WidthLimit)[0];
  45.  
    hv_MaxWidth = ((const HTuple&)hv_WidthLimit)[1];
  46.  
    }
  47.  
    //Parse input tuple HeightLimit
  48.  
    if (0 != (HTuple((hv_HeightLimit.TupleLength())==0).TupleOr(hv_HeightLimit<0)))
  49.  
    {
  50.  
    hv_MinHeight = 400;
  51.  
    hv_MaxHeight = 600;
  52.  
    }
  53.  
    else if (0 != ((hv_HeightLimit.TupleLength())==1))
  54.  
    {
  55.  
    hv_MinHeight = 0;
  56.  
    hv_MaxHeight = hv_HeightLimit;
  57.  
    }
  58.  
    else
  59.  
    {
  60.  
    hv_MinHeight = ((const HTuple&)hv_HeightLimit)[0];
  61.  
    hv_MaxHeight = ((const HTuple&)hv_HeightLimit)[1];
  62.  
    }
  63.  
    //
  64.  
    //Test, if window size has to be changed.
  65.  
    hv_ResizeFactor = 1;
  66.  
    GetImageSize(ho_Image, &hv_ImageWidth, &hv_ImageHeight);
  67.  
    //First, expand window to the minimum extents (if necessary).
  68.  
    if (0 != (HTuple(hv_MinWidth>hv_ImageWidth).TupleOr(hv_MinHeight>hv_ImageHeight)))
  69.  
    {
  70.  
    hv_ResizeFactor = (((hv_MinWidth.TupleReal())/hv_ImageWidth).TupleConcat((hv_MinHeight.TupleReal())/hv_ImageHeight)).TupleMax();
  71.  
    }
  72.  
    hv_TempWidth = hv_ImageWidth*hv_ResizeFactor;
  73.  
    hv_TempHeight = hv_ImageHeight*hv_ResizeFactor;
  74.  
    //Then, shrink window to maximum extents (if necessary).
  75.  
    if (0 != (HTuple(hv_MaxWidth<hv_TempWidth).TupleOr(hv_MaxHeight<hv_TempHeight)))
  76.  
    {
  77.  
    hv_ResizeFactor = hv_ResizeFactor*((((hv_MaxWidth.TupleReal())/hv_TempWidth).TupleConcat((hv_MaxHeight.TupleReal())/hv_TempHeight)).TupleMin());
  78.  
    }
  79.  
    hv_WindowWidth = hv_ImageWidth*hv_ResizeFactor;
  80.  
    hv_WindowHeight = hv_ImageHeight*hv_ResizeFactor;
  81.  
    //Resize window
  82.  
    SetWindowAttr("background_color","black");
  83.  
    OpenWindow(hv_Row,hv_Column,hv_WindowWidth,hv_WindowHeight,0,"visible","",&(*hv_WindowHandle));
  84.  
    HDevWindowStack::Push((*hv_WindowHandle));
  85.  
    if (HDevWindowStack::IsOpen())
  86.  
    SetPart(HDevWindowStack::GetActive(),0, 0, hv_ImageHeight-1, hv_ImageWidth-1);
  87.  
    return;
  88.  
    }
  89.  
     
  90.  
    #ifndef NO_EXPORT_MAIN
学新通
  1.  
    int main()
  2.  
    {
  3.  
     
  4.  
    // Local iconic variables
  5.  
    HObject ho_Img5131;
  6.  
     
  7.  
    // Local control variables
  8.  
    HTuple hv_WindowHandle;
  9.  
     
  10.  
    ReadImage(&ho_Img5131, "C:/Users/Administrator/Desktop/DSC_0097.JPG");
  11.  
    dev_open_window_fit_image(ho_Img5131, 0, 0, -1, -1, &hv_WindowHandle);
  12.  
    if (HDevWindowStack::IsOpen())
  13.  
    ClearWindow(HDevWindowStack::GetActive());
  14.  
    if (HDevWindowStack::IsOpen())
  15.  
    DispObj(ho_Img5131, HDevWindowStack::GetActive());
  16.  
    system("pause");
  17.  
     
  18.  
    return 0;
  19.  
    }
学新通

 这时候我们的代码就成功导入vs2019了,运行一下,有一个问题

学新通

 界面是黑屏的,找了好久好久,加了一行代码

 SetSystem("use_window_thread", "true");
  1.  
    int main()
  2.  
    {
  3.  
    SetSystem("use_window_thread", "true");
  4.  
    // Local iconic variables
  5.  
    HObject ho_Img5131;
  6.  
     
  7.  
    // Local control variables
  8.  
    HTuple hv_WindowHandle;
  9.  
     
  10.  
    ReadImage(&ho_Img5131, "C:/Users/Administrator/Desktop/DSC_0097.JPG");
  11.  
    dev_open_window_fit_image(ho_Img5131, 0, 0, -1, -1, &hv_WindowHandle);
  12.  
    if (HDevWindowStack::IsOpen())
  13.  
    ClearWindow(HDevWindowStack::GetActive());
  14.  
    if (HDevWindowStack::IsOpen())
  15.  
    DispObj(ho_Img5131, HDevWindowStack::GetActive());
  16.  
    system("pause");
  17.  
     
  18.  
    return 0;
  19.  
    }
学新通

 最后运行学新通

成功显示图片 


 

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhiahakf
系列文章
更多 icon
同类精品
更多 icon
继续加载