1.
read_contour_xld_dxf (Contours, DXFFliePath, [], [], DxfStatus)
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale (HomMat2DIdentity, Scale, Scale, 0, 0, HomMat2DScale)
*将任意仿射2D变换,即缩放、旋转、平移和倾斜(倾斜)应用于轮廓中给定的xld轮廓
*并以ContoursAffineTrans的形式返回变换后的轮廓
affine_trans_contour_xld (Contours, ContoursAffinTrans, HomMat2DScale)
copy_obj (ContoursAffinTrans, ImageCleared, 1, -1)
*主要应用于模板在X和Y方向有相同的形变的场合
create_scaled_shape_model_xld (ImageCleared, ‘auto’, 0, 360, ‘auto’, 0.9, 1.1, ‘auto’, [‘none’,‘no_pregeneration’], ‘ignore_local_polarity’, 5, ModelID)
find_scaled_shape_model (Image, ModelID, 0, 360, 0.9, 1.1, 0.8, 0, 0.5, ‘least_squares’, 0, 0.9, Row, Column, Angle, Scale, Score)
for MatchingObjIdx := 0 to |Score| - 1 by 1
hom_mat2d_identity (HomMat2)
hom_mat2d_scale (HomMat2, Scale[MatchingObjIdx], Scale[MatchingObjIdx], 0, 0, HomMat2)
hom_mat2d_rotate (HomMat2, Angle[MatchingObjIdx], 0, 0, HomMat2)
hom_mat2d_translate (HomMat2, Row[MatchingObjIdx], Column[MatchingObjIdx], HomMat2)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2)
dev_display (TransContours)
endfor
return ()
2
**1、**Halcon读取dxf文件,得到的是dxf文件的机械坐标数值,举例说明:
*Create locally deformable model
read_contour_xld_dxf (DxfContours, ‘C:/Users/firecat/neiyi1.dxf’, [], [], DxfStatus)
*合并邻近的XLD,使得细小线段拼接起来
union_adjacent_contours_xld (DxfContours, UnionContours, 10, 1, ‘attr_keep’)
count_obj(UnionContours, NumberContours)
for i := 1 to NumberContours by 1
select_obj (UnionContours, ObjectSelected, i)
length_xld (ObjectSelected, Length)
get_contour_xld (ObjectSelected, row, col)
endfor
create_local_deformable_model_xld (UnionContours, ‘auto’, rad(-10), rad(20), ‘auto’, 1, 1, ‘auto’,
1, 1, ‘auto’, ‘auto’, ‘ignore_local_polarity’, 5, [], [], ModelID)
get_deformable_model_contours (ModelContours, ModelID, 1)
**2、**如何实现xld的缩放?通过缩放XLD中每个点的坐标值来实现。xld需要先缩放,后后拼接!
read_contour_xld_dxf (DxfContours, ‘C:/Users/firecat/neiyi1.dxf’, [], [], DxfStatus)
Scale := 4
count_obj(DxfContours, NumberContours)
gen_empty_obj (ScaleContours)
for i := 1 to NumberContours by 1
select_obj (DxfContours, ObjectSelected, i)
get_contour_xld (ObjectSelected, row, col)
Row1 := []
Col1 := []
for j := 0 to |row|-1 by 1
Row1:=[Row1, row[j] * Scale]
Col1:=[Col1, col[j] * Scale]
endfor
*判断轮廓是不是闭合,如果是闭合的,那么使最后一个点与第一个点重合(即让缩放后的XLD也闭合)
test_closed_xld (ObjectSelected, IsClosed)
if (IsClosed == 1)
Row1:=[Row1, row[0] * Scale]
Col1:=[Col1, col[0] * Scale]
endif
gen_contour_polygon_xld (Contour1, Row1, Col1)
smooth_contours_xld (Contour1, SmoothedContours, 5)
concat_obj (ScaleContours, SmoothedContours, ScaleContours)
endfor
*xld拼接
union_adjacent_contours_xld (ScaleContours, UnionContours, 10, 1, ‘attr_keep’)
其中:
get_contour_xld (Contour, Row, Col)是得到XLD中的一系列点;
gen_contour_polygon_xld (Contour1, Row1, Col1)是通过一系列点重建XLD。
3、镜像(因为DXF坐标系和halcon坐标系存在上下翻转的对应关系)
*使用仿射变换实现镜像,这里是上下翻转
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_reflect (HomMat2DIdentity, 0, 0, 0, 32, HomMat2DReflect)
affine_trans_contour_xld (UnionContours, ContoursAffinTrans, HomMat2DReflect)
hom_mat2d_reflect算子的参数说明:
Px (input_control) point.x → (real / integer)
First point of the axis (x coordinate).
Default value: 0
Suggested values: 0, 16, 32, 64, 128, 256, 512, 1024
Py (input_control) point.y → (real / integer)
First point of the axis (y coordinate).
Default value: 0
Suggested values: 0, 16, 32, 64, 128, 256, 512, 1024
Qx (input_control) point.x → (real / integer)
Second point of the axis (x coordinate).
Default value: 16
Suggested values: 0, 16, 32, 64, 128, 256, 512, 1024
Qy (input_control) point.y → (real / integer)
Second point of the axis (y coordinate).
Default value: 32
Suggested values: 0, 16, 32, 64, 128, 256, 512, 1024
OK实战
read_contour_xld_dxf (DxfContours, ‘C:/Users/firecat/Downloads/match/neiyi/M-OK180503.dxf’, [], [], DxfStatus)
*合并邻近的XLD,使得细小线段拼接起来
union_adjacent_contours_xld (DxfContours, UnionContours, 5, 1, ‘attr_keep’)
*找出面积最大的轮廓,方法1
count_obj(UnionContours, NumberContours)
maxArea := 0
index := 0
for i := 1 to NumberContours by 1
select_obj (UnionContours, ObjectSelected, i)
length_xld (ObjectSelected, Length)
area_center_xld (ObjectSelected, Area, Row1, Column1, PointOrder)
if (Area > maxArea)
maxArea := Area
index := i
endif
get_contour_xld (ObjectSelected, row, col)
endfor
select_obj (UnionContours, MaxAreaSelected, index)
get_contour_xld (MaxAreaSelected, Row2, Column2)
stop()
*找出面积最大的轮廓,方法2
area_center_xld (UnionContours, Area, Row, Column, PointOrder)
*先将Area中各元素按升序排序,然后将排序后的每一个Area分别在原Area元组中的索引放在元组Indices 中
tuple_sort_index (Area, Indices)
Num := |Indices|
*tuple的下标从0开始计数;Obj的下标从1开始计数;
index := Indices[Num-1] + 1
select_obj (UnionContours, MaxAreaSelected, Indices[Num-1] + 1)
get_contour_xld (MaxAreaSelected, Row1, Col1)
stop()
*求出外接矩形
shape_trans_xld(MaxAreaSelected, XLDTrans, ‘rectangle1’)
area_center_xld (XLDTrans, Area, Row2, Column2, PointOrder)
smallest_rectangle1_xld (MaxAreaSelected, Row11, Column11, Row21, Column21)
width := Column21 - Column11 + 1
height := Row21 - Row11 + 1
*新建图片,图片左上角是零点,准备用来存放最大面积
gen_image_const (NewImage, ‘byte’, width, height)
area_center (NewImage, Area, Row1, Column1)
*Create an image with a specified constant gray value
gen_image_proto (NewImage, ImageCleared, 255)
*仿射变换,让外接矩形的最大面积和图片重合
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row1 - Row2, Column1 - Column2, HomMat2DTranslate)
affine_trans_contour_xld (MaxAreaSelected, ContoursAffineTrans, HomMat2DTranslate)
*把最大轮廓区域裁剪出来
gen_region_contour_xld (ContoursAffineTrans, Region, ‘filled’)
*Paint regions into an image
paint_region (Region, ImageCleared, ImageResult, 0, ‘fill’)
*注意保存路径的斜杆,是/
write_image (ImageResult, ‘jpeg’, 0, ‘D:/test.jpg’)
stop()
感谢下面博主分享
————————————————
版权声明:本文为CSDN博主「libaineu2004」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/libaineu2004/article/details/102821750