1.编写halcon程序
read_image (Image, 'E:/Images/lenna.bmp')//读取图片
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)//创建一个窗口
dev_display (Image)
2.将halcon导出成c++代码,下面的代码是导出后有用的代码段
// Main procedure
void action()
{
// Local iconic variables
HObject ho_Image;
// Local control variables
HTuple hv_Width, hv_Height, hv_WindowHandle;
ReadImage(&ho_Image, "E:/Images/lenna.bmp");
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetWindowAttr("background_color","black");
OpenWindow(0,0,hv_Width,hv_Height,0,"visible","",&hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
if (HDevWindowStack::IsOpen())
DispObj(ho_Image, HDevWindowStack::GetActive());
}
3.创建一个Qt项目并配置好halcon(查找Qt如何halcon)
在wideget.h中声明变量
#ifndef WIDGET_H
#define WIDGET_H
#include <QWidget>
#include <QLabel>
#include <QPushButton>
#include "halconcpp\HalconCpp.h"
using namespace HalconCpp;
#pragma execution_character_set("utf-8") //支持中文//有问题这段使用中文,不用理会
class Widget : public QWidget
{
Q_OBJECT
public:
Widget(QWidget *parent = nullptr);
~Widget();
private:
QLabel *label;
HObject ho_Image;
HTuple hv_Width, hv_Height;
HTuple hv_WindowHandle;
};
#endif // WIDGET_H
修改main.cpp代码,也就是改了下窗口哦标题和大小
#include "widget.h"
#include <QApplication>
#include <QPalette>
#include <QWidget>
#include <QMessageBox>
# include <stdio.h>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Widget w;
w.setFixedSize(1000,600);//设置窗口的大小
w.setWindowTitle("halcon_img");//设置窗口标题
w.show();
return a.exec();
}
修改widget.cpp代码为
#include "widget.h"
Widget::Widget(QWidget *parent)
: QWidget(parent)
{
label = new QLabel(this);
label->setGeometry(400,10,500,500);
//label->setText("Hello World");
//QPixmap pixmap("F:/YiBuSheQu/learnOpenCV/learnOpenCV/lena.png");
//label->setPixmap(pixmap);
Hlong MainWndID = (Hlong)this->label->winId();
OpenWindow(0, 0, label->width(), label->height(), MainWndID, "visible", "", &hv_WindowHandle);
ReadImage(&ho_Image, "F:/YiBuSheQu/learnOpenCV/learnOpenCV/noobcv.jpg");
GetImageSize(ho_Image, &hv_Width, &hv_Height);
SetPart(hv_WindowHandle, 0, 0, hv_Height, hv_Width);
DispObj(ho_Image, hv_WindowHandle);
}
Widget::~Widget()
{
}
重新构建项目运行