Halcon常用api背诵版

时间计算


 
  1. count_seconds(s1)
  2. count_seconds(s2)
  3. Time := (s2-s1) * 1000 //Time单位是ms

循环控制


 
  1. for i := 0 to num -1 by 1
  2. endfor
  3. while()
  4. endwhile
  5. if()
  6. endif

控制变量赋值


 
  1. colors := [ 'red', 'yellow']
  2. row1 := []
  3. row2 := [ 323, 2, 3]
  4. column1 := []
  5. column2 := [ 32, 23]
  6. IndexS := []
  7. IndexE := []
  8. ModelIDs := []
  9. singleNums := 12312
  10. //追加元素
  11. IndexS := [IndexS, 12]
  12. //求数组长度
  13. num := |xjh| 这么做是错误的
  14. ||只能当作参数来用,比如 for i:= 0 to |xjh| -1 by 1 或者raw:=[raw, 12,|raw|]

图形变量


 
  1. //图形变量数组
  2. gen_empty_obj (Models)
  3. //追加元素,concat是合并的意思
  4. concat_obj(Objects1, Objects2 : ObjectsConcat : : )
  5. concat_obj(Models, xjh ,Models)
  6. //在Models追加xjh
  7. gen_empty_obj (EmptyObject)
  8. read_image(xjh, 'printer_chip/printer_chip_01')
  9. concat_obj(EmptyObject,xjh,EmptyObject)
  10. read_image (xjh1, 'printer_chip/printer_chip_02')
  11. concat_obj (xjh,xjh1,xjh)
  12. //计算个数
  13. count_obj()

窗口基本操作

每次新建文件时,最好写出如下操作,先设置好:

  1. 显示字体
    1. 填充方式
    2. 填充颜色
    3. 线型

 
  1. read_image()
  2. write_image()
  3. dev_close_window()
  4. dev_open_window()
  5. dev_open_window_fit_image()
  6. get_image_size(Image : : : Width, Height) //获取图像宽高
  7. dev_resize_window_fit_size()
  8. set_window_param()
  9. dev_clear_window()
  10. dev_set_part() //不修改图片分辨率,修改想要显示的部分
  11. dev_update_window()
  12. dev_update_off()
  13. set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
  14. dev_update_off ()
  15. dev_set_draw( 'margin')
  16. dev_set_color( 'red')
  17. dev_set_line_width( 1)

画笔颜色,字体等

基本设置


 
  1. dev_set_draw(‘margin ') //绘画目标是区域还是轮廓等
  2. dev_display()
  3. dev_set_color() //绘制颜色
  4. dev_set_line_width()
  5. set_display_font()
  6. disp_message()
  7. disp_continue_message()
  8. write_string() //写字符道窗口

绘制


 
  1. set_display_font()
  2. disp_message()
  3. disp_continue_message()
  4. write_string() //写字符道窗口
  5. //在窗口中制定位置写字符串,
  6. set_tposition(WindowHandle, 20, 20)
  7. write_string(WindowHandle, "My string in the WindowHandle" + J$ 'd')

绘制图形,抠图


 
  1. gen_circle()
  2. gen_rectangle1()
  3. gen_rectangle2()
  4. area_center()
  5. //抠图
  6. reduce_domain(Image, Region : ImageReduced : : )
  7. gen_image_const() // 空图片
  8. unino1()
  9. union2()

阈值/区域/形态学/灰度化/色彩空间转换


 
  1. threshold()
  2. connection()
  3. select_shape()
  4. fill_up()
  5. dilation_circle()
  6. rgb1_to_gray(Image,grayImage)
  7. decompose(image,r,g,b)
  8. trans_from_rgb(image1,image2,image3,h,s,v, 'hsv')

提取区域转换轮廓


 
  1. //自动设置shape模版图像和自动分割区域,且创建区域金字塔
  2. inspect_shape_model (Image, ModelImages, ModelRegions, 1, 30)
  3. //合并相邻区域,独立不同区域
  4. connection (ModelRegions, ConnectedRegions)
  5. //选择目标区域
  6. select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 20, 100000)
  7. //联合需要区域
  8. union1 (SelectedRegions, ModelRegions)
  9. //区域骨架转换轮廓
  10. gen_contours_skeleton_xld (ModelRegions, ModelContours, 1, 'filter')

