00. 目录

文章目录

  • 00. 目录
  • 01. 概述
  • 02. 编写Halcon程序
  • 03. Halcon程序导出C++文件
  • 04. 创建Qt图形界面项目
  • 05. Qt集成Halcon程序
  • 06. 附录

01. 概述

QT与Halcon联合编程。将Halcon中代码集成到Qt程序中。

开发环境

Qt:Qt5.15.2

Halcon: Halcon 19.11

02. 编写Halcon程序

Halcon程序示例

* 从本地磁盘读取一张图片
read_image (Carb, 'E:/CarB.jpg')* 获取图片大小
get_image_size (Carb, Width, Height)* 关闭当前窗口
dev_close_window ()* 打开新窗口
dev_open_window (0, 0, Width / 2, Height / 2, 'black', WindowHandle)* 显示图片
dev_display (Carb)

Halcon中执行结果

03. Halcon程序导出C++文件

将上述Halcon程序导出为C++源程序

导出内容如下:

///
// File generated by HDevelop for HALCON/C++ Version 19.11.0.0
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
// Ensure that the interface encoding is set to locale encoding by calling
// SetHcppInterfaceStringEncodingIsUtf8(false) at the beginning of the program.
//
// 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.
///#ifndef __APPLE__
#  include "HalconCpp.h"
#  include "HDevThread.h"
#else
#  ifndef HC_LARGE_IMAGES
#    include <HALCONCpp/HalconCpp.h>
#    include <HALCONCpp/HDevThread.h>
#    include <HALCON/HpThread.h>
#  else
#    include <HALCONCppxl/HalconCpp.h>
#    include <HALCONCppxl/HDevThread.h>
#    include <HALCONxl/HpThread.h>
#  endif
#  include <stdio.h>
#  include <CoreFoundation/CFRunLoop.h>
#endifusing namespace HalconCpp;// Procedure declarations
// Local procedures
void blobImage (HObject *ho_Image, HObject *ho_Region);// Procedures
// Local procedures
void blobImage (HObject *ho_Image, HObject *ho_Region)
{ReadImage(&(*ho_Image), "E:/test.jpg.tif");Threshold((*ho_Image), &(*ho_Region), 128, 255);if (HDevWindowStack::IsOpen())DispObj((*ho_Image), HDevWindowStack::GetActive());if (HDevWindowStack::IsOpen())DispObj((*ho_Region), HDevWindowStack::GetActive());return;
}#ifndef NO_EXPORT_MAIN
// Main procedure
void action()
{// Local iconic variablesHObject  ho_Carb;// Local control variablesHTuple  hv_Width, hv_Height, hv_WindowHandle;//从本地磁盘读取一张图片ReadImage(&ho_Carb, "E:/CarB.jpg");//获取图片大小GetImageSize(ho_Carb, &hv_Width, &hv_Height);//关闭当前窗口if (HDevWindowStack::IsOpen())CloseWindow(HDevWindowStack::Pop());//打开新窗口SetWindowAttr("background_color","black");OpenWindow(0,0,hv_Width/2,hv_Height/2,0,"visible","",&hv_WindowHandle);HDevWindowStack::Push(hv_WindowHandle);//显示图片if (HDevWindowStack::IsOpen())DispObj(ho_Carb, HDevWindowStack::GetActive());}#ifndef NO_EXPORT_APP_MAIN#ifdef __APPLE__
// On OS X systems, we must have a CFRunLoop running on the main thread in
// order for the HALCON graphics operators to work correctly, and run the
// action function in a separate thread. A CFRunLoopTimer is used to make sure
// the action function is not called before the CFRunLoop is running.
// Note that starting with macOS 10.12, the run loop may be stopped when a
// window is closed, so we need to put the call to CFRunLoopRun() into a loop
// of its own.
HTuple      gStartMutex;
H_pthread_t gActionThread;
HBOOL       gTerminate = FALSE;static void timer_callback(CFRunLoopTimerRef timer, void *info)
{UnlockMutex(gStartMutex);
}static Herror apple_action(void **parameters)
{// Wait until the timer has fired to start processing.LockMutex(gStartMutex);UnlockMutex(gStartMutex);try{action();}catch (HException &exception){fprintf(stderr,"  Error #%u in %s: %s\n", exception.ErrorCode(),(const char *)exception.ProcName(),(const char *)exception.ErrorMessage());}// Tell the main thread to terminate itself.LockMutex(gStartMutex);gTerminate = TRUE;UnlockMutex(gStartMutex);CFRunLoopStop(CFRunLoopGetMain());return H_MSG_OK;
}static int apple_main(int argc, char *argv[])
{Herror                error;CFRunLoopTimerRef     Timer;CFRunLoopTimerContext TimerContext = { 0, 0, 0, 0, 0 };CreateMutex("type","sleep",&gStartMutex);LockMutex(gStartMutex);error = HpThreadHandleAlloc(&gActionThread);if (H_MSG_OK != error){fprintf(stderr,"HpThreadHandleAlloc failed: %d\n", error);exit(1);}error = HpThreadCreate(gActionThread,0,apple_action);if (H_MSG_OK != error){fprintf(stderr,"HpThreadCreate failed: %d\n", error);exit(1);}Timer = CFRunLoopTimerCreate(kCFAllocatorDefault,CFAbsoluteTimeGetCurrent(),0,0,0,timer_callback,&TimerContext);if (!Timer){fprintf(stderr,"CFRunLoopTimerCreate failed\n");exit(1);}CFRunLoopAddTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);for (;;){HBOOL terminate;CFRunLoopRun();LockMutex(gStartMutex);terminate = gTerminate;UnlockMutex(gStartMutex);if (terminate)break;}CFRunLoopRemoveTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);CFRelease(Timer);error = HpThreadHandleFree(gActionThread);if (H_MSG_OK != error){fprintf(stderr,"HpThreadHandleFree failed: %d\n", error);exit(1);}ClearMutex(gStartMutex);return 0;
}
#endifint main(int argc, char *argv[])
{int ret = 0;try{#if defined(_WIN32)SetSystem("use_window_thread", "true");
#endif// file was stored with local-8-bit encoding//   -> set the interface encoding accordinglySetHcppInterfaceStringEncodingIsUtf8(false);// Default settings used in HDevelop (can be omitted)SetSystem("width", 512);SetSystem("height", 512);#ifndef __APPLE__action();
#elseret = apple_main(argc,argv);
#endif}catch (HException &exception){fprintf(stderr,"  Error #%u in %s: %s\n", exception.ErrorCode(),(const char *)exception.ProcName(),(const char *)exception.ErrorMessage());ret = 1;}return ret;
}#endif#endif

04. 创建Qt图形界面项目

4.1 简单的GUI设计如下

集成halcon编译需要用msvc,如果用minGW,会出错,等有时间在去研究minGW编译情形。

05. Qt集成Halcon程序

5.1 在pro项目配置文件中添加一下Halcon相关配置

# MacOS specific settings. Note that while dyld will search under
# /Library/Frameworks by default, the preprocessor/compiler/linker will not
# and need to be told explicitly.
macx {QMAKE_CXXFLAGS += -F/Library/FrameworksQMAKE_LFLAGS   += -F/Library/FrameworksLIBS           += -framework HALCONCpp
}
else {#defineswin32:DEFINES += WIN32#includesINCLUDEPATH   += "$$(HALCONROOT)/include"INCLUDEPATH   += "$$(HALCONROOT)/include/halconcpp"#libsQMAKE_LIBDIR  += "$$(HALCONROOT)/lib/$$(HALCONARCH)"unix:LIBS     += -lhalconcpp -lhalcon -lXext -lX11 -ldl -lpthreadwin32:LIBS    += "$$(HALCONROOT)/lib/$$(HALCONARCH)/halconcpp.lib" \"$$(HALCONROOT)/lib/$$(HALCONARCH)/halcon.lib"
}

5.2 在mainwindow.h文件中添加Halcon相关头文件

#ifndef __APPLE__
#  include "HalconCpp.h"
#  include "HDevThread.h"
#else
#  ifndef HC_LARGE_IMAGES
#    include <HALCONCpp/HalconCpp.h>
#    include <HALCONCpp/HDevThread.h>
#    include <HALCON/HpThread.h>
#  else
#    include <HALCONCppxl/HalconCpp.h>
#    include <HALCONCppxl/HDevThread.h>
#    include <HALCONxl/HpThread.h>
#  endif
#  include <stdio.h>
#  include <CoreFoundation/CFRunLoop.h>
#endifusing namespace HalconCpp;

5.3 在MainWindow添加halcon相关的变量

源程序如下:

#ifndef MAINWINDOW_H
#define MAINWINDOW_H#include <QMainWindow>#ifndef __APPLE__
#  include "HalconCpp.h"
#  include "HDevThread.h"
#else
#  ifndef HC_LARGE_IMAGES
#    include <HALCONCpp/HalconCpp.h>
#    include <HALCONCpp/HDevThread.h>
#    include <HALCON/HpThread.h>
#  else
#    include <HALCONCppxl/HalconCpp.h>
#    include <HALCONCppxl/HDevThread.h>
#    include <HALCONxl/HpThread.h>
#  endif
#  include <stdio.h>
#  include <CoreFoundation/CFRunLoop.h>
#endifusing namespace HalconCpp;QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACEclass MainWindow : public QMainWindow
{Q_OBJECTpublic:MainWindow(QWidget *parent = nullptr);~MainWindow();private slots:void on_pushButton_clicked();void on_pushButton_3_clicked();private:Ui::MainWindow *ui;// Local iconic variablesHObject  ho_Carb;// Local control variablesHTuple  hv_Width, hv_Height, hv_WindowHandle;};
#endif // MAINWINDOW_H

5.4 实现采集图像槽函数

//采集图像
void MainWindow::on_pushButton_clicked()
{QString fileName = QFileDialog::getOpenFileName(this,tr("打开图片"), "E://", tr("Image Files (*.png *.jpg *.bmp)"));qDebug() << fileName;//从本地磁盘读取一张图片//QString转HTuple要通过StringReadImage(&ho_Carb, fileName.toStdString().c_str());//获取图片大小GetImageSize(ho_Carb, &hv_Width, &hv_Height);}

友情提示

//QString转HTuple要通过String
ReadImage(&ho_Carb, fileName.toStdString().c_str());

5.5 实现显示图像槽函数

//显示图像
void MainWindow::on_pushButton_3_clicked()
{//关闭当前窗口if (HDevWindowStack::IsOpen())CloseWindow(HDevWindowStack::Pop());//打开新窗口SetWindowAttr("background_color","black");OpenWindow(0,0,hv_Width/2,hv_Height/2, (Hlong)ui->label->winId(),"visible","",&hv_WindowHandle);HDevWindowStack::Push(hv_WindowHandle);//显示图片if (HDevWindowStack::IsOpen())DispObj(ho_Carb, HDevWindowStack::GetActive());
}

友情提示

Qt中winId()和Halcon中窗口句柄转换

OpenWindow(0,0,hv_Width/2,hv_Height/2, (Hlong)ui->label->winId(),“visible”,"",&hv_WindowHandle);

5.6 编译程序

运行结果如下:

06. 附录

6.1 测试程序下载

下载:【机器视觉】Qt联合Halcon编程之显示多图片.rar

【机器视觉】Qt联合Halcon编程之显示多图片相关推荐

  1. 【机器视觉】Qt联合Halcon编程之显示图片

    00. 目录 文章目录 00. 目录 01. 概述 02. 编写Halcon程序 03. Halcon程序导出C++文件 04. 创建Qt图形界面项目 05. Qt集成Halcon程序 06. 附录 ...

  2. 【机器视觉C#联合Halcon】

    前言:本人小白一个,自学初入C#(可能连初入也不算,哈哈哈),目前从事机器视觉方面的工作,写这篇文章主要是记录自己平时遇到的一些问题,不懂的地方大家多多支持啊!------------持续更新中. 文 ...

  3. C#联合halcon编程(一):打开\保存图像

    C#和halcon的环境怎么配置我就不说了,网上很多文章都有详细的讲解,今天先说一下怎么实现最简单的打开\保存图像功能模块.成品打包地址我放在最后了 新建一个winform窗体,添加三个控件,一个hW ...

  4. 使用 Qt 获取 UDP 数据并显示成图片

    一个项目,要接收 UDP 数据包,解析并获取其中的数据,主要根据解析出来的行号和序号将数据拼接起来,然后将拼接起来的数据(最重要的数据是 R.G.B 三个通道的像素值)显示在窗口中.考虑到每秒钟要接收 ...

  5. linux qt显示gif图片,QT显示GIF图片

    在QT中要显示GIF图片,不能通过单单的添加部件来完成. 还需要手动的编写程序. 工具:QT Creator 新建一个工程,我们先在designer中,添加一个QLabel部件. 如下图: 将QLab ...

  6. (机器视觉)Halcon下颜色识别与联合C#编程

    一.简介 在上一篇文章中写到了关于用Halcon做颜色识别的一个实验项目,在上一篇中是在Halcon环境中进行开发的,而在实际运用中,现场是不在Halcon中进行运行程序的,而且一些逻辑在Halcon ...

  7. halcon窗体的移动和缩放_halcon与C#联合编程之鼠标控制图片缩放,拖动,roi

    [实例简介]本实例展示了halcon与C#联合编程之鼠标控制图片缩放,拖动,roi [实例截图] [核心代码]using ChoiceTech.Halcon.Control; using Halcon ...

  8. Halcon联合C#编程

    Halcon联合C#编程 Halcon联合C#编程目前我知晓的有三种方式:1.将Halcon代码导成C#代码然后复制到C#程序中:2.直接采用C#中的Halcon库语句进行程序编写(难度有点大,其实就 ...

  9. C#联合halcon框架实现插件编程 运动控制卡 多工具 测量 九点标定全新

    C#联合halcon框架实现插件编程 运动控制卡 多工具 测量 九点标定全新 ,用于学习很nice,适合新手 编号:6937662632121279小鲤鱼历险记

最新文章

  1. Blender左轮手枪制作教程
  2. [转]:xmake插件开发之色彩高亮显示
  3. python3 str bytes 字符串 字节 互相转换
  4. 110. Balanced Binary Tree 平衡二叉树
  5. 前端工程师和设计师必读文章推荐【系列三十六】
  6. 前端学习(3163):react-hello-react之组件化编码
  7. Unity新项目如何快速理清顶层代码结构
  8. Android开发笔记(二十)顶部导航栏ActionBar
  9. 自动启动和关闭Oracle 脚本
  10. 如何优雅的在Vue Project中使用vue-apollo
  11. 我爱Markdown (2)
  12. XVIII Open Cup named after E.V. Pankratiev. Grand Prix of Saratov
  13. signature=9293cc4bd6f47e4f2a5f299011c6e89e,02-本地证书配置指导
  14. Python+selenium批量把网页文档保存为PDF
  15. 坚果手机 误进FastBoot Mode解决方案
  16. android水波纹教程,Android实现简单水波纹效果
  17. markman psd
  18. OpenCV中HSV与PS中HSB对应关系
  19. 动手实践看懂深度学习的DP和DDP
  20. (无美国卡)如何进行美国号码是否可正常接收短信的验证,解决方案详见内文...

热门文章

  1. java socket 通讯
  2. 在路上(on the road)
  3. Enterprise Library 4.0
  4. Java黑皮书课后题第8章:**8.14(探讨矩阵)编写程序,提示用户输入一个方阵的长度,随机地在矩阵中填入0和1,打印这个矩阵,然后找出整行、整列或者对角线都是1或0的行、列和对角线
  5. C语言学习之用指针变量,将数组a中n个整数按相反顺序存放
  6. 2010年浙江大学计算机及软件工程研究生机试真题
  7. 深入学习c++--智能指针(三) unique_ptr
  8. C:简单的学生信息处理程序实现
  9. 蓝书3.7 欧拉回路
  10. [BZOJ1833][ZJOI2010]Count数字计数(DP)