Qt+Halcon
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked 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 it uses 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 += \
HalconImageWnd.cpp \
ROI.cpp \
ROIArc.cpp \
ROICircle.cpp \
ROIController.cpp \
ROILine.cpp \
ROIPolygon.cpp \
ROIPolyline.cpp \
ROIRectangle.cpp \
ROIRectangle2.cpp \
main.cpp \
mainwindow.cpp
HEADERS += \
HalconImageWnd.h \
ROI.h \
ROIArc.h \
ROICircle.h \
ROIController.h \
ROILine.h \
ROIPolygon.h \
ROIPolyline.h \
ROIRectangle.h \
ROIRectangle2.h \
mainwindow.h \
stable.h
#调试输出
#DEFINES += QT_MESSAGELOGCONTEXT
DEFINES += QT_MESSAGELOGCONTEXT
CONFIG(release, debug|release){
DEFINES += QT_NO_WARNING_OUTPUT
DEFINES += QT_NO_DEBUG_OUTPUT
}
#指定生成的临时文件放置的目录
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
#指定编译器选项和项目配置
CONFIG += c++11
CONFIG += warn_on #告诉qmake要把编译器设置为输出警告信息的
#CONFIG += warn_off #不要警告输出
CONFIG += precompile_header #可以在项目中使用预编译头文件的支持
#预编译头文件路径
PRECOMPILED_HEADER = $$PWD/stable.h
#disable C4819 warning
win32:QMAKE_CXXFLAGS_WARN_ON += -wd4819
win32:QMAKE_CXXFLAGS += /FS
#QMAKE_CXXFLAGS += /utf-8
#避免VC编译器关于fopen等应使用fopen_s的安全警告
win32:DEFINES += _CRT_SECURE_NO_WARNINGS
#指定生成的应用程序放置的目录
CONFIG(debug, debug|release){
contains(DEFINES, WIN64) {
DESTDIR = ../_debug64
} else {
DESTDIR = ../_debug32
}
} else {
contains(DEFINES, WIN64) {
DESTDIR = ../_release64
} else {
DESTDIR = ../_release32
}
}
macx:CONFIG(debug, debug|release){
DESTDIR = ../bind
} else:macx:CONFIG(release, debug|release){
DESTDIR = ../bin
}
message($$PWD)
message($$DESTDIR)
#Halcon Configuration
isEmpty(MY_HALCON_PATH) {
win32:MY_HALCON_PATH = "D:/My Resources/ImageProcessingLibrary/halcon/" #注意斜杆的方向;引号;
macx:MY_HALCON_PATH = none
message($$MY_HALCON_PATH)
}
#头文件包含路径
win32 {
INCLUDEPATH += $${MY_HALCON_PATH}/include/
INCLUDEPATH += $${MY_HALCON_PATH}/include/halconcpp
DEPENDPATH += $${MY_HALCON_PATH}/include/
}
#库文件包含路径
win32 {
contains(QT_ARCH, i386) {
message("32-bit")
#Windows x86 (32bit) specific build here
MY_HALCON_LIBS_PATH = $${MY_HALCON_PATH}/lib/x86sse2-win32
} else {
message("64-bit")
#Windows x64 (64bit) specific build here
MY_HALCON_LIBS_PATH = $${MY_HALCON_PATH}/lib/x64-win64
}
message($$MY_HALCON_LIBS_PATH)
}
#依赖库的名称
win32:CONFIG(debug, debug|release): {
LIBS += -L$${MY_HALCON_LIBS_PATH} -lhalcon
LIBS += -L$${MY_HALCON_LIBS_PATH} -lhalconcpp
}
else:win32:CONFIG(release, debug|release):{
LIBS += -L$${MY_HALCON_LIBS_PATH} -lhalcon
LIBS += -L$${MY_HALCON_LIBS_PATH} -lhalconcpp
}
else:unix:{
}
# 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/Frameworks
QMAKE_LFLAGS += -F/Library/Frameworks
LIBS += -framework HALCONCpp
}
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
#pro文件的参考文档
#https://doc.qt.io/qt-5/qmake-variable-reference.html
Qt+OpenCV
#-------------------------------------------------
#
# Project created by QtCreator 2019-08-29T10:43:54
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = qt_opencv_demo
TEMPLATE = app
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as 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_op.cpp \
mylabel.cpp
HEADERS += \
mylabel.h \
Widget_op.h \
stable.h
FORMS += Widget_op.ui
#指定生成的临时文件放置的目录
MOC_DIR = temp/moc
RCC_DIR = temp/rcc
UI_DIR = temp/ui
OBJECTS_DIR = temp/obj
#指定编译器选项和项目配置
CONFIG += c++11
CONFIG += warn_on #告诉qmake要把编译器设置为输出警告信息的
#CONFIG += warn_off #不要警告输出
CONFIG += precompile_header #可以在项目中使用预编译头文件的支持
#预编译头文件路径
PRECOMPILED_HEADER = $$PWD/stable.h
#disable C4819 warning
win32:QMAKE_CXXFLAGS_WARN_ON += -wd4819
win32:QMAKE_CXXFLAGS += /FS
#避免VC编译器关于fopen等应使用fopen_s的安全警告
win32:DEFINES += _CRT_SECURE_NO_WARNINGS
#指定生成的应用程序放置的目录
win32:CONFIG(debug, debug|release){
contains(DEFINES, WIN64) {
DESTDIR = ../_debug64
} else {
DESTDIR = ../_debug32
}
} else:win32:CONFIG(release, debug|release){
contains(DEFINES, WIN64) {
DESTDIR = ../_release64
} else {
DESTDIR = ../_release32
}
}
macx:CONFIG(debug, debug|release){
DESTDIR = ../bind
} else:macx:CONFIG(release, debug|release){
DESTDIR = ../bin
}
message($$PWD)
message($$DESTDIR)
#Opencv Configuration
isEmpty(MY_OPENCV_PATH) {
win32:MY_OPENCV_PATH = "D:/My Resources/ImageProcessingLibrary/opencv/" #注意斜杆的方向;引号;
macx:MY_OPENCV_PATH = "/Users/firecat/opencv4.2.0"
message($$MY_OPENCV_PATH)
}
#头文件包含路径
win32 {
INCLUDEPATH += $${MY_OPENCV_PATH}/include/
DEPENDPATH += $${MY_OPENCV_PATH}/include/
}
macx {
INCLUDEPATH += $${MY_OPENCV_PATH}/include/opencv4
DEPENDPATH += $${MY_OPENCV_PATH}/include/opencv4
}
#库文件包含路径
win32 {
contains(QT_ARCH, i386) {
message("32-bit")
#Windows x86 (32bit) specific build here
MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/Win32/vc15/lib
} else {
message("64-bit")
#Windows x64 (64bit) specific build here
MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/x64/vc15/lib
}
message($$MY_OPENCV_LIBS_PATH)
}
macx {
MY_OPENCV_LIBS_PATH = $${MY_OPENCV_PATH}/lib
message($$MY_OPENCV_LIBS_PATH)
}
#依赖库的名称
win32:CONFIG(debug, debug|release): {#动态库
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d420d
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core420d
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d420d
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann420d
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui420d
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs420d
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc420d
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio420d
}
else:win32:CONFIG(release, debug|release):{
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d420
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core420
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d420
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann420
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui420
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs420
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc420
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio420
}
else:macx:{
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_calib3d.4.2.0
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_core.4.2.0
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_features2d.4.2.0
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_flann.4.2.0
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_highgui.4.2.0
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgcodecs.4.2.0
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_imgproc.4.2.0
LIBS += -L$${MY_OPENCV_LIBS_PATH} -lopencv_videoio.4.2.0
}
#pro文件的参考文档
#https://doc.qt.io/qt-5/qmake-variable-reference.html
预编译头文件stable.h
//#ifndef STABLE_H
//#define STABLE_H
// Add C includes here
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <math.h>
#if defined __cplusplus
// Add C++ includes here
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
// Qt includes
#include <QApplication>
#include <QDateTime>
#include <QDebug>
#include <QFileDialog>
#include <QGraphicsEllipseItem>
#include <QGraphicsRectItem>
#include <QGraphicsScene>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QImage>
#include <QLabel>
#include <QList>
#include <QMainWindow>
#include <QMenu>
#include <QMessageBox>
#include <QMouseEvent>
#include <QPainter>
#include <QPixmap>
#include <QPointF>
#include <QPushButton>
#include <QScopedPointer>
#include <QSettings>
#include <QSlider>
#include <QStandardItem>
#include <QStandardItemModel>
#include <QStyledItemDelegate>
#include <QTextCodec>
#include <QThread>
#include <QUndoGroup>
#include <QUndoStack>
#include <QVBoxLayout>
#include <QVector>
#include <QWheelEvent>
#include <QWidget>
#include <qdebug.h>
#include <qfiledialog.h>
#include <qmath.h>
//如果可以的话,请尽量不要#include <QtCore>和#include <QtGui>,因为这两个头文件涵盖了Qt所有的类,处理他们需要的时间相当长。
//#include <QtCore> //请谨慎包含
//#include <QtGui> //请谨慎包含
#include <opencv2/opencv.hpp>
//using namespace cv;
#ifndef max
#define max(x, y) ((x) < (y) ? (y) : (x))
#define min(x, y) ((x) < (y) ? (x) : (y))
#endif
//解决UTF-8编码中文乱码的问题
#ifdef _MSC_VER
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#pragma warning(disable : 4819)
#endif // _MSC_VER >= 1600
#endif // _MSC_VER
//扩展qDebug以文件行列记录信息
#define QLOG_DEBUG(msg) qDebug() << QString("[%1][%2][%3][%4]%5") \
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz")) \
.arg(QString("%1:%2:%3").arg(__FILE__).arg(__LINE__).arg(__FUNCTION__)) \
.arg(QThread::currentThread()->objectName()) \
.arg("DEBUG") \
.arg(msg);
#define LWLGD //LWLG::日志开关,注释关闭打印日志
#ifdef LWLGD
#define lwlgout qDebug() << "[lwlg]File" << __FILE__ << ",Line:" << __LINE__ << ">>"
#else
#define lwlgout
#endif
#endif //__cplusplus
//#endif // STABLE_H
建议不要在项目中使用using namespace cv;否则会报错:error C2872: “ACCESS_MASK”: 不明确的符号
因为winnt.h里边定义了cv,它的cv空间里有一个符号叫ACCESS_MASK;
opencv函数里边也定义了命名空间cv,它的cv空间里也有一个符号叫ACCESS_MASK。
那么我的项目如果同时包含这两个头文件,计算机是否知道我要调用哪个ACCESS_MASK吗?
main.cpp
#include "mainwindow.h"
#include <QApplication>
#include <QString>
#include <QTextCodec>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
#if (QT_VERSION <= QT_VERSION_CHECK(5, 0, 0))
#if _MSC_VER
QTextCodec *codec = QTextCodec::codecForName("GBK");
#else
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
#endif
QTextCodec::setCodecForLocale(codec);
QTextCodec::setCodecForCStrings(codec);
QTextCodec::setCodecForTr(codec);
#else
QTextCodec *codec = QTextCodec::codecForName("UTF-8");
QTextCodec::setCodecForLocale(codec);
#endif
MainWindow w;
w.show();
return a.exec();
}