halocn/C++ (第一篇)

在使用C++编写halcon之前,确定自己有较好的C++基础,并熟悉一套开发平台如VC

Programmers_guide.pdf chapter7中有关于creating Aplicatin with halcon/c++的详细介绍

以vs2008为例 工具---》选项  (有图介绍不配文字啦)

用C++写例子  ~~取狒狒的眼睛

//

#include "stdafx.h"

#include "HalconCpp.h"

#pragma comment(lib,"halconcpp.lib")

int main()

{

using namespace Halcon;

HImage Mandrill("monkey");

HWindow w;

w.Display(Mandrill);

w.Click();

HRegion Bright = (Mandrill >=128); // 取亮度大于128的区域

HRegionArray conn = Bright.Connection();

//取面积在500~9000的区域,狒狒的大鼻子也是大于128的,显然不是我们检测的重点根据面积舍去它

HRegionArray large = conn.SelectShape("area","and",500,9000);

//眼睛是椭圆形的anisometry 是离心率即ra/rb 两半径之比,目测来看取1.2 -1.7

HRegionArray eyes = large.SelectShape("anisometry","and",1.2,1.7);

eyes.Display(w);

w.Click();

return 0;

}

//~~ 提取字符

#include "stdafx.h"

#include "HalconCpp.h"

#include <iostream>

#pragma comment(lib,"halconcpp.lib")

using namespace std;

using namespace Halcon;

int main()

{

HImage image("alpha1");

HRegion region;

HWindow w;

long threshold;

image.Display(w);

w.Click();

// image.GetDomain -- 获得图像region

//sigma --- 高斯平滑系数

//Percent --- 前景背景灰度不同百分比 95 代表 至多有5%是最大灰度值

region = image.CharThreshold(image.GetDomain(),2,95,&threshold);

image.Display(w);

region.Display(w);

w.Click();

cout<<"Threshold for ‘alpha‘: "<<threshold;

w.Click();

return 0;

}

注意:参考手册中的操作符不全,A complete list can be found in the ?le include\cpp\HCPPGlobal.h.

1.关于参数:

①.halcon的输入参数 (input parameters)不会被操作符改变,也就是说是按值传递的

如下代码示例

HImage original_image("monkey"), smoothed_image;

smoothed_image = original_image.MeanImage(11, 11);

meanimage不会改变original_image,而中值后的图像保存在返回值 smoothed_image中

②.与之相反的是输出参数(output parameters),所以传的是指针或者引用

2.halcon /C++关于面向对象和过程

这是通过类来调用方法的 ,面向对象方式

HImage image("barcode/ean13/ean1301");

HTuple paramName, paramValue;

HBarCode barcode(paramName, paramValue);

HRegionArray code_region;

HTuple result;

code_region = barcode.FindBarCode(image, "EAN-13", &result);

code_region = image.FindBarCode(barcode, "EAN-13", &result);

面向过程的方式

Hobject image;

HTuple barcode;

Hobject code_region;

HTuple result;

read_image(&image, "barcode/ean13/ean1301");

create_bar_code_model(HTuple(), HTuple(), &barcode);

find_bar_code(image, &code_region, barcode, "EAN-13", &result);

3.halcon/c++所有的 对象 可以在C:\Program Files\MVTec\HALCON-10.0\include\cpp 中找到 (以halcon安装在C盘为例)

4.halcon/c++的数组模式

我们可以用操作符来 打开多个图片 region 等等 看该操作是否支持 数组模式 要看它的定义了

如图所示

数组的几点说明:

① 创建,初始化数组

C++ 可以通过 []

C 通过 gen_empty_obj 增加数组 concat_obj

②操纵对象

C++ 通过[N] ,统计数组元素个数 num()

C  select_obj       count_obj

③iconic 数组以 0 开始  , Hobject 数组 以 1 开始

④通过例子说明第四点

#include "stdafx.h"

#include "HalconCpp.h"

#include <iostream>

#pragma comment(lib,"halconcpp.lib")

using namespace std;

using namespace Halcon;

//~~ 提取字符

int main()

