点云计算的库都是C++的,没办法,VC本周开始学习一下,先拿Halcon+MFC练手熟悉流程。
环境Win7-64,VS2015,Halcon18.11;代码晚上教程练习完一起上传
1.打开VS2015新建项目,
2。设置环境,点击配置管理器,将平台设置为X64。
3。添加C++目录,
“C:\Program Files\MVTec\HALCON-18.11-Progress\include";“C:\Program Files\MVTec\HALCON-18.11-Progress\include\halconcpp"
3。添加HalconCPP目录,不用带引号
C:\Program Files\MVTec\HALCON-18.11-Progress\lib\x64-win64
4。添加附加依赖项,
配置到此完成==========================================注意双引号的中英文,中文引号浪费是十分钟才发现。
5.添加代码,头文件添加
在MFC2015_Halcon18_Test2Dlg.h最上面添加
#include "HalconCpp.h"
using namespace HalconCpp;
6.MFC2015_Halcon18_Test2Dlg.cpp中添加代码(HalconCpp::可以取点,因为前面已经引用命名空间了)
窗口添加按钮和单击事件
单击事件内添加代码
//===============
HTuple m_ImageWidth, m_ImageHeight, Type, WindowHandle, pointer;
HObject Image;
void CMFC2015_Halcon18_Test2Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
//TODO: 在此添加控件通知处理程序代码
HalconCpp::ReadImage(&Image, "D:\\pen-01.png"); //地址改为你电脑上一张图片的地址
HalconCpp::GetImagePointer1(Image, &pointer, &Type, &m_ImageWidth, &m_ImageHeight);
HalconCpp::SetWindowAttr("background_color", "black");
HalconCpp::OpenWindow(0, 80, m_ImageWidth, m_ImageHeight, 0, "", "", &WindowHandle);
HalconCpp::DispObj(Image, WindowHandle);
}
//===============
7.运行看看,完成
8.将窗口显示都住窗口
这是因为open_window(0,0,Width/2,Height/2,0,"","",&WindowHandle);中所指定的父窗口为0,意味着指定的父窗口为桌面,此时需要将其父窗口改为对话框。为此,在该句所在的响应的函数中添加此句:
Hlong MainWndID =(Hlong) m_hWnd;
并修改open_window的参数,用MainWndID代替0.
HTuple m_ImageWidth, m_ImageHeight, Type, WindowHandle, pointer;
HObject Image;
// Local iconic variables
HObject Region, ConnectedRegions, SelectedRegions;
HObject RegionClosing, RegionDifference, ConnectedRegions1;
HObject SelectedRegions1;
// Local control variables
HTuple AcqHandle, Pointer, Width, Height;
void CMFC2015_Halcon18_Test2Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
//TODO: 在此添加控件通知处理程序代码
HalconCpp::ReadImage(&Image, "D:\\pen-01.png");
HalconCpp::GetImagePointer1(Image, &pointer, &Type, &m_ImageWidth, &m_ImageHeight);
HalconCpp::SetWindowAttr("background_color", "black");
Hlong MainWndID = (Hlong)m_hWnd;
HalconCpp::OpenWindow(0, 80, m_ImageWidth, m_ImageHeight, MainWndID, "", "", &WindowHandle);
HalconCpp::DispObj(Image, WindowHandle);
SetColor(WindowHandle, "red");
Threshold(Image, &Region, 0, 50);
Connection(Region, &ConnectedRegions);
SelectShape(ConnectedRegions, &SelectedRegions, "rect2_len1", "and", 1, 55000);
ClosingRectangle1(SelectedRegions, &RegionClosing, 80, 80);
Difference(RegionClosing, SelectedRegions, &RegionDifference);
Connection(RegionDifference, &ConnectedRegions1);
SelectShape(ConnectedRegions1, &SelectedRegions1, "area", "and", 1, 9999900);
ClearWindow(WindowHandle);
DispObj(Image, WindowHandle);
DispObj(SelectedRegions1, WindowHandle);
}
完成