xld轮廓相关


 
  1. fit_line_contour_xld()
  2. gen_contour_polygon_xld()
  3. gen_contour_polygon_rounded_xld() //亚像素轮廓
  4. paint_xld()
  5. //读取轮廓
  6. // 参数(输出读取的contours,文件路径,输入dxf文件控制参数名,控制参数值,Dxf文件被读取的状态信息)
  7. read_contour_xld_dxf (Contours, 'metal-part-' + J$ '02', [], [], DxfStatus)

数学运算


 
  1. // 计算标准差,就是看看离散程度,标准差=sqrt(方差)
  2. StdDevRows := deviation(Rows)
  3. StdDevCols := deviation(Cols)
  4. // 弧度
  5. AngleStart := -rad( 44)
  6. AngleEnd := -rad( 144)

模版匹配

ncc


 
  1. // ncc模版匹配
  2. /create_ncc_model(Template,输入单通道模版图片
  3. NumLevels,金字塔层数
  4. AngleStart, 模版最小旋转角度
  5. AngleExtent, 模版最大旋转角度
  6. AngleStep, 模版角度步长
  7. Metric,匹配算法
  8. ModelID输出模版模型句柄)
  9. /
  10. /find_ncc_model(Image,输入图片
  11. ModelID, 模版模型句柄
  12. AngleStart, 匹配旋转其实角度
  13. AngleExtent, 终止角度
  14. MinScore, 匹配得分最小值
  15. NumMatches, 匹配个数 0全要
  16. MaxOverlap, 最大重叠率 0 -1
  17. SubPixel, 是否采用亚像素
  18. NumLevels,使用模版模型中第几层金字塔
  19. Row, 输出匹配结果的行列坐标角度和得分
  20. Column, Angle, Score)
  21. /
  22. create_ncc_model(Template : : NumLevels, AngleStart, AngleExtent, AngleStep, Metric : ModelID)
  23. find_ncc_model(Image : : ModelID, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels : Row, Column, Angle, Score)
  24. clear_ncc_model()
  25. dev_display_ncc_matching_results()

shape

