Halcon读取dxf文件生成xld,然后实现点坐标遍历/缩放/镜像/求最大面积等操作(★firecat推荐★)

简介: Halcon读取dxf文件生成xld,然后实现点坐标遍历/缩放/镜像/求最大面积等操作(★firecat推荐★)

Halcon中的坐标系的原点在左上角,而一般二维平面坐标系的原点在左下角。


请注意,使用read_polygon_xld_dxf读取DXF文件所产生的几何信息与使用read_contour_xld_dxf读取文件完全相同。 但是,数据结构不同。


draw_xld_mod — Interactive modification of a contour.


read_contour_xld_dxf (Contours, 'AAA.dxf', 'min_num_points', 100, DxfStatus)
hom_mat2d_identity (HomMat2DIdentity)
get_image_size (Image, Width, Height)
hom_mat2d_scale (HomMat2DIdentity, 0.06, 0.06, Height/2.0, Width/2.0, HomMat2DScale)
affine_trans_contour_xld(Contours, ContoursAffinTrans1, HomMat2DScale)

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


4、更详细的实践

read_contour_xld_dxf (DxfContours, 'C:/Users/firecat/Downloads/1.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()
*把最大轮廓裁剪出来,生成图片
gen_region_contour_xld (MaxAreaSelected, Region, 'filled')
gen_image_const (NewImage, 'byte', 1000, 1000)
*Create an image with a specified constant gray value
gen_image_proto (NewImage, ImageCleared1, 255)
*Paint regions into an image
paint_region (Region, ImageCleared1, ImageResult, 0, 'fill')
write_image (ImageResult, 'jpeg', 0, 'D:/1.jpg')



再优化升级一稿:


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()


相关文章
|
计算机视觉
我的Qt作品(6)使用Qt完整解析dxf文件并绘制(支持椭圆和样条曲线)
我的Qt作品(6)使用Qt完整解析dxf文件并绘制(支持椭圆和样条曲线)
1113 0
我的Qt作品(6)使用Qt完整解析dxf文件并绘制(支持椭圆和样条曲线)
|
13天前
|
存储 计算机视觉
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
本文介绍了使用OpenCV进行图像读取、显示和存储的基本操作,以及如何绘制直线、圆形、矩形和文本等几何图形的方法。
Opencv的基本操作(一)图像的读取显示存储及几何图形的绘制
|
5月前
|
存储 计算机视觉 索引
【OpenCV】-查找并绘制轮廓
【OpenCV】-查找并绘制轮廓
ENVI Classic:如何加载栅格数据(Img/DEM)和矢量数据(evf of ROI)?
ENVI Classic:如何加载栅格数据(Img/DEM)和矢量数据(evf of ROI)?
567 0
ENVI Classic: 如何进行波段合成、矢量栅格叠加显示、窗口链接、图像格式转换、头文件编辑、重采样等(详细)2
ENVI Classic: 如何进行波段合成、矢量栅格叠加显示、窗口链接、图像格式转换、头文件编辑、重采样等(详细)
393 0
|
5月前
|
编解码 数据可视化 定位技术
Python中gdal读取多波段HDF栅格遥感影像数据图层文件并依据像素绘制直方图
Python中gdal读取多波段HDF栅格遥感影像数据图层文件并依据像素绘制直方图
122 1
|
5月前
|
存储 计算机视觉 索引
OpenCV中确定像素位置及获取、修改像素BGR值讲解及演示(Python实现 附源码)
OpenCV中确定像素位置及获取、修改像素BGR值讲解及演示(Python实现 附源码)
172 0
|
5月前
[Qt5] 矩形、圆和多边形ROI区域的交互(List View列表视图,halcon实现)
[Qt5] 矩形、圆和多边形ROI区域的交互(List View列表视图,halcon实现)
132 0
|
C语言
旋转的沙漏-[ Qt绘制旋转图像]
旋转的沙漏-[ Qt绘制旋转图像]
141 0
|
传感器 编解码
ENVI Classic: 如何进行波段合成、矢量栅格叠加显示、窗口链接、图像格式转换、头文件编辑、重采样等(详细)3
ENVI Classic: 如何进行波段合成、矢量栅格叠加显示、窗口链接、图像格式转换、头文件编辑、重采样等(详细)
278 0