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 }