目标:给一组图片,找到每张图片的角点坐标。
原图
效果图
代码
read_image(Image,'D:/1207_培训/培训计划/halcon培训测试题/模板匹配/测试题/定位角/Image_0001664162743348.bmp')
dev_get_window(WindowHandle)
get_image_size(Image, Width, Height)
*制作模板
draw_rectangle1(WindowHandle, Row14, Column14, Row24, Column24)
gen_rectangle1(Rectangle, Row14, Column14, Row24, Column24)
*获取模板的中心坐标 Row3, Column3,后面矩阵仿射的时候用
area_center(Rectangle, Area, Row3, Column3)
reduce_domain(Image, Rectangle, ImageReduced)
create_shape_model(ImageReduced, 'auto',rad(-180), rad(720), 'auto', 'auto', 'ignore_global_polarity', 'auto', 'auto', ModelID)
*自己画线(这个算子是外部算子)
draw_rake(Regions, WindowHandle, 30, 60, 15, Row1, Column1, Row2, Column2)
draw_rake(Regions1, WindowHandle, 30, 60, 15, Row11, Column11, Row21, Column21)
*相当于卡尺工具
rake(Image, Regions2, 30, 60, 15, 1, 20, 'all', 'max', Row1, Column1, Row2, Column2, ResultRow, ResultColumn)
gen_cross_contour_xld(Cross, ResultRow, ResultColumn, 6, 0.785398)
pts_to_best_line(Line, ResultRow, ResultColumn, 2, Row12, Column12, Row22, Column22)
rake(Image, Regions3, 30, 60, 15, 1, 20, 'all', 'max', Row11, Column11, Row21, Column21, ResultRow1, ResultColumn1)
gen_cross_contour_xld(Cross1, ResultRow1, ResultColumn1, 6, 0.785398)
pts_to_best_line(Line1, ResultRow1, ResultColumn1, 2, Row13, Column13, Row23, Column23)
list_files ('D:/1207_培训/培训计划/halcon培训测试题/模板匹配/测试题/定位角', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*开始查找模板
find_shape_model(Image, ModelID, rad(-180), rad(720), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
if(|Row|>0)
dev_display_shape_matching_results(ModelID, 'red', Row, Column, Angle, 1, 1, 0)
*根据模板中心坐标形成一个矩阵
vector_angle_to_rigid(Row3, Column3, 0, Row, Column, Angle, HomMat2D)
affine_trans_pixel(HomMat2D, Row1, Column1, RowTrans, ColTrans)
affine_trans_pixel(HomMat2D, Row2, Column2, RowTrans1, ColTrans1)
rake(Image, Regions4, 30, 60, 15, 1, 20, 'all', 'max', RowTrans, ColTrans, RowTrans1, ColTrans1, ResultRow2, ResultColumn2)
pts_to_best_line(Line2, ResultRow2, ResultColumn2, 2, Row15, Column15, Row25, Column25)
affine_trans_pixel(HomMat2D, Row11, Column11, RowTrans2, ColTrans2)
affine_trans_pixel(HomMat2D, Row21, Column21, RowTrans3, ColTrans3)
rake(Image, Regions5, 30, 60, 15, 1, 20, 'all', 'max', RowTrans2, ColTrans2, RowTrans3, ColTrans3, ResultRow3, ResultColumn3)
pts_to_best_line(Line3, ResultRow3, ResultColumn3, 2, Row16, Column16, Row26, Column26)
*求两条直线的交点
intersection_lines ( Row15, Column15, Row25, Column25,Row16, Column16, Row26, Column26,Row4, Column4, IsOverlapping)
gen_cross_contour_xld(Cross4, Row4, Column4, 40, 0.785398)
*显示信息
dev_set_color('green')
dev_display(Regions4)
dev_display(Regions5)
dev_set_color('blue')
dev_display(Line2)
dev_display(Line3)
dev_set_color('red')
dev_display(Cross4)
disp_message(WindowHandle, ['x:'+Row4,'y:'+Column4], 'image', Row4+10, Column4+10, 'black', 'true')
endif
stop()
endfor
这个效果实现的关键在于引入外部算子。外部算子的获取渠道推荐一个(51halcon视觉技术网
网址 https://www.51halcon.com/forum.php),这是我意外发现的,里面有很多的算子,以及halcon的学习资料。
如何引入外部算子
首先要找到你要引入的函数的文件,放到某个目录下。
函数——管理函数——添加
这个代码的难点是坐标有点多,注意不要混淆。加油呀!