大很多场合,需要在视觉程序中导入CAD文档,比如,在3C行业,需要对手机外壳进行CNC加工,或者点胶操作,此时,需要获取产品的各个点的数据。如果将CAD直接导入,就会大的减少编程工作量,同时也能达到很高的精度。

以下为Halcon自带例程:

* This example program shows how to read DXF files and how to
* use this CAD description of objects to generate shape models.
* The program is a slightly modified copy of the example program
* examples/hdevelop/Applications/Object-Recognition-2D/
* pm_multiple_models.hdev.
* The following modifications to this program were made:
* - The model images are created from the DXF description of
* the objects.
* - The parameters of the operator find_shape_models were
* slightly relaxed because the DXF object description does
* not fit exactly to the objects in the search image because
* the images were taken with an uncalibrated and slightly
* oblique viewing camera.
* This program shows how to use HALCON's shape-based
* matching to find multiple different models in one call to
* find_shape_models. Note that this is one mode of operation
* that is frequently useful. However, the number of
* applications that can be solved with this mechanism is much
* larger. Other applications where finding multiple models
* in one call is useful are applications where the same object
* can only occur in small angle ranges around a discrete set
* of angles, e.g., °, °, °, and °. In these cases,
* it would be wasteful to train the model for the full °
* rotation range and to match the model in this range. Instead,
* four models using the small angle ranges around the discrete
* set of angles should be generated from the same model image
* and used in the matching stage using four different angle
* ranges.
*
dev_update_pc ('off')
dev_update_window ('off')
dev_update_var ('off')
dev_close_window ()
dev_open_window (, , , , 'black', WindowHandle)
dev_set_part (, , , )
dev_set_draw ('margin')
set_display_font (WindowHandle, , 'mono', 'true', 'false')
dev_set_line_width ()
* These colors will be used to graphically discern the different
* models in the visualization code below.
Colors := ['red','green','cyan']
* The object Models will hold a set of XLD contours that
* represent the different models. They are used below to overlay
* the found models on the current image. XLD contours are used
* because they can be transformed much faster than regions. This
* creates a slight problem because in general multiple XLD
* contours will represent one model. Therefore, the start and
* end indices of the different models will be stored in IndexS
* and IndexE, respectively.
gen_empty_obj (Models)
IndexS := []
IndexE := []
* The variable ModelIDs holds the different models that are
* created below.
ModelIDs := []
* Likewise, RowsRef and ColumnsRef store the reference points
* of the different models. They are necessary to transform the
* models to the found instances in the current image.
for J := to by
dev_clear_window ()
read_contour_xld_dxf (Contours, 'metal-part-' + J$'', [], [], DxfStatus)//读取DXF文件,并将其转换为DXF轮廓。
gen_model_image_of_bright_object_with_holes (Contours, Image, 3.38, , )
dev_display (Image)
dev_set_color ('green')
set_tposition (WindowHandle, , )
write_string (WindowHandle, 'Generating shape model ' + J$'d')
get_domain (Image, Domain)
area_center (Domain, Area, Row, Column)
* Although we will use get_shape_model_contours below to
* obtain the representation of the model, we extract a model
* representation here using inspect_shape_model because this
* enables us to display the representation of the model while
* the model is being created.
inspect_shape_model (Image, ModelImages, ModelRegions, , )
* Since the shape models contain a few extraneous edges,
* they will be removed here to give a slightly nicer
* visualization.
connection (ModelRegions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', , )
union1 (SelectedRegions, ModelRegions)
gen_contours_skeleton_xld (ModelRegions, ModelContours, , 'filter')
dev_set_color ('red')
dev_display (ModelContours)
create_shape_model (Image, , rad(), rad(), 'auto', 'pregeneration', 'use_polarity', , , ModelID)
get_shape_model_contours (ModelCont, ModelID, )
select_shape_xld (ModelCont, ModelContours, 'contlength', 'and', , )
* Count how many XLD contours there are in the current
* model and in the already stored models. This is necessary
* to compute IndexS and IndexE.
count_obj (ModelContours, NumModel)
count_obj (Models, NumModels)
concat_obj (Models, ModelContours, Models)
IndexS := [IndexS,NumModels + ]
IndexE := [IndexE,NumModels + NumModel]
ModelIDs := [ModelIDs,ModelID]
endfor
disp_message (WindowHandle, ['Press left button to start','and stop the demo'], 'image', , , 'yellow', 'false')
get_mbutton (WindowHandle, Row1, Column1, Button1)
wait_seconds (0.5)
dev_set_color ('red')
Button :=
ImgNo :=
while (Button != )
read_image (Image, 'metal-parts/metal-parts-' + ImgNo$'02d')
count_seconds (S1)
find_shape_models (Image, ModelIDs, rad(), rad(), 0.6, , 0.5, 'least_squares', , 0.3, Row, Column, Angle, Score, Model)
count_seconds (S2)
Time := (S2 - S1) * .
dev_display (Image)
Num := |Score|
for J := to Num - by
* Select the correct XLD contours from the Models object.
copy_obj (Models, ModelSelected, IndexS[Model[J]], IndexE[Model[J]] - IndexS[Model[J]] + )
* Compute the transformation from the model object to
* the current instance.
vector_angle_to_rigid (, , , Row[J], Column[J], Angle[J], HomMat2D)
affine_trans_contour_xld (ModelSelected, ModelTrans, HomMat2D)
dev_set_color (Colors[Model[J]])
dev_display (ModelTrans)
endfor
if (Num == )
disp_message (WindowHandle, Num$'1d' + ' object found in ' + Time$'4.2f' + 'ms', 'image', , , 'yellow', 'false')
else
disp_message (WindowHandle, Num$'1d' + ' objects found in ' + Time$'4.2f' + ' ms', 'image', , , 'yellow', 'false')
endif
ImgNo := ImgNo +
if (ImgNo > )
ImgNo :=
endif
dev_error_var (Error, )
dev_set_check ('~give_error')
get_mposition (WindowHandle, R, C, Button)
dev_error_var (Error, )
dev_set_check ('give_error')
if (Error != H_MSG_TRUE)
Button :=
endif
endwhile
for J := to |ModelIDs| - by
clear_shape_model (ModelIDs[J])
endfor

程序中read_contour_xld_dxf 是读取DXF格式文件的算子,可以将DXF文件转换为XLD轮廓。如果没有提供文件的绝对路径,则会在当前目录下搜索DXF文件。

read_contour_xld_dxf支持如下DXF实体:多段线,2D线段,点,圆,圆弧,椭圆,块,典线。对应DXF实体的X和Y坐标分别存储在列和行的坐标。Z坐标被忽略。

输出参数DxfStatus包含有关DXF文件的部分数据,并转换为轮廓的形式输出。

我们也可以通过write_contour_xld_dxf来将XLD轮廓转换为DXF格式的CAD文件。write_contour_xld_dxf保存DXF格式时可以通过设置参数来确保数据的完整性,设置这些属性的读取可以通过设置通用参数‘read_attributes’为false来关闭,通用参数GenParamNames参数名和在GenParamValues设置的值。

DXF实体中的圆,弧,椭圆和曲线转换过程中为近似的XLD轮廓,近似精度可以通用参数min_num_points和max_approx_error来控制。参数min_num_point定义了被用于近似采样点的最小数量。需要注意的是,参数min_num_points针对完整的圆或椭圆形对应的采集点数,对应于圆弧或椭圆弧,如果min_num_points设置为50,则表示读取一个半圆,此时半圆为至少25个采样点。参数max_approx_error定义的XLD轮廓的理想圆或椭圆,分别的最大偏差(单位:pixel像素)

Halcon自带例程

* This example program shows how to use the operators
* write_contour_xld_dxf and read_contour_xld_dxf as well as
* the operators write_polygons_xld_dxf and read_polygon_xld_dxf
*
* First, edges are extracted from an image.
read_image (Image, 'mreut')
edges_sub_pix (Image, Edges, 'canny', , , )
*
* Then, to create global attributes, the regression lines are determined.
regress_contours_xld (Edges, RegressContours, 'no', )
*
* Now, we can query what attributes and global attributes
* are available for the contours.
select_obj (RegressContours, ObjectSelected, )
query_contour_attribs_xld (ObjectSelected, Attribs)
query_contour_global_attribs_xld (ObjectSelected, GlobalAttribs)
*
* In the next step, the contours are written into a DXF file. This file
* contains also the attributes and global attributes.
write_contour_xld_dxf (RegressContours, 'contours')
*
* When the contours are read from the DXF file created with
* read_contours_xld_dxf, the (global) attributes are read, too.
read_contour_xld_dxf (ContoursRead, 'contours', [], [], DxfStatusCont)
*
* All (global) attributes that were available for the
* original contours (RegressContours) are available also for the
* contours (ContoursRead) that have been read from the DXF file.
select_obj (ContoursRead, ObjectSelected, )
query_contour_attribs_xld (ObjectSelected, AttribsTest)
query_contour_global_attribs_xld (ObjectSelected, GlobalAttribsTest)
*
* Now, we show how to write and read polygons
gen_polygons_xld (ContoursRead, Polygons, 'ramer', )
write_polygon_xld_dxf (Polygons, 'polygons')
read_polygon_xld_dxf (PolygonsRead, 'polygons', [], [], DxfStatusPoly)

Halcon一日一练:CAD类型的相关操作的更多相关文章

  1. Halcon一日一练:读取文件目录图像的三种方法

    第一种方法: 读了一个单一图像: read_image(Image,'fabrik') 这种方式可以快速的读取软件自身携带的库图像文件,系统设定了库图像映像文件的快速读取方式,我们也可以通过绝对地址的 ...

  2. Halcon一日一练:获取程序运行时间

    很多时候,我们需要知道每个函数的运算周期,以提高程序的运行效率.知道运行时间对于图像算法处理很重要 Halcon提供相关的算子,我们先来看代码: **获取图像处理时间 read_image(Image ...

  3. Halcon一日一练:获取图像属性

    从图像属性我们可以了解图像的基本信息,比如大小,高度,指针等. Halcon提供了获取图像属性的算子. 我们来看看下面例子: **获取图像属性 read_image(Image,'Clip') dev ...

  4. Halcon一日一练:创建三通道图像

    首先理解一个什么是三通道图像: 三通道图像就是彩色图像,我们之前黑白相机或黑白电视机都是彩用的灰阶图像,即单通道图像,一般是2的8次方个灰阶,即256个灰阶.彩色图像采用RGB,红绿蓝三个通道来合成彩 ...

  5. Halcon一日一练:图像、变量实时更新

    某些场合,我们需要刷新图像来识别图像处理过程的差异性,便于调试判断问题和预测.Halcon提供了图像刷新操作,这些操作不会改变程序的最终处理结果. 例程: **实时刷新图像 dev_update_wi ...

  6. Halcon一日一练:图像分辨率与像素

    1.图像像素: 像素是指由图像的小方格即所谓的像素(pixel)组成的,这些小方块都有一个明确的位置和被分配的色彩数值,而这些一小方格的颜色和位置就决定该图像所呈现出来的样子.像素是构成图像的基本单元 ...

  7. Halcon一日一练:图像拼接技术2:步骤与例程

    上一篇主要介绍了图像拼接的一些原理和方法,这一篇将主要介绍步骤和例程: 接上一篇: 基于特征的接拼方法,分为四个步骤 1.特征检测:从图像中检测出显著且独特的图像特征,诸如:闭合区域,直线段,边缘,轮 ...

  8. Halcon一日一练:图像采集设备的基本参数

    因操作图像处理之前,需要对图像进行采集.采集图像,我们首先要确定的是图像的像素和采集的效率.这些都需要对设备进行配置与操作.现实情况是图像设备有各自不同的采集方式,配置也各不相同.这就需要设备提供商提 ...

  9. Halcon一日一练:图像设备介绍

    Halcon在设计之初就提供了完整的图像采集方案,适应了多种图像设备采集图像,以及各种不同环境的采集方案. 通常情况下,图像的采集应该是所有机器视觉项目首要解决的任务,不幸的是,需要解决图像采集的问题 ...

随机推荐

  1. 我的第一个python web开发框架(21)——小结

    这个小网站终于成功上线,小白除了收获一笔不多的费用外,还得到女神小美的赞赏,心中满满的成就感.这一天下班后,他请老菜一起下馆子,兑现请吃饭的承诺,顺便让老菜点评一下. 小白:老大,在你的指导下终于完成 ...

  2. 学而精计算机公共基础学习之路TEST1

    算法 一:算法基本概念 算法是个什么概念学了这么久的程序尽然没有听说过,其实算法就是为了解决问题那么怎么准确完整的解决这个问题就是算法.所以我们所写的程序就可以说为对算法的描述,但是程序编制是不能有于 ...

  3. nginx服务器配置/websocket nginx 配置笔记

    server { listen 80; server_name xxx.com; # add_header '*' ; location /u/ { # 反向代理透传客户端ip proxy_set_h ...

  4. 基于VUE的九宫格抽奖功能

    HTML代码: <template> <div class="luckDraw"> <title-bar :title="title&quo ...

  5. MyBatis之基于XML的动态SQL

    先说下我的梦想,大学的时候一直想着是能开店卖胡辣汤,到目前依然还是我的梦想,上周一家出版社联系我问我有没有时间可以合作出书,这也是我的梦想之一,想了想还是放弃了,至少觉得目前不行,毕竟工作还不到五年, ...

  6. ios - 如何获取app上的数据

    做过ios开发的人应该都用过Charles,通常叫它花瓶.Charles是Mac下常用的对网络流量进行分析的工具,类似于Windows下的Fiddler.在开发iOS程序的时候,往往需要调试客户端和服 ...

  7. 【开发技术】java中代码检查checkStyle结果分析

    编写Javadoc代码在Java代码的类.函数.数据成员前中输入/**回车,Eclipse能够自动生成相应的Javadoc代码.可以在后面添加相关的文字说明. Type is missing a ja ...

  8. J.U.C JMM. pipeline.指令重排序,happen-before(续)

    前面已经介绍硬件平台Cache Coherence问题和解决办法,下面来看看Java虚拟机平台的相关知识.硬件平台处理器,高速缓存,主存之间的交互关系如下: Java内存模型(JMM)         ...

  9. 前端开发人员需要了解的CSS原理

    转自http://web.jobbole.com/10011/ 一.浏览器的发展与CSS 网页浏览器主要通过HTTP协议连接网页服务器而取得网页,HTTP容许网页浏览器送交资料到网页服务器并且获取网页 ...

  10. aliyun 购买的linux安装tomcat

    按照网上的教程,下载tomcat,解压(即安装),启动,发现无法访问.有说端口未开放,修改/etc/sysconfig/iptables,添加端口开放.未发现有此文件,只有iptables-confg ...