halcon因其丰富的图像处理算法,在工业视觉中缺陷检测使用非常广泛。今天给大家带来的是halcon建立模板并进行形状匹配搜索图像实例,以前一直用visionPro原来halcon用起来也不算复杂。
图像是我用画图工具画的6个圆,其中5个是完全一样的,第6个的大小不一样。第一个圆使用halcon的算子建立模板,并成功匹配搜索到5个一模一样的图形。
代码如下:
//加载模板图像dev_open_window (0, 0, 800, 500, 'black', WindowID)read_image (ModelImage, 'C:/Users/Administrator/Desktop/halcon.png')get_image_pointer1 (ModelImage, Pointer, Type, Width, Height)dev_display (ModelImage)stop ()//绘制感兴趣区域Row1 := 32Column1 := 67Row2 := 224Column2 := 325gen_rectangle1 (ROIPart1, Row1, Column1, Row2, Column2)dev_display (ModelImage)dev_display (ROIPart1)stop ()//创建模板reduce_domain (ModelImage, ROIPart1, ImageROI)create_shape_model (ImageROI, 4, 0, 0, 'auto', 'none', 'use_polarity', 30, 10, ModelID)inspect_shape_model (ImageROI, ShapeModelImage, ShapeModelRegion, 1, 30)get_shape_model_contours (ShapeModel, ModelID, 1)dev_clear_window ()dev_set_color ('blue')dev_display (ShapeModelRegion)stop ()//加载搜索图像并搜索模板read_image (SearchImage, 'C:/Users/Administrator/Desktop/halcon.png')dev_display (SearchImage)find_shape_model (SearchImage, ModelID, 0, 0, 0.8, 0, 0.5, 'least_squares', 0, 0.7, RowCheck, ColumnCheck, AngleCheck, Score)if (|Score| > 0)for i := 0 to |Score| - 1 by 1vector_angle_to_rigid (0, 0, 0, RowCheck[i], ColumnCheck[i], AngleCheck[i], MovementOfObject)affine_trans_contour_xld (ShapeModel, ModelAtNewPosition, MovementOfObject)set_display_font (WindowID, 14, 'mono', 'true', 'false')dev_set_color ('green')dev_display (ModelAtNewPosition)set_tposition (WindowID, RowCheck[i], ColumnCheck[i])write_string (WindowID, i+':'+Score[i])endforendif
我这里使用的模板图像和搜索图像是同一张图片,上面的代码主要涉及到gen_rectangle1
绘制矩形区域、create_shape_model
创建模板算子、find_shape_model
搜索模板等算子的使用。
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/tech/iot/241844.html