⭐️Halcon常用api背诵版

⭐️Halcon常用api背诵版

时间计算

count_seconds(s1)
count_seconds(s2)
Time := (s2-s1) * 1000 //Time单位是ms

循环控制

for i := 0 to num -1 by 1 
endfor

while()
endwhile


if()
endif

控制变量赋值

colors := ['red', 'yellow']
row1 := []
row2 := [323,2,3]
column1 := []
column2 := [32,23]
IndexS := []
IndexE := []
ModelIDs := []
singleNums := 12312
//追加元素
IndexS := [IndexS,12]
//求数组长度
num := |xjh| 这么做是错误的 
||只能当作参数来用,比如for i:=0 to |xjh|-1 by 1 或者raw:=[raw,12,|raw|]

图形变量

//图形变量数组
gen_empty_obj (Models)
//追加元素,concat是合并的意思
concat_obj(Objects1, Objects2 : ObjectsConcat : : )
concat_obj(Models, xjh ,Models)
//在Models追加xjh
gen_empty_obj (EmptyObject)
read_image(xjh, 'printer_chip/printer_chip_01')
concat_obj(EmptyObject,xjh,EmptyObject)
read_image (xjh1,'printer_chip/printer_chip_02')
concat_obj (xjh,xjh1,xjh)
//计算个数
count_obj()

窗口基本操作

每次新建文件时,最好写出如下操作,先设置好:

  1. 显示字体
    1. 填充方式
    2. 填充颜色
    3. 线型
read_image()
write_image()
dev_close_window()
dev_open_window()
dev_open_window_fit_image()

get_image_size(Image : : : Width, Height) //获取图像宽高 
dev_resize_window_fit_size()

set_window_param()
dev_clear_window()

dev_set_part() //不修改图片分辨率,修改想要显示的部分 

dev_update_window()
dev_update_off()

set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_update_off ()
dev_set_draw('margin')
dev_set_color('red')
dev_set_line_width(1)

画笔颜色,字体等

基本设置

dev_set_draw(‘margin') //绘画目标是区域还是轮廓等
dev_display()
dev_set_color() //绘制颜色
dev_set_line_width()

set_display_font()
disp_message()
disp_continue_message()

write_string() //写字符道窗口

绘制

set_display_font()
disp_message()
disp_continue_message()

write_string() //写字符道窗口

//在窗口中制定位置写字符串,
set_tposition(WindowHandle,20,20)
write_string(WindowHandle,"My string in the WindowHandle" + J$'d')

绘制图形,抠图

gen_circle()
gen_rectangle1()
gen_rectangle2()
area_center()
//抠图
reduce_domain(Image, Region : ImageReduced : : )
gen_image_const() // 空图片
unino1()
union2()

阈值/区域/形态学/灰度化/色彩空间转换

threshold()
connection()
select_shape()

fill_up()

dilation_circle()
rgb1_to_gray(Image,grayImage)
decompose(image,r,g,b)
trans_from_rgb(image1,image2,image3,h,s,v,'hsv')

提取区域转换轮廓

 //自动设置shape模版图像和自动分割区域,且创建区域金字塔
inspect_shape_model (Image, ModelImages, ModelRegions, 1, 30)
//合并相邻区域,独立不同区域
connection (ModelRegions, ConnectedRegions)
//选择目标区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 20, 100000)
//联合需要区域
union1 (SelectedRegions, ModelRegions)
//区域骨架转换轮廓
gen_contours_skeleton_xld (ModelRegions, ModelContours, 1, 'filter')

xld轮廓相关

fit_line_contour_xld()
gen_contour_polygon_xld()

gen_contour_polygon_rounded_xld()//亚像素轮廓
paint_xld()

//读取轮廓
// 参数(输出读取的contours,文件路径,输入dxf文件控制参数名,控制参数值,Dxf文件被读取的状态信息)
read_contour_xld_dxf (Contours, 'metal-part-' + J$'02', [], [], DxfStatus)

数学运算

// 计算标准差,就是看看离散程度,标准差=sqrt(方差)
StdDevRows := deviation(Rows)
StdDevCols := deviation(Cols)
// 弧度
AngleStart := -rad(44)
AngleEnd := -rad(144)

模版匹配

ncc

// ncc模版匹配

/create_ncc_model(Template,输入单通道模版图片
			NumLevels,金字塔层数
			AngleStart, 模版最小旋转角度
			AngleExtent, 模版最大旋转角度
			AngleStep, 模版角度步长
			Metric,匹配算法
			ModelID输出模版模型句柄)
/

/find_ncc_model(Image,输入图片
			ModelID, 模版模型句柄
			AngleStart, 匹配旋转其实角度
			AngleExtent, 终止角度
			MinScore, 匹配得分最小值
			NumMatches, 匹配个数0全要
			MaxOverlap, 最大重叠率0-1
			SubPixel, 是否采用亚像素
			NumLevels,使用模版模型中第几层金字塔
			Row, 输出匹配结果的行列坐标角度和得分
			Column, Angle, Score)
/
create_ncc_model(Template : : NumLevels, AngleStart, AngleExtent, AngleStep, Metric : ModelID)

find_ncc_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels : Row, Column, Angle, Score)
clear_ncc_model()
dev_display_ncc_matching_results()