{

HImageArray images;

HRegionArray regions;

HTuple thresholds;

HWindow w;

for (int i = 1; i <=2;++i)

{

//HTuple("alpha") +i 的解释

//HTuple 中有对 operater + 的重载

//HTuple("alpha") +i ~~~ alpha1 alpha2.....

images[i-1] = HImage::ReadImage(HTuple("alpha") +i);

}

//这一句相当于对 images中所有的对象进行了 charthreshold操作

regions = images.CharThreshold(images[0].GetDomain(),2,95,&thresholds);

for (int i = 0 ; i<regions.Num();++i)

{

images[i].Display(w);

w.Click();

regions[i].Display(w);

w.Click();

cout<<"threshold : "<<thresholds[i].L()<<endl;

}

w.Click();

return 0;

}

5. 关于参数转换

• Converting Hobject into iconic parameter classes

Hobject p_image;

read_image(&p_image, "barcode/ean13/ean1301");

HImage o_image(p_image);

Iconic parameters can be converted from Hobject to, e.g., HImage simply by calling the construc-

tor with the procedural variable as a parameter.

• Converting handles into handle classes

HTuple p_barcode;

create_bar_code_model(HTuple(), HTuple(), &p_barcode);

HBarCode o_barcode;

o_barcode.SetHandle(p_barcode[0]);

o_code_region = o_barcode.FindBarCode(o_image, "EAN-13", &result);

Handles cannot be converted directly via a constructor; instead, you call the method SetHandle()

with the procedural handle as a parameter.

• Converting handle classes into handles

p_barcode = o_barcode.GetHandle();

Similarly, a handle can be extracted from the corresponding class via the method GetHandle().

You can even omit the method, as the handle classes provide cast operators which convert them

automatically into handles.

p_barcode = o_barcode;

•Converting iconic parameter classes into Hobject

Hobject p_code_region = o_code_region.Id();

Iconic parameters can be converted from classes like HRegion back into Hobject via the method

Id(). In contrast to the handle classes no cast operator is provided.

• Converting HWindow into a window handle

long p_window;

open_window(0, 0, width/2, height/2, 0, "visible", "", &p_window);

HWindow o_window(0, 0, 100, 100, 0, "visible", "");

p_window = o_window.WindowHandle();

disp_obj(p_code_region, p_window);

In contrast to other handles, procedural window handles cannot be converted into instances of the

class HWindow! However, you can extract the handle from an instance of HWindow via the method

WindowHandle().

6 . The halcon Parameter classes

6.1 Iconic Objects

基类为Hobject

HImage ,HRegion,HXLD 基类为Hobject

6.1.1 Reions

region 是图像平面的一组坐标点 , 它可能不是连接的,可能含有洞,它可以比图像还大。

通过一个例子来了解HRegion

#include "stdafx.h"

#include "HalconCpp.h"

#include <iostream>

#pragma comment(lib,"halconcpp.lib")

using namespace std;

using namespace Halcon;

/*

例子介绍了region的用法 ,更详细的介绍在 progarma_guide  chapter6.1.1

*/

int main()

{

HImage image("mreut");

HRegion region;

HWindow w;

region = image >=190;

w.SetColor("red");

region.Display(w);

w.Click();

//region.Fillup() 调用方法

HRegion filled = region.FillUp();

filled.Display(w);

w.Click();

//运算符重载 开运算 >> 腐蚀 <<膨胀 , 半径4.5

HRegion open = (filled >>4.5)<<4.5;

w.SetColor("green");

open.Display(w);

w.Click();

w.ClearWindow();

//平移

HDPoint2D trans(-100 , -150);

HRegion moved = open + trans;

HRegion zoomed = moved * 2.0;

zoomed.Display(w);

w.Click();

return 0;

}

#include "stdafx.h"

#include "HalconCpp.h"

#include <iostream>

#pragma comment(lib,"halconcpp.lib")

using namespace std;

using namespace Halcon;

/*

例子介绍了regionArry的用法

*/

int main()

{

HImage image("fabrik");

// 1 1 row col 越小越精细

// 4 容差 种子和被检测像素值差

// 100 最小region size

HRegionArray regs = image.Regiongrowing(1,1,4,100);

HWindow w;

w.SetColored(12);

image.Display(w);

w.Click();

regs.Display(w);

w.Click();

HRegionArray rect;

for (int i = 0 ; i < regs.Num() ; ++ i )

{

// compactness ~~ 在图像处理中经常叫做圆形度 而这里用作 region的紧密度

//C = L^2 / (4 F pi)) L = 周长 F = 面积

//C = 1 是 为圆 ,region 较长是 或者region 有洞洞时 C >1

//这里就是得出了 比较方正的 没小洞洞的 region

if ( (regs[i].Area() > 1000) && ( regs[i].Compactness() < 1.5) )

rect.Append(regs[i]); //RegionArry 添加元素哦

}

image.Display(w);

rect.Display(w);

w.Click();

return 0;

}

