我电脑的基本讯息
Halcon安装路径:17版默认安装
C:\Program Files\MVTec\HALCON-17.12-Progress
QT安装路径:5.13.0安装msvc默认路径
C:\Qt\Qt5.13.0\5.13.0\msvc2017_64
步骤:
1. 新建一个QT项目, 项目按右键=》添加库=》外部库,然后添加2个
1)C:\Program Files\MVTec\HALCON-17.12-Progress\lib\x64-win64\halcon.lib
1)C:\Program Files\MVTec\HALCON-17.12-Progress\lib\x64-win64\halconcpp.lib
只选Windows,不要勾选“为debug版本添加‘d’做为后缀
或直接在pro文件里加入
win32: LIBS += -L'C:/Program Files/MVTec/HALCON-17.12-Progress/lib/x64-win64/' -lhalcon -lhalconcpp
INCLUDEPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/lib/x64-win64'
DEPENDPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/lib/x64-win64'
2. pro文件里添加
INCLUDEPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/include'
INCLUDEPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/include/hlib'
INCLUDEPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/include/halconcpp'
3. 将 C:\Program Files\MVTec\HALCON-17.12-Progress\include(不包含子目录)
下面的h文件(包括Halcon.h, HBase.h等17个)拷贝到项目里(可以集中放在一个文件夹方便管理,例如我放在halconInclude),然后添加现有项.
第3这个是重点,不知道为什么我的qt一直读不到这include目录下的这17个文件,也许哪里没设置,如果这17个文件加入项目中,他们引用的路径读不到,可以直接改为绝对路径。
4. 这样就完成了,导出一份halcon的案例到main.cpp运行看看
和halcon里的结果相同
pro文件
#-------------------------------------------------
#
# Project created by QtCreator 2019-09-16T04:57:27
#
#-------------------------------------------------
QT += core gui
greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
TARGET = HalconTanHei
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
CONFIG += c++11
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
halconInclude/HBase.h \
halconInclude/HConst.h \
halconInclude/HCtype.h \
halconInclude/HDeclSpec.h \
halconInclude/HErrorDef.h \
halconInclude/HExtern.h \
halconInclude/HIntDef.h \
halconInclude/HInterfaceC.h \
halconInclude/HMacro.h \
halconInclude/HParallel.h \
halconInclude/HProto.h \
halconInclude/HSync.h \
halconInclude/HVersNum.h \
halconInclude/Halcon.h \
halconInclude/HalconC.h \
halconInclude/HalconCDefs.h \
halconInclude/IPType.h \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
win32: LIBS += -L'C:/Program Files/MVTec/HALCON-17.12-Progress/lib/x64-win64/' -lhalcon -lhalconcpp
INCLUDEPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/lib/x64-win64'
DEPENDPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/lib/x64-win64'
INCLUDEPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/include'
INCLUDEPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/include/hlib'
INCLUDEPATH += 'C:/Program Files/MVTec/HALCON-17.12-Progress/include/halconcpp'
halcon
Width:=0.18 //单位
NumSquares:=17 //
create_rectification_grid (Width,NumSquares, 'rectification_grid.ps')
*先读取有 map的图片,生成map后,再读取原图,再映射
read_image (Image, 'can_with_grid')
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
MinCOntrast:=25
Radius:=10
find_rectification_grid (Image, GridRegion, MinCOntrast, Radius)
reduce_domain (Image, GridRegion, ImageReduced)
SigmaSaddlePoints:=1.5
Threshold:=5
saddle_points_sub_pix (ImageReduced, 'facet', SigmaSaddlePoints, Threshold, Row, Column)
gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)
SigmaConnectGridPoints := 0.9
MaxDist := 5.0
connect_grid_points (ImageReduced, ConnectingLines1, Row, Column, SigmaConnectGridPoints, MaxDist)
GridSpacing := 20
roateAngle:='auto'
gen_grid_rectification_map (ImageReduced, ConnectingLines1, Map, Meshes, GridSpacing,roateAngle, Row, Column, 'bilinear')
map_image (Image, Map, ImageMapped)
read_image (Image1, 'can')
map_image (Image1, Map, ImageMapped1)
* dev_display (Image)
* dev_display (ImageMapped1)
* disp_continue_message (WindowHandle, 'black', 'true')
* stop ()
create_bar_code_model ([], [], BarCodeHandle)
set_bar_code_param (BarCodeHandle, 'element_size_min', 1)
set_bar_code_param (BarCodeHandle, 'barcode_width_min', 50)
find_bar_code (ImageMapped1, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
dev_clear_window ()
dev_set_draw ('margin')
dev_display (ImageMapped1)
dev_display (SymbolRegions)
disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'red', 'true')
* connect_grid_points (ImageReduced, ConnectingLines, Width, Width, 0.9, 5.5)
* dev_display (ConnectingLines)
main.cpp
#include "mainwindow.h"
#include <QApplication>
# include "HalconCpp.h"
# include "HDevThread.h"
using namespace HalconCpp;
// Procedure declarations
// External procedures
// Chapter: Graphics / Text
// Short Description: This procedure displays 'Click 'Run' to continue' in the lower right corner of the screen.
void disp_continue_message (HTuple hv_WindowHandle, HTuple hv_Color, HTuple hv_Box);
// Chapter: Graphics / Text
// Short Description: This procedure writes a text message.
void disp_message (HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box);
// Procedures
// External procedures
// Chapter: Graphics / Text
// Short Description: This procedure displays 'Click 'Run' to continue' in the lower right corner of the screen.
void disp_continue_message (HTuple hv_WindowHandle, HTuple hv_Color, HTuple hv_Box)
{
// Local iconic variables
// Local control variables
HTuple hv_GenParamName, hv_GenParamValue, hv_ContinueMessage;
//This procedure displays 'Press Run (F5) to continue' in the
//lower right corner of the screen.
//It uses the procedure disp_message.
//
//Input parameters:
//WindowHandle: The window, where the text shall be displayed
//Color: defines the text color.
// If set to '' or 'auto', the currently set color is used.
//Box: If set to 'true', the text is displayed in a box.
//
//Convert the parameter Box to generic parameters.
hv_GenParamName = HTuple();
hv_GenParamValue = HTuple();
if (0 != ((hv_Box.TupleLength())>0))
{
if (0 != (HTuple(hv_Box[0])==HTuple("false")))
{
//Display no box
hv_GenParamName = hv_GenParamName.TupleConcat("box");
hv_GenParamValue = hv_GenParamValue.TupleConcat("false");
}
else if (0 != (HTuple(hv_Box[0])!=HTuple("true")))
{
//Set a color other than the default.
hv_GenParamName = hv_GenParamName.TupleConcat("box_color");
hv_GenParamValue = hv_GenParamValue.TupleConcat(HTuple(hv_Box[0]));
}
}
if (0 != ((hv_Box.TupleLength())>1))
{
if (0 != (HTuple(hv_Box[1])==HTuple("false")))
{
//Display no shadow.
hv_GenParamName = hv_GenParamName.TupleConcat("shadow");
hv_GenParamValue = hv_GenParamValue.TupleConcat("false");
}
else if (0 != (HTuple(hv_Box[1])!=HTuple("true")))
{
//Set a shadow color other than the default.
hv_GenParamName = hv_GenParamName.TupleConcat("shadow_color");
hv_GenParamValue = hv_GenParamValue.TupleConcat(HTuple(hv_Box[1]));
}
}
//
if (0 != (hv_Color==HTuple("")))
{
//disp_text does not accept an empty string for Color.
hv_Color = HTuple();
}
//
//Display the message.
hv_ContinueMessage = "Press Run (F5) to continue";
DispText(hv_WindowHandle, hv_ContinueMessage, "window", "bottom", "right", hv_Color,
hv_GenParamName, hv_GenParamValue);
return;
}
// Chapter: Graphics / Text
// Short Description: This procedure writes a text message.
void disp_message (HTuple hv_WindowHandle, HTuple hv_String, HTuple hv_CoordSystem,
HTuple hv_Row, HTuple hv_Column, HTuple hv_Color, HTuple hv_Box)
{
// Local iconic variables
// Local control variables
HTuple hv_GenParamName, hv_GenParamValue;
//This procedure displays text in a graphics window.
//
//Input parameters:
//WindowHandle: The WindowHandle of the graphics window, where
// the message should be displayed
//String: A tuple of strings containing the text message to be displayed
//CoordSystem: If set to 'window', the text position is given
// with respect to the window coordinate system.
// If set to 'image', image coordinates are used.
// (This may be useful in zoomed images.)
//Row: The row coordinate of the desired text position
// A tuple of values is allowed to display text at different
// positions.
//Column: The column coordinate of the desired text position
// A tuple of values is allowed to display text at different
// positions.
//Color: defines the color of the text as string.
// If set to [], '' or 'auto' the currently set color is used.
// If a tuple of strings is passed, the colors are used cyclically...
// - if |Row| == |Column| == 1: for each new textline
// = else for each text position.
//Box: If Box[0] is set to 'true', the text is written within an orange box.
// If set to' false', no box is displayed.
// If set to a color string (e.g. 'white', '#FF00CC', etc.),
// the text is written in a box of that color.
// An optional second value for Box (Box[1]) controls if a shadow is displayed:
// 'true' -> display a shadow in a default color
// 'false' -> display no shadow
// otherwise -> use given string as color string for the shadow color
//
//It is possible to display multiple text strings in a single call.
//In this case, some restrictions apply:
//- Multiple text positions can be defined by specifying a tuple
// with multiple Row and/or Column coordinates, i.e.:
// - |Row| == n, |Column| == n
// - |Row| == n, |Column| == 1
// - |Row| == 1, |Column| == n
//- If |Row| == |Column| == 1,
// each element of String is display in a new textline.
//- If multiple positions or specified, the number of Strings
// must match the number of positions, i.e.:
// - Either |String| == n (each string is displayed at the
// corresponding position),
// - or |String| == 1 (The string is displayed n times).
//
//
//Convert the parameters for disp_text.
if (0 != (HTuple(hv_Row==HTuple()).TupleOr(hv_Column==HTuple())))
{
return;
}
if (0 != (hv_Row==-1))
{
hv_Row = 12;
}
if (0 != (hv_Column==-1))
{
hv_Column = 12;
}
//
//Convert the parameter Box to generic parameters.
hv_GenParamName = HTuple();
hv_GenParamValue = HTuple();
if (0 != ((hv_Box.TupleLength())>0))
{
if (0 != (HTuple(hv_Box[0])==HTuple("false")))
{
//Display no box
hv_GenParamName = hv_GenParamName.TupleConcat("box");
hv_GenParamValue = hv_GenParamValue.TupleConcat("false");
}
else if (0 != (HTuple(hv_Box[0])!=HTuple("true")))
{
//Set a color other than the default.
hv_GenParamName = hv_GenParamName.TupleConcat("box_color");
hv_GenParamValue = hv_GenParamValue.TupleConcat(HTuple(hv_Box[0]));
}
}
if (0 != ((hv_Box.TupleLength())>1))
{
if (0 != (HTuple(hv_Box[1])==HTuple("false")))
{
//Display no shadow.
hv_GenParamName = hv_GenParamName.TupleConcat("shadow");
hv_GenParamValue = hv_GenParamValue.TupleConcat("false");
}
else if (0 != (HTuple(hv_Box[1])!=HTuple("true")))
{
//Set a shadow color other than the default.
hv_GenParamName = hv_GenParamName.TupleConcat("shadow_color");
hv_GenParamValue = hv_GenParamValue.TupleConcat(HTuple(hv_Box[1]));
}
}
//Restore default CoordSystem behavior.
if (0 != (hv_CoordSystem!=HTuple("window")))
{
hv_CoordSystem = "image";
}
//
if (0 != (hv_Color==HTuple("")))
{
//disp_text does not accept an empty string for Color.
hv_Color = HTuple();
}
//
DispText(hv_WindowHandle, hv_String, hv_CoordSystem, hv_Row, hv_Column, hv_Color,
hv_GenParamName, hv_GenParamValue);
return;
}
#ifndef NO_EXPORT_MAIN
// Main procedure
void action()
{
// Local iconic variables
HObject ho_Image, ho_GridRegion, ho_ImageReduced;
HObject ho_Cross, ho_ConnectingLines1, ho_Map, ho_Meshes;
HObject ho_ImageMapped, ho_Image1, ho_ImageMapped1, ho_SymbolRegions;
// Local control variables
HTuple hv_Width, hv_NumSquares, hv_WindowHandle;
HTuple hv_MinCOntrast, hv_Radius, hv_SigmaSaddlePoints;
HTuple hv_Threshold, hv_Row, hv_Column, hv_SigmaConnectGridPoints;
HTuple hv_MaxDist, hv_GridSpacing, hv_roateAngle, hv_BarCodeHandle;
HTuple hv_DecodedDataStrings;
hv_Width = 0.18;
hv_NumSquares = 17;
CreateRectificationGrid(hv_Width, hv_NumSquares, "rectification_grid.ps");
//先读取有 map的图片,生成map后,再读取原图,再映射
ReadImage(&ho_Image, "can_with_grid");
SetWindowAttr("background_color","black");
OpenWindow(0,0,512,512,0,"visible","",&hv_WindowHandle);
HDevWindowStack::Push(hv_WindowHandle);
hv_MinCOntrast = 25;
hv_Radius = 10;
FindRectificationGrid(ho_Image, &ho_GridRegion, hv_MinCOntrast, hv_Radius);
ReduceDomain(ho_Image, ho_GridRegion, &ho_ImageReduced);
hv_SigmaSaddlePoints = 1.5;
hv_Threshold = 5;
SaddlePointsSubPix(ho_ImageReduced, "facet", hv_SigmaSaddlePoints, hv_Threshold,
&hv_Row, &hv_Column);
GenCrossContourXld(&ho_Cross, hv_Row, hv_Column, 6, 0.785398);
hv_SigmaConnectGridPoints = 0.9;
hv_MaxDist = 5.0;
ConnectGridPoints(ho_ImageReduced, &ho_ConnectingLines1, hv_Row, hv_Column, hv_SigmaConnectGridPoints,
hv_MaxDist);
hv_GridSpacing = 20;
hv_roateAngle = "auto";
GenGridRectificationMap(ho_ImageReduced, ho_ConnectingLines1, &ho_Map, &ho_Meshes,
hv_GridSpacing, hv_roateAngle, hv_Row, hv_Column, "bilinear");
MapImage(ho_Image, ho_Map, &ho_ImageMapped);
ReadImage(&ho_Image1, "can");
MapImage(ho_Image1, ho_Map, &ho_ImageMapped1);
//dev_display (Image)
//dev_display (ImageMapped1)
//disp_continue_message (WindowHandle, 'black', 'true')
//stop ()
CreateBarCodeModel(HTuple(), HTuple(), &hv_BarCodeHandle);
SetBarCodeParam(hv_BarCodeHandle, "element_size_min", 1);
SetBarCodeParam(hv_BarCodeHandle, "barcode_width_min", 50);
FindBarCode(ho_ImageMapped1, &ho_SymbolRegions, hv_BarCodeHandle, "auto", &hv_DecodedDataStrings);
if (HDevWindowStack::IsOpen())
ClearWindow(HDevWindowStack::GetActive());
if (HDevWindowStack::IsOpen())
SetDraw(HDevWindowStack::GetActive(),"margin");
if (HDevWindowStack::IsOpen())
DispObj(ho_ImageMapped1, HDevWindowStack::GetActive());
if (HDevWindowStack::IsOpen())
DispObj(ho_SymbolRegions, HDevWindowStack::GetActive());
disp_message(hv_WindowHandle, hv_DecodedDataStrings, "window", 12, 12, "red", "true");
//connect_grid_points (ImageReduced, ConnectingLines, Width, Width, 0.9, 5.5)
//dev_display (ConnectingLines)
}
#ifndef NO_EXPORT_APP_MAIN
#ifdef __APPLE__
// On OS X systems, we must have a CFRunLoop running on the main thread in
// order for the HALCON graphics operators to work correctly, and run the
// action function in a separate thread. A CFRunLoopTimer is used to make sure
// the action function is not called before the CFRunLoop is running.
// Note that starting with macOS 10.12, the run loop may be stopped when a
// window is closed, so we need to put the call to CFRunLoopRun() into a loop
// of its own.
HTuple gStartMutex;
H_pthread_t gActionThread;
HBOOL gTerminate = FALSE;
static void timer_callback(CFRunLoopTimerRef timer, void *info)
{
UnlockMutex(gStartMutex);
}
static Herror apple_action(void **parameters)
{
// Wait until the timer has fired to start processing.
LockMutex(gStartMutex);
UnlockMutex(gStartMutex);
try
{
action();
}
catch (HException &exception)
{
fprintf(stderr," Error #%u in %s: %s\n", exception.ErrorCode(),
(const char *)exception.ProcName(),
(const char *)exception.ErrorMessage());
}
// Tell the main thread to terminate itself.
LockMutex(gStartMutex);
gTerminate = TRUE;
UnlockMutex(gStartMutex);
CFRunLoopStop(CFRunLoopGetMain());
return H_MSG_OK;
}
static int apple_main(int argc, char *argv[])
{
Herror error;
CFRunLoopTimerRef Timer;
CFRunLoopTimerContext TimerContext = { 0, 0, 0, 0, 0 };
CreateMutex("type","sleep",&gStartMutex);
LockMutex(gStartMutex);
error = HpThreadHandleAlloc(&gActionThread);
if (H_MSG_OK != error)
{
fprintf(stderr,"HpThreadHandleAlloc failed: %d\n", error);
exit(1);
}
error = HpThreadCreate(gActionThread,0,apple_action);
if (H_MSG_OK != error)
{
fprintf(stderr,"HpThreadCreate failed: %d\n", error);
exit(1);
}
Timer = CFRunLoopTimerCreate(kCFAllocatorDefault,
CFAbsoluteTimeGetCurrent(),0,0,0,
timer_callback,&TimerContext);
if (!Timer)
{
fprintf(stderr,"CFRunLoopTimerCreate failed\n");
exit(1);
}
CFRunLoopAddTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);
for (;;)
{
HBOOL terminate;
CFRunLoopRun();
LockMutex(gStartMutex);
terminate = gTerminate;
UnlockMutex(gStartMutex);
if (terminate)
break;
}
CFRunLoopRemoveTimer(CFRunLoopGetCurrent(),Timer,kCFRunLoopCommonModes);
CFRelease(Timer);
error = HpThreadHandleFree(gActionThread);
if (H_MSG_OK != error)
{
fprintf(stderr,"HpThreadHandleFree failed: %d\n", error);
exit(1);
}
ClearMutex(gStartMutex);
return 0;
}
#endif
int main(int argc, char *argv[])
{
int ret = 0;
try
{
#if defined(_WIN32)
SetSystem("use_window_thread", "true");
#elif defined(__linux__) && !defined(__arm__)
XInitThreads();
#endif
// Default settings used in HDevelop (can be omitted)
SetSystem("width", 512);
SetSystem("height", 512);
#ifndef __APPLE__
action();
#else
ret = apple_main(argc,argv);
#endif
}
catch (HException &exception)
{
fprintf(stderr," Error #%u in %s: %s\n", exception.ErrorCode(),
(const char *)exception.ProcName(),
(const char *)exception.ErrorMessage());
ret = 1;
}
while(true);
return ret;
}
#endif
#endif