shape

⭐️Halcon常用api背诵版_第1张图片

//主要的算子
//不带缩放的模版
create_shape_model()
/create_scaled_shape_model(Template模版图片,
					NumbLevels模板金字塔层数,=5
					AngleStart模板最小旋转角度,=0
					AngleExtent模版旋转范围,=360
					AngleStep旋转步长,'auto'
					ScaleMin最小缩放比例,=0.8
					ScaleMax最大缩放比例,=1.1
					ScaleStep缩放步长,'auto'
					Optimization优化方法,='none'
					Metric匹配标准,='ignore_global_polarity'
					Contrast对比度,=40
					MinContrast最小对比度,=10
					ModelID生成的形状模版模型句柄)
对比度小一点可能比较容易找到,但是容易带来干扰
这个算子会直接把模型坐标放到0,0角度调整到0
/
create_scaled_shape_model (ImageReduced, 5, rad(-45), rad(90), 'auto', 0.8, 1.0, 'auto', 'none', 'ignore_global_polarity', 40, 10, ModelID)
create_scaled_shape_model()
get_shape_model_contours() //所有的contours的位置都在0,0和angle:=0处
find_shape_model()


//带缩放的算子
/find_scaled_shape_model(Image输入图片, 
					ModelID模型句柄, 
					AngleStart旋转初始角度, 
					AngleExtent角度范围, 
					ScaleMin最小缩放比例, 
					ScaleMax最大缩放比例, 
					MinScore匹配最小得分, 
					NumMatches匹配个数, 
					MaxOverlap重叠率, 
					SubPixel亚像素级精度, 
					NumLevels模版金字塔第几层, 
					Greediness贪心度,
					Row, Column, Angle, Scale, Score匹配结果的行列坐标,缩放比例和得分值)
/
find_scaled_shape_model()
clear_shape_model()

create_aniso_shape_model() // 创建不等比例的模型匹配
find_aniso_shape_model()
clear_shape_model()

//显示模版图片,和各个区域金字塔,层数按NumLevels指定
//相当于自动求出了各个区域
inspect_shape_model(Image : ModelImages, ModelRegions : NumLevels, Contrast : )
//示例
inspect_shape_model(ImageReduced, ModelImages, ModelRegions, 4, 30)

