aardio 官方社区

 找回密码
 注册会员

QQ登录

只需一步,快速开始

搜索
查看: 11494|回复: 0

halcon视觉处理通过边界查找并绘制圆示例

[复制链接]

38

主题

129

回帖

1045

积分

荣誉会员

积分
1045
发表于 2018-1-12 12:56:45 | 显示全部楼层 |阅读模式
以下aar代码移植于官方软件示例:
功能: 查找图片的边界, 然后根据边界绘制出圆形
工程下载链接:https://pan.baidu.com/s/1bqMNTZh 密码:lxoq
GIF动态步骤演示:
GIF.gif
最终效果如下:
QQ截图20180112121537.png
aar主要代码:
  1. import win.ui;
  2. /*DSG{{*/
  3. mainForm = win.form(text="Halcon函数circle测试";right=833;bottom=525)
  4. mainForm.add(
  5. button={cls="button";text="button";left=9;top=9;right=170;bottom=59;z=2};
  6. picturebox={cls="picturebox";left=182;top=0;right=834;bottom=526;bgcolor=8421376;z=1};
  7. snumber={cls="static";text="0";left=18;top=397;right=150;bottom=433;font=LOGFONT(h=-21);transparent=1;z=3}
  8. )
  9. /*}}*/

  10. //import console;

  11. var dll = ..raw.loadDll("\res\halconc.dll","halconlib","cdecl");
  12. T_open_window= dll.api("T_open_window","int(struct Row, struct Column, struct Width, struct Height, struct FatherWindow, struct Mode, struct Machine, struct& WindowHandle)");
  13. open_window= dll.api("open_window","int(int Row, int Column, int Width, int Height, int FatherWindow, string Mode, string Machine, int& WindowHandle)");
  14. T_read_image= dll.api("T_read_image","int(ADDR& Image, struct FileName)");
  15. read_image= dll.api("read_image","int(ADDR& Image, string FileName)");
  16. T_get_image_size= dll.api("T_get_image_size","int(ADDR Image, struct& Width, struct& Height)");
  17. get_image_size= dll.api("get_image_size","int(ADDR Image, int& Width, int& Height)");
  18. T_disp_obj= dll.api("T_disp_obj","int(ADDR Object, struct WindowHandle)");
  19. disp_obj= dll.api("disp_obj","int(ADDR Object, int WindowHandle)");
  20. T_fast_threshold= dll.api("T_fast_threshold","int(ADDR Image, ADDR& Region, struct MinGray, struct MaxGray, struct MinSize)");
  21. fast_threshold= dll.api("fast_threshold","int(ADDR Image, ADDR& Region, double MinGray, double MaxGray, int MinSize)");
  22. T_disp_region= dll.api("T_disp_region","int(ADDR DispRegions, struct WindowHandle)");
  23. disp_region= dll.api("disp_region","int(ADDR DispRegions, int WindowHandle)");
  24. T_set_colored= dll.api("T_set_colored","int(struct WindowHandle, struct NumberOfColors)");
  25. set_colored= dll.api("set_colored","int(int WindowHandle, int NumberOfColors)");
  26. T_set_color= dll.api("T_set_color","int(struct WindowHandle, struct Color)");
  27. set_color= dll.api("set_color","int(int WindowHandle, string Color)");
  28. T_boundary= dll.api("T_boundary","int(ADDR Region, ADDR& RegionBorder, struct BoundaryType)");
  29. boundary= dll.api("boundary","int(ADDR Region, ADDR& RegionBorder, string BoundaryType)");
  30. T_clip_region_rel= dll.api("T_clip_region_rel","int(ADDR Region, ADDR& RegionClipped, struct Top, struct Bottom, struct Left, struct Right)");
  31. clip_region_rel= dll.api("clip_region_rel","int(ADDR Region, ADDR& RegionClipped, int Top, int Bottom, int Left, int Right)");
  32. T_dilation_circle= dll.api("T_dilation_circle","int(ADDR Region, ADDR& RegionDilation, struct Radius)");
  33. dilation_circle= dll.api("dilation_circle","int(ADDR Region, ADDR& RegionDilation, double Radius)");
  34. T_reduce_domain= dll.api("T_reduce_domain","int(ADDR Image, ADDR Region, ADDR& ImageReduced)");
  35. reduce_domain= dll.api("reduce_domain","int(ADDR Image, ADDR Region, ADDR& ImageReduced)");
  36. T_edges_sub_pix= dll.api("T_edges_sub_pix","int(ADDR Image, ADDR& Edges, struct Filter, struct Alpha, struct Low, struct High)");
  37. edges_sub_pix= dll.api("edges_sub_pix","int(ADDR Image, ADDR& Edges, string Filter, double Alpha, int Low, int High)");
  38. T_segment_contours_xld= dll.api("T_segment_contours_xld","int(ADDR Contours, ADDR& ContoursSplit, struct Mode, struct SmoothCont, struct MaxLineDist1, struct MaxLineDist2)");
  39. segment_contours_xld= dll.api("segment_contours_xld","int(ADDR Contours, ADDR& ContoursSplit, string Mode, int SmoothCont, double MaxLineDist1, double MaxLineDist2)");
  40. T_count_obj= dll.api("T_count_obj","int(ADDR Objects, struct& Number)");
  41. count_obj= dll.api("count_obj","int(ADDR Objects, int& Number)");
  42. T_set_draw= dll.api("T_set_draw","int(struct WindowHandle, struct Mode)");
  43. set_draw= dll.api("set_draw","int(int WindowHandle, string Mode)");
  44. T_select_obj= dll.api("T_select_obj","int(ADDR Objects, ADDR& ObjectSelected, struct Index)");
  45. select_obj= dll.api("select_obj","int(ADDR Objects, ADDR& ObjectSelected, int Index)");
  46. T_get_contour_global_attrib_xld= dll.api("T_get_contour_global_attrib_xld","int(ADDR Contour, string Name, struct& Attrib)");
  47. T_fit_circle_contour_xld= dll.api("T_fit_circle_contour_xld","int(ADDR Contours, struct Algorithm, struct MaxNumPoints, struct MaxClosureDist, struct ClippingEndPoints, struct Iterations, struct ClippingFactor, struct& Row, struct& Column, struct& Radius, struct& StartPhi, struct& EndPhi, struct& PointOrder)");
  48. fit_circle_contour_xld= dll.api("fit_circle_contour_xld","int(ADDR Contours, string Algorithm, int MaxNumPoints, double MaxClosureDist, int ClippingEndPoints, int Iterations, double ClippingFactor, double& Row, double& Column, double& Radius, double& StartPhi, double& EndPhi, string& PointOrder)");
  49. T_gen_circle_contour_xld= dll.api("T_gen_circle_contour_xld","int(ADDR& ContCircle, struct Row, struct Column, struct Radius, struct StartPhi, struct EndPhi, struct PointOrder, struct Resolution)");
  50. gen_circle_contour_xld= dll.api("gen_circle_contour_xld","int(ADDR& ContCircle, double Row, double Column, double Radius, double StartPhi, double EndPhi, string PointOrder, double Resolution)");
  51. T_get_contour_attrib_xld= dll.api("T_get_contour_attrib_xld","int(ADDR Contour, string Name, struct& Attrib)");
  52. T_set_line_width= dll.api("T_set_line_width","int(struct WindowHandle, struct Width)");
  53. set_line_width= dll.api("set_line_width","int(int WindowHandle, double Width)");


  54. //console.open();
  55. mainForm.button.oncommand = function(id,event){
  56.      
  57.     var ret,Image = read_image(0,"res/double_circle");
  58.     var ret,width,height = get_image_size(Image,0,0);
  59.     var ret,Hwindow = open_window(0,0,width,height,mainForm.picturebox.hwnd,"visible", "",0);
  60.     var ret = disp_obj(Image,Hwindow);
  61.     win.delay(1000)
  62.     var ret,Region = fast_threshold(Image,0,0,120,7);
  63.     set_colored(Hwindow,6);
  64.     disp_region(Region,Hwindow)
  65.     win.delay(1000)
  66.     var ret,RegionBorder = boundary(Region,0,"inner");
  67.     var ret,RegionClipped = clip_region_rel(RegionBorder,0,5,5,5,5);
  68.     var ret,RegionDilation = dilation_circle(RegionClipped,0,2.5);
  69.     var ret,ImageReduced = reduce_domain(Image,RegionDilation,0);
  70.     var ret = disp_obj(ImageReduced,Hwindow);
  71.     win.delay(1000)
  72.     var ret,Edges = edges_sub_pix(ImageReduced,0,"canny",2,20,60);
  73.     var ret = disp_obj(Edges,Hwindow);
  74.     win.delay(1000)
  75.     var ret,ContoursSplit = segment_contours_xld(Edges,0,"lines_circles",5,4,3);
  76.     //var ret = disp_obj(ContoursSplit,Hwindow);
  77.     var ret = disp_obj(Image,Hwindow);
  78.     var ret,Number = count_obj(ContoursSplit,0);
  79.     //mainForm.snumber.text = Number;
  80.     set_draw(Hwindow,"margin");
  81.     set_color(Hwindow,"orange red");
  82.     set_line_width(Hwindow,4.0);
  83.     for(i=1;Number;1){
  84.         var ret,ObjectSelected = select_obj(ContoursSplit,0,i);
  85.         //var ret,Attrib = T_get_contour_global_attrib_xld(ObjectSelected,"cont_approx",{});
  86.         //if(Attrib > 0){
  87.             var ret,Row, Column, Radius, StartPhi, EndPhi, PointOrder = fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, 0, 0, 0, 0, 0, 0)
  88.             var ret,ContCircle = gen_circle_contour_xld (0, Row, Column, Radius, 0, math.rad(360), 'positive', 1.0)
  89.             var ret = disp_obj(ContCircle,Hwindow);
  90.             win.delay(1000)
  91.        // }
  92.     }
  93. }

  94. mainForm.enableDpiScaling();
  95. mainForm.show();

  96. return win.loopMessage();
复制代码


您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

手机版|未经许可严禁引用或转载本站文章|aardio.com|aardio 官方社区 ( 皖ICP备09012014号 )

GMT+8, 2025-2-13 06:57 , Processed in 0.056870 second(s), 25 queries .

Powered by Discuz! X3.5

Copyright © 2001-2024 Tencent Cloud.

快速回复 返回顶部 返回列表