6.1.2 Images

#include "stdafx.h"

#include "HalconCpp.h"

#include <iostream>

#pragma comment(lib,"halconcpp.lib")

using namespace std;

using namespace Halcon;

/*

drawregion 画感兴趣区域

meanimage 做个中值滤波

和中值滤波前图像比较

*/

int main()

{

HImage image("mreut");

HWindow w;

image.Display(w);

cout<< " width = " << image.Width()<<endl;

cout << " height = " << image.Height() <<endl;

HRegion mask= w.DrawRegion();

//  & 重载  取感兴趣区域的图像

HImage reduced = image & mask;

w.ClearWindow();

reduced.Display(w);

w.Click() ;

HImage mean = reduced.MeanImage(61,61);

mean.Display(w);

HRegion reg = reduced.DynThreshold(mean,3,"light");

reg.Display(w);

w.Click();

return 0;

}

6.1.2.2  像素值

操纵像素的两种方法

int main()

{

HByteImage in("mreut");

HByteImage out = in ;

HWindow w;

in.Display(w);

w.Click() ;

int nwidht = in.Width();

int nheight = in.Height();

long end = nwidht * nheight;

for (int k = 0 ; k < end ; k++)

{

//方法1

//int pix = in.GetPixVal(k);

//out.SetPixVal(k,255 - pix);

//方法2

out[k]= 255 - in[k];

}

out.Display(w);

w.Click();

return 0;

}

6.1.3 XLD Objects

XLD is the abbreviation for eXtended Line Description. This is a data structure used for describing areas

(e.g., arbitrarily sized regions or polygons) or any closed or open contour, i.e., also lines. In contrast to

regions, which represent all areas at pixel precision, XLD objects provide subpixel precision. There are

two basic XLD structures: contours and polygons.

Similarly to images, HALCON/C++ provides both a base class HXLD and a set of specialized classes

derived from HXLD, e.g., HXLDCont for contours or HXLDPoly for polygons. For all classes there exists

a corresponding container class, e.g., HXLDArray.

In contrast to the classes described in the previous sections, the XLD classes provide only member

functions corresponding to HALCON operators (see also section 5.2.2 on page 35).

6.1.4 Low - level iconic objects 底层iconic 对象

the class Hobject is used for all iconic parameters, be it an image, a region, or even an image array

In fact, the class Hobject is HALCON’s basic class for accessing the internal data management

Furthermore, Hobject serves as the basis for the class HObject and the derived classes, e.g., HImage.

As noted above, an instance of Hobject can also contain a tuple (array) of iconic objects. Unfortunately,

Hobject provides no special member functions to add objects or select them; instead, you must use the

operators gen_empty_obj, concat_obj, select_obj, and count_obj 。

一个Hobject实例可以包含一组iconic 对象,但Hobject 没有提供增加或选择它们的成员函数,所以必须通过

gen_empty_obj, concat_obj, select_obj, and count_obj 来解决。

6.2 Control parameters

来自为知笔记(Wiz)

附件列表

时间: 2024-07-31 19:16:14

halocn/C++ (第一篇)的相关文章

CSS px, em, 和rem; float以及clear(第一篇学习)

px:相对长度,相对于屏幕分辨率: em:相对长度单位,相对于当前对象内文本的字体尺寸.如当前对行内文本的字体尺寸未被人为设置,则相对于浏览器的默认字体尺寸.  任意浏览器的默认字体高都是16px.所有未经调整的浏览器都符合: 1em=16px.那么12px=0.75em,10px=0.625em.为了简化font-size的换算,需要在css中的body选择器中声明Font-size=62.5%,这就使em值变为 16px*62.5%=10px, 这样 12px=1.2em, 10px=1em

无限互联奖学金文章连载北京总部四十九期胡梦川 第一篇