// 标准套路
dev_update_pc('off')
dev_update_window('off')
dev_update_var('off')
read_image(Image, 'green-dot')
get_image_size(Image, Width, Height)
dev_close_window()
dev_open_window(0, 0, Width, Height, 'black', WindowHandle)
dev_display(Image)
dev_set_color('red')
dev_set_draw('margin')
dev_set_line_width (1)
dev_set_colored(12)
threshold (Image, Region, 0,128)
connection(Region, ConnectedRegions)
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 13000, 16000)
fill_up(SelectedRegions, RegionFillUp)
dilation_circle(RegionFillUp, RegionDilation, 3.5)
reduce_domain(Image, RegionDilation, ImageReduced)
//create shape-contours model
/create_scaled_shape_model (ImageReduced, 'auto', rad(0), rad(360), 'auto', 0.9, 1.1, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
/
//注释掉的只能检测出完全的目标,对于只有半个模版形状的目标,无法检测
create_scaled_shape_model (ImageReduced, 5, rad(-45), rad(90), 'auto', 0.8, 1.0, 'auto', 'none', 'ignore_global_polarity', 40, 10, ModelID)
//获取轮廓的目的是为了显示匹配结果
get_shape_model_contours (Model, ModelID, 1) 
read_image (Image1, 'green-dots')
find_scaled_shape_model (Image1, ModelID, rad(0), rad(360), 0.8, 1.0, 0.5, 0, 0.5, 'least_squares', 5, 0.8, Row, Column, Angle, Scale, Score)
for J:=0 to  |Score|-1 by 1
    vector_angle_to_rigid (0,0,0,Row[J], Column[J], Angle[J], HomMat2D)
    affine_trans_contour_xld (Model, ContoursAffineTrans, HomMat2D)
    dev_display (Image1)
    dev_display (ContoursAffineTrans)
endfor

保存,读取,获得参数

//读取
raed_shape_model()
read_shape_model (ModelFile, ReusedModelID)
//获得参数
get_shape_model_contours()
get_shape_model_origin()
get_shape_model_params()
	//示例
get_shape_model_contours (ReusedShapeModel, ReusedModelID, 1)
get_shape_model_origin (ReusedModelID, ReusedRefPointRow, ReusedRefPointCol)
get_shape_model_params (ReusedModelID, NumLevels, AngleStart, AngleExtent, AngleStep, ScaleMin, ScaleMax, ScaleStep, Metric, MinContrast)
//保存
write_shape_model()
write_shape_model (ModelID, 'green-dot.shm')

多对多模版匹配

dev_open_window (0, 0, 646, 482, 'black', WindowHandle)
dev_set_draw ('margin')

Colors := ['red','green','cyan']

Row1 := [135,150,185]
Column1 := [250,170,220]
Row2 := [375,310,335]
Column2 := [355,395,375]


gen_empty_obj (Models)
IndexS := []
IndexE := []
ModelIDs := []

for J := 1 to 3 by 1
    read_image (Image, 'metal-parts/metal-part-model-' + J$'02d')
    gen_rectangle1 (Rectangle, Row1[J - 1], Column1[J - 1], Row2[J - 1], Column2[J - 1])
    area_center (Rectangle, Area, Row, Column)
    reduce_domain (Image, Rectangle, ImageReduced)
    inspect_shape_model (Image, ModelImages, ModelRegions, 1, 30)
    connection (ModelRegions, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 20, 100000)
    union1 (SelectedRegions, ModelRegions)
    gen_contours_skeleton_xld (ModelRegions, ModelContours, 1, 'filter')
    create_shape_model (ImageReduced, 5, rad(0), rad(360), 'auto', 'pregeneration', 'use_polarity', 30, 7, ModelID)
    get_shape_model_contours (ModelCont, ModelID, 1)
    select_shape_xld (ModelCont, ModelContours, 'contlength', 'and', 20, 1000)

    count_obj (ModelContours, NumModel)
    count_obj (Models, NumModels)
    concat_obj (Models, ModelContours, Models)
    IndexS := [IndexS,NumModels + 1]
    IndexE := [IndexE,NumModels + NumModel]
    ModelIDs := [ModelIDs,ModelID]
endfor


Button := 0
ImgNo := 1
while (Button != 1)
    read_image (Image, 'metal-parts/metal-parts-' + ImgNo$'02d')
    //返回的Row和Column是个数组,完全取决于匹配了几个目标
    find_shape_models (Image, ModelIDs, rad(0), rad(360), 0.5, 0, 0.5, 'least_squares', 0, 0.8, Row, Column, Angle, Score, Model)
    
    //根据匹配的目标个数来进行仿射变换用来显示匹配结果
    Num := |Score|
    for J := 0 to Num - 1 by 1
        copy_obj (Models, ModelSelected, IndexS[Model[J]], IndexE[Model[J]] - IndexS[Model[J]] + 1)
        vector_angle_to_rigid (0, 0, 0, Row[J], Column[J], Angle[J], HomMat2D)
        affine_trans_contour_xld (ModelSelected, ModelTrans, HomMat2D)
        dev_set_color (Colors[Model[J]])
        dev_display (ModelTrans)
    endfor

    ImgNo := ImgNo + 1
    if (ImgNo > 15)
        ImgNo := 1
    endif

endwhile
for J := 0 to |ModelIDs| - 1 by 1
    clear_shape_model (ModelIDs[J])
endfor

仿射affine变换

通过点角度到点角度

//从点和角度计算仿射变换
/vector_angle_to_rigid(Row1, Column1, Angle1, Row2, Column2, Angle2 : HomMat2D)
/
vector_angle_to_rigid() //获得放射矩阵
//将2维放射矩阵作用到区域中
affine_trans_region(Region : RegionAffineTrans : HomMat2D, Interpolate : )

通过添加向量方法

直接input想要映射到的位置、角度、缩放量,会自动把对应向量添加到放射矩阵中

// 平移,旋转,缩放
// 生成一个齐次变换矩阵
    hom_mat2d_identity (HomMat2DIdentity)
// 在矩阵中增加平移量
    hom_mat2d_translate (HomMat2DIdentity, Row[I], Column[I], HomMat2DTranslate)
// 在矩阵中增加角度量
    hom_mat2d_rotate (HomMat2DTranslate, Angle[I], Row[I], Column[I], HomMat2DRotate)
// 在矩阵中增加缩放量
    hom_mat2d_scale (HomMat2DRotate, Scale[I], Scale[I], Row[I], Column[I], HomMat2DScale)
//作用到轮廓框架中
affine_trans_contours_xld()

测量拟合

// 基于边缘对
gen_rectangle2()
gen_measure_reactangle2()
measure_pairs()
close_measure()
// 基于非边缘对
gen_rectagnle2()
gen_measure_ractangle2()
measure_pos()
close_measure()
// 弧形测量
get_points_ellipse()
gen_measure_arc()
measure_pos()
close_measure()
// 两点距离
distance_pp()

 

 

 

你可能感兴趣的:(Halcon)