将halcon写好的代码,转成c++代码,配置好visual studio环境后,在vs2019中编译运行。
以一张图片为示例:
计算中间区域的长度和宽度。
在halcon中写一个函数,
*get_diameter_of_handain_region (Mask, Row, Column, Phi, Length1, Length2)
get_image_size(Mask,Width,Height)
threshold(Mask,Region,255,255)
opening_circle(Region,Region1,12)
connection(Region1,ConnectedRegions)
select_shape(ConnectedRegions,SelectedRegions,'area','and',300, 99999)
area_center (SelectedRegions, Area, Row, Column)
dis := abs(Row - Height*0.5)+abs(Column - Width*0.5)
tuple_min(dis,dis_min)
tuple_find(dis,dis_min,indice)
select_obj (SelectedRegions, RegionSelected, indice + 1)
gen_contour_region_xld(RegionSelected,Contour,'border')
smooth_contours_xld(Contour,SmoothContour,15)
gen_region_contour_xld(SmoothContour,SmoothRegion,'filled')
smallest_rectangle2(SmoothRegion,Row,Column,Phi,Length1,Length2)
gen_rectangle2(Rectangle,Row,Column,Phi,Length1,Length2)
return ()
在halcon的当前页面中,打开该函数,点击选项卡中的"文件"--”导出程序"
选择"当前函数",就会生成一个cpp文件
///
// File generated by HDevelop for HALCON/C++ Version 22.05.0.0
// Non-ASCII strings in this file are encoded in local-8-bit encoding (cp936).
// Ensure that the interface encoding is set to locale encoding by calling
// SetHcppInterfaceStringEncodingIsUtf8(false) at the beginning of the program.
//
// Please note that non-ASCII characters in string constants are exported
// as octal codes in order to guarantee that the strings are correctly
// created on all systems, independent on any compiler settings.
//
// Source files with different encoding should not be mixed in one project.
///
#ifndef __APPLE__
# include "HalconCpp.h"
# include "HDevThread.h"
#else
# ifndef HC_LARGE_IMAGES
# include <HALCONCpp/HalconCpp.h>
# include <HALCONCpp/HDevThread.h>
# else
# include <HALCONCppxl/HalconCpp.h>
# include <HALCONCppxl/HDevThread.h>
# endif
#endif
using namespace HalconCpp;
// Procedure declarations
void get_diameter_of_handain_region (HObject ho_Mask, HTuple *hv_Row, HTuple *hv_Column,
HTuple *hv_Phi, HTuple *hv_Length1, HTuple *hv_Length2);
// Procedures
void get_diameter_of_handain_region (HObject ho_Mask, HTuple *hv_Row, HTuple *hv_Column,
HTuple *hv_Phi, HTuple *hv_Length1, HTuple *hv_Length2)
{
// Local iconic variables
HObject ho_Region, ho_Region1, ho_ConnectedRegions;
HObject ho_SelectedRegions, ho_RegionSelected, ho_Contour;
HObject ho_SmoothContour, ho_SmoothRegion, ho_Rectangle;
// Local control variables
HTuple hv_Width, hv_Height, hv_Area, hv_dis;
HTuple hv_dis_min, hv_indice;
GetImageSize(ho_Mask, &hv_Width, &hv_Height);
Threshold(ho_Mask, &ho_Region, 255, 255);
OpeningCircle(ho_Region, &ho_Region1, 12);
Connection(ho_Region1, &ho_ConnectedRegions);
SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", 300, 99999);
AreaCenter(ho_SelectedRegions, &hv_Area, &(*hv_Row), &(*hv_Column));
hv_dis = (((*hv_Row)-(hv_Height*0.5)).TupleAbs())+(((*hv_Column)-(hv_Width*0.5)).TupleAbs());
TupleMin(hv_dis, &hv_dis_min);
TupleFind(hv_dis, hv_dis_min, &hv_indice);
SelectObj(ho_SelectedRegions, &ho_RegionSelected, hv_indice+1);
GenContourRegionXld(ho_RegionSelected, &ho_Contour, "border");
SmoothContoursXld(ho_Contour, &ho_SmoothContour, 15);
GenRegionContourXld(ho_SmoothContour, &ho_SmoothRegion, "filled");
SmallestRectangle2(ho_SmoothRegion, &(*hv_Row), &(*hv_Column), &(*hv_Phi), &(*hv_Length1),
&(*hv_Length2));
GenRectangle2(&ho_Rectangle, (*hv_Row), (*hv_Column), (*hv_Phi), (*hv_Length1),
(*hv_Length2));
return;
}
将该cpp中的函数声明,以及函数实现,复制到vs2019的工程的代码中,然后配置好环境,即可完成调用。
vs2019中的halcon环境配置
halcon设置库目录:
头文件:
C:\Program Files\MVTec\HALCON-22.05-Progress\include
C:\Program Files\MVTec\HALCON-22.05-Progress\include\halconcpp
链接器》常规》附加库目录:
C:\Program Files\MVTec\HALCON-22.05-Progress\lib\x64-win64
链接器》输入》附加依赖项:halconcpp.lib
若生成的算法库,用到的dll文件:
halcon.dll
halconcpp.dll
halconcppxl.dll
注意opencv的cv::mat和halcon的HObject的转换,以及c++中的int, double, string和halcon中的HTuple的类型的转换。
基于halcon测量中区域的长和宽,完整的c++代码实现如下,共3个文件:
halcon_hx.h
#include "HalconCpp.h"
#include "HDevThread.h"
#include <opencv2/opencv.hpp>
using namespace HalconCpp;
HalconCpp::HObject __Mat1ToHObject__(const cv::Mat& image);
cv::Mat HImageToMat(const HalconCpp::HImage& hImg);
HalconCpp::HObject MatToHObject(const cv::Mat& image);
void get_diameter_of_handain_region(HObject ho_Mask, HTuple* hv_Row, HTuple* hv_Column,
HTuple* hv_Phi, HTuple* hv_Length1, HTuple* hv_Length2);
halcon_hx.cpp
#include "halcon_hx.h"
HalconCpp::HObject __Mat1ToHObject__(const cv::Mat& image)
{
HalconCpp::HObject hObj = HalconCpp::HObject();
GenImage1(&hObj, "byte", image.cols, image.rows, (Hlong)image.ptr(0));
return hObj;
}
HalconCpp::HObject MatToHObject(const cv::Mat& image)
{
HalconCpp::HObject hObj;
if (image.type() == CV_8UC1)
return __Mat1ToHObject__(image);
else if (image.type() == CV_8UC3)
{
std::vector<cv::Mat> vec;
cv::split(image, vec);
cv::Mat imgB = vec[0];
cv::Mat imgG = vec[1];
cv::Mat imgR = vec[2];
HalconCpp::HObject himgR = __Mat1ToHObject__(imgR);
HalconCpp::HObject himgG = __Mat1ToHObject__(imgG);
HalconCpp::HObject himgB = __Mat1ToHObject__(imgB);
HalconCpp::Compose3(himgR, himgG, himgB, &hObj);
}
return hObj;
}
cv::Mat HImageToMat(const HalconCpp::HImage& hImg)
{
cv::Mat mat;
int channels = hImg.CountChannels()[0].I();
HalconCpp::HImage hImage = hImg.ConvertImageType("byte");
Hlong hW = 0, hH = 0; HString cType;
if (channels == 1)
{
void* r = hImage.GetImagePointer1(&cType, &hW, &hH);
mat.create(int(hH), int(hW), CV_8UC1);
memcpy(mat.data, static_cast<unsigned char*>(r), int(hW * hH));
}
else if (channels == 3)
{
void* r = NULL, * g = NULL, * b = NULL;
hImage.GetImagePointer3(&r, &g, &b, &cType, &hW, &hH);
mat.create(int(hH), int(hW), CV_8UC3);
std::vector<cv::Mat> vec(3);
vec[0].create(int(hH), int(hW), CV_8UC1);
vec[1].create(int(hH), int(hW), CV_8UC1);
vec[2].create(int(hH), int(hW), CV_8UC1);
memcpy(vec[2].data, static_cast<unsigned char*>(r), int(hW * hH));
memcpy(vec[1].data, static_cast<unsigned char*>(g), int(hW * hH));
memcpy(vec[0].data, static_cast<unsigned char*>(b), int(hW * hH));
cv::merge(vec, mat);
}
return mat;
}
void get_diameter_of_handain_region(HObject ho_Mask, HTuple* hv_Row, HTuple* hv_Column,
HTuple* hv_Phi, HTuple* hv_Length1, HTuple* hv_Length2)
{
// Local iconic variables
HObject ho_Region, ho_Region1, ho_ConnectedRegions;
HObject ho_SelectedRegions, ho_RegionSelected, ho_Contour;
HObject ho_SmoothContour, ho_SmoothRegion, ho_Rectangle;
// Local control variables
HTuple hv_Width, hv_Height, hv_Area, hv_dis;
HTuple hv_dis_min, hv_indice;
GetImageSize(ho_Mask, &hv_Width, &hv_Height);
Threshold(ho_Mask, &ho_Region, 255, 255);
OpeningCircle(ho_Region, &ho_Region1, 12);
Connection(ho_Region1, &ho_ConnectedRegions);
SelectShape(ho_ConnectedRegions, &ho_SelectedRegions, "area", "and", 300, 99999);
AreaCenter(ho_SelectedRegions, &hv_Area, &(*hv_Row), &(*hv_Column));
hv_dis = (((*hv_Row) - (hv_Height * 0.5)).TupleAbs()) + (((*hv_Column) - (hv_Width * 0.5)).TupleAbs());
TupleMin(hv_dis, &hv_dis_min);
TupleFind(hv_dis, hv_dis_min, &hv_indice);
SelectObj(ho_SelectedRegions, &ho_RegionSelected, hv_indice + 1);
GenContourRegionXld(ho_RegionSelected, &ho_Contour, "border");
SmoothContoursXld(ho_Contour, &ho_SmoothContour, 15);
GenRegionContourXld(ho_SmoothContour, &ho_SmoothRegion, "filled");
SmallestRectangle2(ho_SmoothRegion, &(*hv_Row), &(*hv_Column), &(*hv_Phi), &(*hv_Length1),
&(*hv_Length2));
GenRectangle2(&ho_Rectangle, (*hv_Row), (*hv_Column), (*hv_Phi), (*hv_Length1),
(*hv_Length2));
return;
}
main.cpp
#include <iostream>
#include <opencv2/opencv.hpp>
#include "halcon_hx.h"
int main()
{
std::string pngpath= "D:/my_code/halcon/jianhexian/images/jhx_solder/000_crop_mask.png";
cv::Mat crop_mask = cv::imread(pngpath, -1);
printf("crop_mask: %d %d %d\n", crop_mask.rows, crop_mask.cols, crop_mask.channels());
HObject HObj_mask = MatToHObject(crop_mask);
HTuple hv_Row, hv_Column, hv_Phi, hv_Length1, hv_Length2;
get_diameter_of_handain_region(HObj_mask, &hv_Row, &hv_Column, &hv_Phi, &hv_Length1, &hv_Length2);
double hd_row = hv_Row[0].D();
double hd_col = hv_Column[0].D();
double hd_phi = hv_Phi[0].D();
double hd_length1 = hv_Length1[0].D();
double hd_length2 = hv_Length2[0].D();
printf("hd_row:%.3lf hd_col:%.3lf hd_phi:%.3lf hd_length1:%.3lf hd_length2:%.3lf\n", hd_row, hd_col, hd_phi, hd_length1, hd_length2);
system("pause");
return 0;
}
std::string和HTuple之间的数据转换
//std::string转HTuple
std::string light_type="backlight";
const char* pLightType = light_type.c_str();
HTuple hv_light_type = (HTuple)pLightType;
//HTuple转std::string
HTuple hv_light_type = "backlight";
std::string light_type = hv_light_type.S();
参考博客:
VS2019配置Halcon的c++环境傻瓜式教程_vs2019 c++ halcon配置_asd_sz的博客-优快云博客