无限互联奖学金文章连载北京总部四十九期胡梦川 第一篇: 今天是来到无限互联的第四天,严格来说已经第六天了,刚来就是开班典礼,给人一种很好的氛围.老师讲了很多关于以后学习的技巧和规定,我的第一感觉是,比备战高考还要严格,不过这才能体现一个组织的负责任.正式开讲才感觉到这个班级的大神无处不在,不努力根本赶不上,就是这个学习氛围和高强度的练习很重要.多用心你才能感觉到有些事其实很简单.关于学习时间大家基本都是一天不动的在敲代码,等于给自己一个机会吧.时间飞逝,抓住机会才重要.刚来第一周,感受最深就是好

第一篇

奖学金文章连载北京总部四十九期胡梦川 第一篇: 今天是来到无限互联的第四天,严格来说已经第六天了,刚来就是开班典礼,给人一种很好的氛围.老师讲了很多关于以后学习的技巧和规定,我的第一感觉是,比备战高考还要严格,不过这才能体现一个组织的负责任.正式开讲才感觉到这个班级的大神无处不在,不努力根本赶不上,就是这个学习氛围和高强度的练习很重要.多用心你才能感觉到有些事其实很简单.关于学习时间大家基本都是一天不动的在敲代码,等于给自己一个机会吧.时间飞逝,抓住机会才重要.刚来第一周,感受最深就是好多事做了

SaltStack 入门到精通 - 第一篇: 安装SaltStack

实际环境的设定: 系统环境: centos6 或centos5 实验机器: 192.168.1.100 软件需求: salt 套件,及其需求环境 实验目的: 成功安装salt,并实现salt主从间通讯 特殊设置: 其它目的: 安装SaltStack(下面简称为salt) epel安装:salt安装需要epel源支持,所以在安装salt前需要先安装epel包 # centos5 下载下面rpm  wget -O    epel.rpm https://dl.fedoraproject.org/pu

jstl标签 core fmt fn函数使用参考(为第一篇的补充,更为实用)

JSTL标签 参考手册 前言 ========================================================================= JSTL标签库,是日常开发经常使用的,也是众多标签中性能最好的.把常用的内容,放在这里备份一份,随用随查.尽量做到不用查,就可以随手就可以写出来.这算是Java程序员的基本功吧,一定要扎实. JSTL全名为JavaServer Pages Standard Tag Library,目前最新的版本为1.1版.JSTL是由J

Python开发【第一篇】:目录

本系列博文改编自武沛齐老师的原创博文,主要包含  Python基础.前端开发.Web框架.缓存以及队列等内容 ,用于学习记录成长!!! Python开发[第一篇]:目录 Python开发[第二篇]:初识Python

Eclipse插件开发 学习笔记 PDF 第一篇到第四篇 免分下载 开发基础 核心技术 高级进阶 综合实例

<<Eclipse插件开发 学习笔记>>,本书由浅入深.有重点.有针对性地介绍了Eclipse插件开发技术,全书分为4篇共24章.第一篇介绍Eclipse平台界面开发的基础知识.包含SWT控件的使用.界面布局.事件处理等内容:第二篇是插件开发核心技术,主要介绍插件开发的核心知识要点,包含行为(Action).视图(ViewPart).编辑器(Editor).透视图(Perspective)等10章的内容.第三篇主要讲述插件开发的高级内容,包含开发高级内容.富client平台技术(R

U-BOOT-2016.07移植 (第一篇) 初步分析

U-BOOT-2016.07移植 (第一篇) 初步分析 目录 U-BOOT-201607移植 第一篇 初步分析 目录 编译和移植环境 更新交叉编译工具 1 下载arm-linux-gcc 443 2 安装arm-linux-gcc 443 安装环境Ubuntu 910 下载u-boot-201607并解压 分析顶层Makefile 1 找出目标依赖关系 2 总结 初次编译u-boot 1 配置 2 编译 分析u-boot启动流程 1 分析startS 2 分析crt0S 3 总结 1. 编译和移

我的第一篇博文:输入法编程

这是我的第一篇博文,在这之前,我要介绍一下我自己.我是一名核电工程师,已经工作了19年,我同时也是一名计算机爱好者.我利用业余时间学习编程. 最近我研究了输入法的编程.我阅读了很多园里的文章,特别是借鉴了启程之星公开的客户端的源码.研究已经取得进展,可惜眼睛看坏了,现在不是很方便用计算机,所以等我眼睛好一些时候,我会更详细地介绍我研究的输入法的心得.我做的几件事如下: 1. 启程之星输入法源码的主要原理搞懂了.编译成功,知道主要部分的逻辑关系: 2. 研究了启程之星最近版与服务器通讯的原理(此部