创建匹配模板
获取图片
halcon自带图片
*读取模板图像
read_image(Image,'C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/boxes/cardboard_boxes_01.png')
*图片旋转
rotate_image (Image, ImageRotate, 50, 'constant')
*灰度操作
rgb1_to_gray(ImageRotate,GrayImage)
*选择匹配的图像,交互式ROI
dev_open_window_fit_image(GrayImage,0,0,-1,-1,WinHandle1)
dev_display(GrayImage)
dev_set_draw('margin')
dev_set_line_width(3)
draw_rectangle1(WinHandle1,r1,c1,r2,c2)
gen_rectangle1(Rectangle1,r1,c1,r2,c2)
*选择要匹配的图像,去除不要的
reduce_domain(GrayImage,Rectangle1,OutRegion)
*创建一个ncc的表示模型
create_ncc_model(OutRegion,'auto', -3.14, 6.29, 'auto', 'use_polarity',ModelID)
*关闭窗口
dev_close_window()
匹配
*读取测试图像
read_image(ImageTest,'C:/Users/Public/Documents/MVTec/HALCON-19.11-Progress/examples/images/boxes/cardboard_boxes_03.png')
*将测试图像转换为灰度图像
rgb1_to_gray(ImageTest,GrayImageTest)
dev_open_window_fit_image(GrayImageTest,0,0,-1,-1,WinHandle2)
dev_display(GrayImageTest)
*进行算法匹配
find_ncc_model(GrayImageTest, ModelID,-3.14, 6.29, 0.5, 1, 0.5,'true',0,Row,Column,Angle,Score)
*显示匹配结果
dev_display_ncc_matching_results (ModelID, 'red', Row, Column, Angle, 0)
*匹配结束,释放资源
clear_ncc_model(ModelID)