HALCON示例程序measure_metal_part_id.hdev使用xld边缘拟合检测零件加工是否合格

HALCON示例程序measure_metal_part_id.hdev使用xld边缘拟合检测零件加工是否合格

示例程序源码(加注释)

  • 关于显示类函数解释
    dev_update_off ()
    Imagefiles := [‘metal-parts/metal-part-model-01’,‘metal-parts/metal-parts-01’,‘metal-parts/metal-part-distorted-01’,‘metal-parts/metal-part-distorted-02’,‘metal-parts/metal-part-distorted-03’]
    for k := 0 to |Imagefiles| - 1 by 1
    read_image (Image, Imagefiles[k])
    get_image_size (Image, Width, Height)
    dev_close_window ()
    dev_open_window (0, 0, Width, Height, ‘light gray’, WindowID)
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_set_line_width (2)
    dev_set_draw (‘margin’)
    dev_display (Image)
    set_display_font (WindowID, 16, ‘mono’, ‘true’, ‘false’)
    disp_message (WindowID, ‘Image’ + k, ‘window’, 10, 10, ‘white’, ‘false’)
    stop ()
    dev_set_draw (‘fill’)

    • 二值化、膨胀,减少定义域、求取边界
      threshold (Image, Region, 90, 255)
      dilation_rectangle1 (Region, RegionDilation, 10, 10)
      reduce_domain (Image, RegionDilation, ImageReduced)
    • 提取水平阈值的交叉点
      threshold_sub_pix (ImageReduced, Edges, 75)
    • 转正检测产品
      orientation_region (Region, OrientationRegion)
      area_center (Region, Area, RowCenter, ColumnCenter)
    • 创建一个以旋转中心与旋转角度为基础的旋转变换矩阵
      vector_angle_to_rigid (RowCenter, ColumnCenter, OrientationRegion, RowCenter, ColumnCenter, 0, HomMat2DRotate)
    • 对XLD轮廓应用任意仿射2D变换。
      affine_trans_contour_xld (Edges, ContoursAffinTrans, HomMat2DRotate)
    • 分割xld边缘轮廓
      segment_contours_xld (ContoursAffinTrans, ContoursSplit, ‘lines_circles’, 6, 4, 4)
    • 对XLD边缘轮廓进行排序
      sort_contours_xld (ContoursSplit, SortedContours, ‘upper_left’, ‘true’, ‘column’)
      dev_clear_window ()
      disp_message (WindowID, ‘Circles in a local coordinate system’, ‘window’, 10, 10, ‘white’, ‘false’)
      dev_set_color (‘black’)
      dev_display (SortedContours)
      dev_set_color (‘white’)
      count_obj (SortedContours, NumberSegments)
      RowsE := []
      ColsE := []
      RadiiE := []
      gen_empty_obj (Lines)
    for i := 1 to NumberSegments by 1select_obj (SortedContours, ObjectSelected, i)* 获取xld边缘轮廓属性get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)if (Attrib == 1)* 拟合圆形fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)* 绘制圆形gen_circle_contour_xld (ContCircle, Row, Column, Radius, StartPhi, EndPhi, PointOrder, 1.5)RowsE := [RowsE,Row]ColsE := [ColsE,Column]RadiiE := [RadiiE,Radius]dev_display (ContCircle)else* 拟合直线fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)* 绘制边缘gen_contour_polygon_xld (Line, [RowBegin,RowEnd], [ColBegin,ColEnd])concat_obj (Lines, Line, Lines)endif
    endfor
    

    dev_set_color (‘white’)
    count_obj (Lines, NumberLines)

    • 根据线的方向筛选xld轮廓
      select_contours_xld (Lines, LinesVertical, ‘direction’, rad(88), rad(92), 0, 0)
      count_obj (LinesVertical, NumberLV)
    • 根据线的方向筛选xld轮廓
      select_contours_xld (Lines, LinesHorizontal, ‘direction’, rad(-2), rad(2), 0, 0)
      count_obj (LinesHorizontal, NumberLH)
      ColVmin := 0
      RowHmax := 0
      for i := 1 to NumberLV by 1
      select_obj (LinesVertical, SelectedV, i)
      get_contour_xld (SelectedV, RowV, ColV)
      if (i == 1)
      ColVmin := ColV[0]
      RowA1 := RowV[0]
      ColA1 := ColV[0]
      RowA2 := RowV[1]
      ColA2 := ColV[1]
      else
      if (ColV[0] < ColVmin)
      ColVmin := ColV[0]
      RowA1 := RowV[0]
      ColA1 := ColV[0]
      RowA2 := RowV[1]
      ColA2 := ColV[1]
      endif
      endif
      endfor
      for j := 1 to NumberLH by 1
      select_obj (LinesHorizontal, SelectedH, j)
      get_contour_xld (SelectedH, RowH, ColH)
      if (RowH[0] > RowHmax)
      RowHmax := RowH[0]
      RowB1 := RowH[0]
      ColB1 := ColH[0]
      RowB2 := RowH[1]
      ColB2 := ColH[1]
      endif
      endfor
    • 求两直线交点
      intersection_lines (RowA1, ColA1, RowA2, ColA2, RowB1, ColB1, RowB2, ColB2, RowO, ColO, IsOverlapping)
      gen_cross_contour_xld (Cross, RowO, ColO, 10, 0.785398)
      dev_display (Cross)
      disp_arrow (WindowID, RowO, ColO, RowO - 100, ColO, 2)
      disp_arrow (WindowID, RowO, ColO, RowO, ColO + 100, 2)
    • 仿射变换的齐次矩阵
      hom_mat2d_identity (HomMat2DIdentityResults)
    • 添加旋转
      hom_mat2d_slant (HomMat2DIdentityResults, rad(180), ‘x’, 0, 0, HomMat2DSlantResults)
    • 添加平移
      hom_mat2d_translate (HomMat2DSlantResults, RowO, -ColO, HomMat2DTranslateResults)
    • 进行像素的仿射变换
      affine_trans_pixel (HomMat2DTranslateResults, RowsE, ColsE, RowsELocal, ColsELocal)
      if (k == 0)
      RowsELocalRef := RowsELocal
      ColsELocalRef := ColsELocal
      RadiiERef := RadiiE
      NumberRowsERef := |RowsELocal|
      for i := 0 to |RadiiE| - 1 by 1
      disp_message (WindowID, i + 1, ‘window’, RowsE[i] + 10, ColsE[i] + RadiiE[i], ‘white’, ‘false’)
      endfor
      disp_message (WindowID, ‘Reference Object’, ‘window’, 50, 10, ‘white’, ‘false’)

    else
    ID_Deviation := []
    ID_Missing := []
    NumberRowsE := |RowsE|
    if (NumberRowsE == NumberRowsERef)
    distance_pp (RowsELocalRef, ColsELocalRef, RowsELocal, ColsELocal, DistanceEllipseCenters)
    DiffRadius := abs(RadiiE - RadiiERef)
    for i := 0 to |DistanceEllipseCenters| - 1 by 1
    if (DistanceEllipseCenters[i] > 2 or DiffRadius[i] > 2)
    ID_Deviation := [ID_Deviation,i + 1]
    endif
    endfor
    endif
    if (NumberRowsE < NumberRowsERef)
    j := 0
    for i := 0 to NumberRowsE - 1 by 1
    ok := 0
    while (ok == 0)
    distance_pp (RowsELocalRef[j], ColsELocalRef[j], RowsELocal[i], ColsELocal[i], Distance)
    DiffRadius := abs(RadiiE[i] - RadiiERef[j])
    if ((Distance < 10) and (DiffRadius < 10))
    if (Distance > 2 or DiffRadius > 2)
    ID_Deviation := [ID_Deviation,j + 1]
    endif
    ok := 1
    else
    ID_Missing := [ID_Missing,j + 1]
    endif
    if (j == NumberRowsERef - 1)
    ok := 1
    endif
    j := j + 1
    endwhile
    endfor
    endif
    if (NumberRowsE > NumberRowsERef)
    disp_message (WindowID, ‘There are more circles than in the reference model!’, ‘window’, 350, 150, ‘white’, ‘false’)
    endif
    dev_set_draw (‘margin’)
    NumCircleDeviation := |ID_Deviation|
    NumCircleMissing := |ID_Missing|
    counter := 0
    * 仿射变换的反变换
    hom_mat2d_invert (HomMat2DTranslateResults, HomMat2DInvert)
    for i := 0 to NumCircleDeviation - 1 by 1
    index := ID_Deviation[i]
    counter := counter + 1
    disp_message (WindowID, 'Deviation (>2 pixels) for circle with index ’ + index, ‘window’, 30 + counter * 20, 10, ‘red’, ‘false’)
    affine_trans_pixel (HomMat2DInvert, RowsELocalRef[index - 1], ColsELocalRef[index - 1], RowsEVis, ColsEVis)
    gen_circle (Circle, RowsEVis, ColsEVis, RadiiERef[index - 1])
    dev_display (Circle)
    endfor
    for i := 0 to NumCircleMissing - 1 by 1
    index := ID_Missing[i]
    counter := counter + 1
    disp_message (WindowID, 'Missing: circle with index ’ + index, ‘window’, 30 + counter * 20, 10, ‘red’, ‘false’)
    affine_trans_pixel (HomMat2DInvert, RowsELocalRef[index - 1], ColsELocalRef[index - 1], RowsEVis, ColsEVis)
    gen_circle (Circle, RowsEVis, ColsEVis, RadiiERef[index - 1])
    dev_display (Circle)
    endfor
    endif
    stop ()
    endfor
    dev_update_window (‘on’)

处理思路

这个例子是主要讲解了xld轮廓的拟合和xld轮廓的仿射变换。

后记

大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/254966.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

编写批处理文件-------基础

第一、Windows bat 批处理文件 编写 如何编写批处理文件 批处理文件&#xff08;batch file&#xff09;包含一系列 DOS命令&#xff0c;通常用于自动执行重复性任务。 用户只需双击批处理文件便可执行任务&#xff0c;而无需重复输入相同指令。编写批处理文件非常简单&#xf…

主控芯片

主控芯片&#xff1a; 主控芯片里有310&#xff0c;320,3288&#xff0c;288,318&#xff0c;333&#xff0c;345&#xff0c;7501, 其中310是中星微发展比较早&#xff0c;比较成熟的芯片。在现在一般应用在水晶夹子之类的低端产品上。 3288也是低端芯片&#xff0c;318&…

MPEG2、H.263、H.264协议效率对比

[摘录]1.1 MPEG2、H.263、H.264协议效率对比ITUT中定义的双向视频通信协议族包括&#xff1a;H.320、H.323&#xff0c;这两个协议族中&#xff0c;包含了很多子协议&#xff0c;例如音频编码协议、视频编码协议等&#xff0c;其中视频编码包括&#xff1a;H.261、H.263、H.264…

WebService SOAP、Restful和HTTP(post/get)请求区别

web service&#xff08;SOAP&#xff09; Webservice的一个最基本的目的就是提供在各个不同平台的不同应用系统的协同工作能力。 Web service 就是一个应用程序&#xff0c;它向外界暴露出一个能够通过Web进行调用的API。 SOAP是一种简单基于xml的轻量协议&#xff0c;用户web…

Block的循环引用详解

1.首先我们创建了一个网络请求工具类 然后storyboard里面去创建了一个导航控制器 并且把它设置为初始控制器 然后拖入一个bar button &#xff0d;&#xff0d;show&#xff0d;&#xff0d;到自带的控制器 这个时候运行代码的结果是 x 显然这个时候没有造成循环引用 为什…

HALCON示例程序measure_pump.hdev螺纹孔位置与尺寸测量

HALCON示例程序measure_pump.hdev螺纹孔位置与尺寸测量 示例程序源码&#xff08;加注释&#xff09; 关于显示类函数解释 dev_update_var (‘off’) dev_update_off () read_image (Image, ‘pumpe’) get_image_size (Image, Width, Height) dev_close_window () dev_open_…

计算机视觉和图形学中的摄像机内参数矩阵详解

在计算机视觉和图形学中都有“摄像机内参数矩阵”这个概念&#xff0c;其含义大致相同&#xff0c;但在实际使用过程中&#xff0c;这两个矩阵却相差甚远。在增强现实中&#xff0c;为了使计算机绘制的虚拟物体和真实环境图像对其&#xff0c;需要令虚拟摄像机的内参数和真实摄…

c#和html方法互调

具体见连接&#xff1a;https://www.cnblogs.com/zeroLove/p/3912460.html转载于:https://www.cnblogs.com/gaara-zhang/p/8746403.html

数据库基础杂记

sql,Structured Query Language结构化查询语言。SQL 是一门 ANSI(美国国家标准局) 的标准计算机语言&#xff0c;用来访问和操作数据库系统。SQL 语句用于取回和更新数据库中的数据。SQL 可与数据库程序协同工作&#xff0c;比如 MS Access、DB2、Informix、MS SQL Server、Ora…

很有用的X264和ffmpeg的设置

很有用的X264和FFMPEG的设置 http://www.360doc.com/content/11/0209/16/3705984_91612512.shtml 点击打开链接

HALCON示例程序measure_ring.hdev齿轮齿宽度测量

HALCON示例程序measure_ring.hdev齿轮齿宽度测量 示例程序源码&#xff08;加注释&#xff09; 关于显示类函数解释 read_image (Image, ‘rings_and_nuts’) dev_close_window () dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandle) set_display_font (Windo…

基于RBGD的mapping

最近学习RGBD的SLAM&#xff0c;收集了两个RGBD的mapping的开源工具包 1.RGBDSlam2 a.安装方法&#xff1a; #准备工作空间 source /opt/ros/indigo/setup.bash mkdir -p ~/rgbdslam_catkin_ws/src cd ~/rgbdslam_catkin_ws/src catkin_init_workspace cd ~/rgbdslam_catkin_ws…

【瓜分5000元奖金】Wannafly挑战赛13

链接&#xff1a;https://www.nowcoder.com/acm/contest/80/A来源&#xff1a;牛客网 zzy的小号 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒空间限制&#xff1a;C/C 262144K&#xff0c;其他语言524288K64bit IO Format: %lld题目描述 学家zzy根据字体的特点&#…

X264参数设定详细解释

x264 core:65 r1074M b6bb3d4 Syntax: x264 [options] -o outfile infile [widthxheight] 语法(命令行写法)&#xff1a;x264 [参数] -o 输出文件名 输入文件名 [宽x高] 范例&#xff1a; x264 --crf 26 --ref 3 --mixed-refs --bframes 3 --b-adapt 2 --b-pyramid --weightb -…

20145217《网络对抗》 恶意代码分析

20145217《网络对抗》 免杀原理与实践 知识点学习总结 进行恶意代码分析之前必须具备以下知识&#xff1a;编程、汇编/反汇编、网络基本知识、PE文件结构以及一些常用行为分析软件。 一、在一个已经感染了恶意代码的机器上如何找到病毒文件&#xff1f; 找到恶意代码才能对其分…

HALCON示例程序measure_screw.hdev螺纹尺寸测量

HALCON示例程序measure_screw.hdev螺纹尺寸测量 示例程序源码&#xff08;加注释&#xff09; 关于显示类函数解释 dev_update_off () read_image (Image, ‘screw_thread’) get_image_pointer1 (Image, Pointer, Type, Width, Height) dev_close_window () dev_open_window…

边工作边刷题:70天一遍leetcode: day 97-2

Design Hit Counter 要点&#xff1a;因为是second granularity&#xff0c;所以可以用以秒为单位的circular buffer方法。这题简单在只需要count过去300秒的&#xff0c;增加难度可以count过去秒&#xff0c;分钟&#xff0c;小时。 2个时间点都有可能更新超时的统计&#xff…

cvRemap 对图像进行普通几何变换

cvRemap 对图像进行普通几何变换 函数 cvRemap 利用下面指定的矩阵变换输入图像:   dst(x,y)<-src(mapx(x,y),mapy(x,y))   与其它几何变换类似&#xff0c;可以使用一些插值方法&#xff08;由用户指定&#xff0c;同cvResize&#xff09;来计算非整数坐标的像素值 vo…

disconf(二):服务端使用总结

1、服务端原理客户端启动&#xff0c;把配置文件&#xff0c;配置项存到仓库&#xff0c;等到服务端启动&#xff0c;从服务端拉取数据&#xff1b;服务端更新&#xff0c;则通过zk通知客户端&#xff0c;客户端知道更新后&#xff0c;会从服务端拉取最新的配置文件&#xff0c…

B2C和B2B之间有多大差距

从产品应用的角度&#xff0c;我们团队经历了企图将B2C系统套用到B2B业务流程上的阶段&#xff0c;对于自营业务这还勉强可以实施&#xff0c;但对于外部用户的实施难度就太大了&#xff0c;用户体验也不好。这个过程中&#xff0c;我只关注了技术范畴的迭代速度、而忽略了用户…