找了好多资料,网上关于QT与halcon和OpenCV配置资料和显示图片很复杂,我给出我的方式,亲测有效,在此分享出来,供大家参考。
一、配置.pro文件,也就是VS中包含文件和库目录
我直接贴出我的.pro文件,更改之后记得qmake一下(构建->qmake)
#-------------------------------------------------
#
# Project created by QtCreator 2019-07-16T10:19:59
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = test
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
widget.cpp
HEADERS += \
widget.h
FORMS += \
widget.ui
# 以下为主要部分
INCLUDEPATH += $$(HALCONROOT)/include/ \
$$(HALCONROOT)/include/halconcpp \
C:/opencv/build/include/ \
C:/opencv/build/include/opencv \
C:/opencv/build/include/opencv2
win32: LIBS += -L$$(HALCONROOT)/lib/$$(HALCONARCH)/ \
-lhalconcpp \
-lhalcon \
-LC:/opencv/build/x64/vc14/lib/ \
-lopencv_world401d
二、widget.cpp代码
#include "widget.h"
#include "ui_widget.h"
#include "HalconCpp.h"
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
#include <opencv2/opencv.hpp>
using namespace HalconCpp;
Widget::Widget(QWidget *parent) :
QWidget(parent),
ui(new Ui::Widget)
{
ui->setupUi(this);
}
Widget::~Widget()
{
delete ui;
}
void Widget::on_pushButton_clicked()
{
// 使用halcon显示图片
HObject himage1;
QString str = "C:/Users/lenovo/Pictures/Saved Pictures/Lena.png";
//读取图片
HalconCpp::ReadImage(&himage1,HTuple(str.toStdString().c_str()));
HImage Image(HTuple(str.toStdString().c_str()));
Hlong width, height;
Image.GetImageSize(&width, &height);
HWindow w(0, 0, width, height);
Image.DispImage(w);
w.Click();
w.ClearWindow();
}
void Widget::on_pB_dispImage_OpenCV_clicked()
{
// 使用OpenCV显示图片
cv::Mat img = cv::imread("C:/Users/lenovo/Pictures/Saved Pictures/Lena.png");
cv::namedWindow("Lena");
cv::imshow("Lena", img);
}
void Widget::on_pB_dispImage_clicked()
{
// 显示图片(label)
cv::Mat srcImage, gray_image, srcImage1;
QString Fileadd = QFileDialog::getOpenFileName(this, "get file");
if(Fileadd.isEmpty())
{
QMessageBox::information(this, "警告","没有选择文件");
return;
}
srcImage = cv::imread(Fileadd.toLatin1().data()); //读取图片
cv::cvtColor(srcImage, srcImage1, cv::COLOR_BGR2RGB); //图像转换格式
QImage distImage = QImage((const unsigned char*)(srcImage1.data), srcImage1.cols, srcImage1.rows, QImage::Format_RGB888);
ui->label->setPixmap(QPixmap::fromImage(distImage).scaled(ui->label->size(), Qt::KeepAspectRatio));
}
void Widget::on_pB_dispImage_2_clicked()
{
// 显示图片(graphicsView)
QString fileName = QFileDialog::getOpenFileName(this, tr("Open Image"), ".", tr("Image File(*.png *.jpg *.jpeg *.bmp)"));
if(fileName.isEmpty())
{
return;
}
cv::Mat srcImage = cv::imread(fileName.toLatin1().data()); // 读取图片数据
cv::cvtColor(srcImage, srcImage, cv::COLOR_BGR2RGB); // 图像格式转换
QImage disImage = QImage((const unsigned char*)(srcImage.data), srcImage.cols, srcImage.rows, QImage::Format_RGB888);
QGraphicsScene *scene = new QGraphicsScene; // 图像显示
scene->addPixmap(QPixmap::fromImage(disImage));
ui->graphicsView->setScene(scene);
ui->graphicsView->show();
}