Halcon常用api背诵版


 
  1. //主要的算子
  2. //不带缩放的模版
  3. create_shape_model()
  4. /create_scaled_shape_model(Template模版图片,
  5. NumbLevels模板金字塔层数,= 5
  6. AngleStart模板最小旋转角度,= 0
  7. AngleExtent模版旋转范围,= 360
  8. AngleStep旋转步长, 'auto'
  9. ScaleMin最小缩放比例,= 0.8
  10. ScaleMax最大缩放比例,= 1.1
  11. ScaleStep缩放步长, 'auto'
  12. Optimization优化方法,= 'none'
  13. Metric匹配标准,= 'ignore_global_polarity'
  14. Contrast对比度,= 40
  15. MinContrast最小对比度,= 10
  16. ModelID生成的形状模版模型句柄)
  17. 对比度小一点可能比较容易找到,但是容易带来干扰
  18. 这个算子会直接把模型坐标放到 0, 0角度调整到 0
  19. /
  20. create_scaled_shape_model (ImageReduced, 5, rad( -45), rad( 90), 'auto', 0.8, 1.0, 'auto', 'none', 'ignore_global_polarity', 40, 10, ModelID)
  21. create_scaled_shape_model()
  22. get_shape_model_contours() //所有的contours的位置都在0,0和angle:=0处
  23. find_shape_model()
  24. //带缩放的算子
  25. /find_scaled_shape_model(Image输入图片,
  26. ModelID模型句柄,
  27. AngleStart旋转初始角度,
  28. AngleExtent角度范围,
  29. ScaleMin最小缩放比例,
  30. ScaleMax最大缩放比例,
  31. MinScore匹配最小得分,
  32. NumMatches匹配个数,
  33. MaxOverlap重叠率,
  34. SubPixel亚像素级精度,
  35. NumLevels模版金字塔第几层,
  36. Greediness贪心度,
  37. Row, Column, Angle, Scale, Score匹配结果的行列坐标,缩放比例和得分值)
  38. /
  39. find_scaled_shape_model()
  40. clear_shape_model()
  41. create_aniso_shape_model() // 创建不等比例的模型匹配
  42. find_aniso_shape_model()
  43. clear_shape_model()
  44. //显示模版图片,和各个区域金字塔,层数按NumLevels指定
  45. //相当于自动求出了各个区域
  46. inspect_shape_model(Image : ModelImages, ModelRegions : NumLevels, Contrast : )
  47. //示例
  48. inspect_shape_model(ImageReduced, ModelImages, ModelRegions, 4, 30)
  49. // 标准套路
  50. dev_update_pc( 'off')
  51. dev_update_window( 'off')
  52. dev_update_var( 'off')
  53. read_image(Image, 'green-dot')
  54. get_image_size(Image, Width, Height)
  55. dev_close_window()
  56. dev_open_window( 0, 0, Width, Height, 'black', WindowHandle)
  57. dev_display(Image)
  58. dev_set_color( 'red')
  59. dev_set_draw( 'margin')
  60. dev_set_line_width ( 1)
  61. dev_set_colored( 12)
  62. threshold (Image, Region, 0, 128)
  63. connection(Region, ConnectedRegions)
  64. select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 13000, 16000)
  65. fill_up(SelectedRegions, RegionFillUp)
  66. dilation_circle(RegionFillUp, RegionDilation, 3.5)
  67. reduce_domain(Image, RegionDilation, ImageReduced)
  68. //create shape-contours model
  69. /create_scaled_shape_model (ImageReduced, 'auto', rad( 0), rad( 360), 'auto', 0.9, 1.1, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
  70. /
  71. //注释掉的只能检测出完全的目标,对于只有半个模版形状的目标,无法检测
  72. create_scaled_shape_model (ImageReduced, 5, rad( -45), rad( 90), 'auto', 0.8, 1.0, 'auto', 'none', 'ignore_global_polarity', 40, 10, ModelID)
  73. //获取轮廓的目的是为了显示匹配结果
  74. get_shape_model_contours (Model, ModelID, 1)
  75. read_image (Image1, 'green-dots')
  76. find_scaled_shape_model (Image1, ModelID, rad( 0), rad( 360), 0.8, 1.0, 0.5, 0, 0.5, 'least_squares', 5, 0.8, Row, Column, Angle, Scale, Score)
  77. for J:= 0 to |Score| -1 by 1
  78. vector_angle_to_rigid ( 0, 0, 0,Row[J], Column[J], Angle[J], HomMat2D)
  79. affine_trans_contour_xld (Model, ContoursAffineTrans, HomMat2D)
  80. dev_display (Image1)
  81. dev_display (ContoursAffineTrans)
  82. endfor

保存,读取,获得参数


 
  1. //读取
  2. raed_shape_model()
  3. read_shape_model (ModelFile, ReusedModelID)
  4. //获得参数
  5. get_shape_model_contours()
  6. get_shape_model_origin()
  7. get_shape_model_params()
  8. //示例
  9. get_shape_model_contours (ReusedShapeModel, ReusedModelID, 1)
  10. get_shape_model_origin (ReusedModelID, ReusedRefPointRow, ReusedRefPointCol)
  11. get_shape_model_params (ReusedModelID, NumLevels, AngleStart, AngleExtent, AngleStep, ScaleMin, ScaleMax, ScaleStep, Metric, MinContrast)
  12. //保存
  13. write_shape_model()
  14. write_shape_model (ModelID, 'green-dot.shm')

多对多模版匹配


 
  1. dev_open_window ( 0, 0, 646, 482, 'black', WindowHandle)
  2. dev_set_draw ( 'margin')
  3. Colors := [ 'red', 'green', 'cyan']
  4. Row1 := [ 135, 150, 185]
  5. Column1 := [ 250, 170, 220]
  6. Row2 := [ 375, 310, 335]
  7. Column2 := [ 355, 395, 375]
  8. gen_empty_obj (Models)
  9. IndexS := []
  10. IndexE := []
  11. ModelIDs := []
  12. for J := 1 to 3 by 1
  13. read_image (Image, 'metal-parts/metal-part-model-' + J$ '02d')
  14. gen_rectangle1 (Rectangle, Row1[J - 1], Column1[J - 1], Row2[J - 1], Column2[J - 1])
  15. area_center (Rectangle, Area, Row, Column)
  16. reduce_domain (Image, Rectangle, ImageReduced)
  17. inspect_shape_model (Image, ModelImages, ModelRegions, 1, 30)
  18. connection (ModelRegions, ConnectedRegions)
  19. select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 20, 100000)
  20. union1 (SelectedRegions, ModelRegions)
  21. gen_contours_skeleton_xld (ModelRegions, ModelContours, 1, 'filter')
  22. create_shape_model (ImageReduced, 5, rad( 0), rad( 360), 'auto', 'pregeneration', 'use_polarity', 30, 7, ModelID)
  23. get_shape_model_contours (ModelCont, ModelID, 1)
  24. select_shape_xld (ModelCont, ModelContours, 'contlength', 'and', 20, 1000)
  25. count_obj (ModelContours, NumModel)
  26. count_obj (Models, NumModels)
  27. concat_obj (Models, ModelContours, Models)
  28. IndexS := [IndexS,NumModels + 1]
  29. IndexE := [IndexE,NumModels + NumModel]
  30. ModelIDs := [ModelIDs,ModelID]
  31. endfor
  32. Button := 0
  33. ImgNo := 1
  34. while (Button != 1)
  35. read_image (Image, 'metal-parts/metal-parts-' + ImgNo$ '02d')
  36. //返回的Row和Column是个数组,完全取决于匹配了几个目标
  37. find_shape_models (Image, ModelIDs, rad( 0), rad( 360), 0.5, 0, 0.5, 'least_squares', 0, 0.8, Row, Column, Angle, Score, Model)
  38. //根据匹配的目标个数来进行仿射变换用来显示匹配结果
  39. Num := |Score|
  40. for J := 0 to Num - 1 by 1
  41. copy_obj (Models, ModelSelected, IndexS[Model[J]], IndexE[Model[J]] - IndexS[Model[J]] + 1)
  42. vector_angle_to_rigid ( 0, 0, 0, Row[J], Column[J], Angle[J], HomMat2D)
  43. affine_trans_contour_xld (ModelSelected, ModelTrans, HomMat2D)
  44. dev_set_color (Colors[Model[J]])
  45. dev_display (ModelTrans)
  46. endfor
  47. ImgNo := ImgNo + 1
  48. if (ImgNo > 15)
  49. ImgNo := 1
  50. endif
  51. endwhile
  52. for J := 0 to |ModelIDs| - 1 by 1
  53. clear_shape_model (ModelIDs[J])
  54. endfor

