Qt调用Halcon13.0机器视觉算法包
Qt:5.6.1 MSVC2013 32bit
Halcon版本:13.0(注意,这里最好是正版,或者已经破译的版本,否则会引起很多意料之外的问题)
1、在pro文件中添加必要的库和必要的头文件
1 INCLUDEPATH += $$PWD/Halcon/Include 2 INCLUDEPATH += $$PWD/Halcon/Include/halconcpp 3 LIBS += -L$$PWD/lib -lhalconcpp
2、引用头文件
3、做你想要处理的事情
Qt添加Halcon窗体Demo
先看一下效果:
上一篇文章中提供了如何在Qt中加入Halcon这个视觉开发库,直接讲核心:
1、如何嵌入窗体到Qt中:
①、获取Qt对应窗口的ID
②、利用OpenWindow函数把Halcon窗体指定大小和位置添加到指定位置。
OpenWindow原型是这样的:
LIntExport void OpenWindow(const HTuple& Row, const HTuple& Column, const HTuple& Width, const HTuple& Height, const HTuple& FatherWindow, const HTuple& Mode, const HTuple& Machine, HTuple* WindowHandle);
2、QString转HTuple是不能直接转换的
①、先把QString转换成std::string
②、再把std::string转换成CString
③、再把CString转换成HTuple。
3、灰化函数Rgb1ToGray
原型是这样的:
LIntExport void Rgb1ToGray(const HObject& RGBImage, HObject* GrayImage);
代码实现是这样的:
1 HObject ho_Image; 2 HObject ok_Image; 3 HTuple hv_WindowID; 4 long widid = this->winId(); 5 HTuple widid2 = widid; 6 ReadImage(&ho_Image, HTuple(filename.toStdString().c_str())); // 此方法Halcon提供
7 Rgb1ToGray(ho_Image, &ok_Image); // 此方法Halcon提供
8 OpenWindow(9, 337, ui->label_2->width(),ui->label_2->height(),widid2,"","",&hv_WindowID); 9 DispImage(ok_Image,hv_WindowID);
可以看到,先获取窗口的ID,Qt的窗口类型为Wid,其实就是long类型,和Halcon中Hlong很像。
代码示例:
1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include "Halcon/Include/halconcpp/HalconCpp.h"
4 #include <QDebug>
5 #include <QFileDialog>
6 #include <QMessageBox>
7
8 using namespace HalconCpp; 9
10 MainWindow::MainWindow(QWidget *parent) : 11 QMainWindow(parent), 12 ui(new Ui::MainWindow) 13 { 14 ui->setupUi(this); 15
16 QString filename; 17 filename=QFileDialog::getOpenFileName(this, tr("选择图像"), "", tr("Images (*.png *.bmp *.jpg *.tif *.GIF )")); 18 if(filename.isEmpty()) { 19 return; 20 } else { 21 QImage* img=new QImage; 22 if(! ( img->load(filename) ) ) { //加载图像
23 QMessageBox::information(this, tr("打开图像失败"), tr("打开图像失败!")); 24 delete img; 25 return; 26 } 27 ui->label->setPixmap(QPixmap::fromImage(*img)); 28 } 29 ui->label->setScaledContents(true); 30
31
32 HObject ho_Image; 33 HObject ok_Image; 34 HTuple hv_WindowID; 35 long widid = this->winId(); 36 HTuple widid2 = widid; 37 ReadImage(&ho_Image, HTuple(filename.toStdString().c_str())); // 此方法Halcon提供
38 Rgb1ToGray(ho_Image, &ok_Image); // 此方法Halcon提供
39 OpenWindow(ui->label_2->y(), ui->label_2->x(), ui->label_2->width(),ui->label_2->height(),widid2,"","",&hv_WindowID); 40 DispImage(ok_Image,hv_WindowID); 41 } 42
43 MainWindow::~MainWindow() 44 { 45 delete ui; 46 }