仿射affine变换

通过点角度到点角度


 
  1. //从点和角度计算仿射变换
  2. /vector_angle_to_rigid(Row1, Column1, Angle1, Row2, Column2, Angle2 : HomMat2D)
  3. /
  4. vector_angle_to_rigid() //获得放射矩阵
  5. //将2维放射矩阵作用到区域中
  6. affine_trans_region(Region : RegionAffineTrans : HomMat2D, Interpolate : )

通过添加向量方法

直接input想要映射到的位置、角度、缩放量,会自动把对应向量添加到放射矩阵中


 
  1. // 平移,旋转,缩放
  2. // 生成一个齐次变换矩阵
  3. hom_mat2d_identity (HomMat2DIdentity)
  4. // 在矩阵中增加平移量
  5. hom_mat2d_translate (HomMat2DIdentity, Row[I], Column[I], HomMat2DTranslate)
  6. // 在矩阵中增加角度量
  7. hom_mat2d_rotate (HomMat2DTranslate, Angle[I], Row[I], Column[I], HomMat2DRotate)
  8. // 在矩阵中增加缩放量
  9. hom_mat2d_scale (HomMat2DRotate, Scale[I], Scale[I], Row[I], Column[I], HomMat2DScale)
  10. //作用到轮廓框架中
  11. affine_trans_contours_xld()

测量拟合


 
  1. // 基于边缘对
  2. gen_rectangle2()
  3. gen_measure_reactangle2()
  4. measure_pairs()
  5. close_measure()
  6. // 基于非边缘对
  7. gen_rectagnle2()
  8. gen_measure_ractangle2()
  9. measure_pos()
  10. close_measure()
  11. // 弧形测量
  12. get_points_ellipse()
  13. gen_measure_arc()
  14. measure_pos()
  15. close_measure()
  16. // 两点距离
  17. distance_pp()
上一篇:Fits图片互相关交互


下一篇:Python纯手动搭建BP神经网络--手写数字识别