halcon模版匹配应用一例(查物件总数量)
By
admin
at 2018-01-27 • 1人收藏 • 2132人看过
dev_update_pc ('off') dev_update_window ('off') dev_update_var ('off') dev_close_window () dev_open_window (0, 0, 646, 482, 'black', WindowHandle) dev_set_part (0, 0, 481, 645) set_display_font (WindowHandle, 20, 'mono', 'true', 'false') dev_set_draw ('margin') dev_set_line_width (3) * These colors will be used to graphically discern the different models in the * visualization code below. Colors := ['red','green','cyan'] * The models will be generated from hard-coded rectangles given by the * following coordinates: Row1 := [62,290,274] Column1 := [187,134,424] Row2 := [144,354,329] Column2 := [237,170,479] * The object Models will hold a set of XLD contours that represent the different * models. They are used below to overlay the found models on the current * image. XLD contours are used because they can be transformed much faster * than regions. This creates a slight problem because in general multiple XLD * contours will represent one model. Therefore, the start and end indices of the * different models will be stored in IndexS and IndexE, respectively. gen_empty_obj (Models) IndexS := [] IndexE := [] * The variable ModelIDs holds the different models that are created below. ModelIDs := [] * Likewise, RowsRef and ColumnsRef store the reference points of the different * models. They are necessary to transform the models to the found instances * in the current image. for J := 1 to 3 by 1 read_image (Image, 'C:/Users/Administrator/Desktop/t1t/metal-part-model-' + J$'02d') dev_display (Image) dev_set_color ('green') set_tposition (WindowHandle, 20, 20) write_string (WindowHandle, 'Generating shape model ' + J$'d') gen_rectangle1 (Rectangle, Row1[J - 1], Column1[J - 1], Row2[J - 1], Column2[J - 1]) area_center (Rectangle, Area, Row, Column) reduce_domain (Image, Rectangle, ImageReduced) * Although we will use get_shape_model_contours below to obtain the * representation of the model, we extract a model representation here using * inspect_shape_model because this enables us to display the representation * of the model while the model is being created. inspect_shape_model (Image, ModelImages, ModelRegions, 1, 60) * Since the shape models contain a few extraneous edges, they will be * removed here to give a slightly nicer visualization. connection (ModelRegions, ConnectedRegions) select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 20, 100000) union1 (SelectedRegions, ModelRegions) gen_contours_skeleton_xld (ModelRegions, ModelContours, 1, 'filter') dev_set_color ('red') dev_display (ModelContours) create_shape_model (ImageReduced, 5, rad(0), rad(360), 'auto', 'pregeneration', 'use_polarity', 30, 7, ModelID) get_shape_model_contours (ModelCont, ModelID, 1) select_shape_xld (ModelCont, ModelContours, 'contlength', 'and', 20, 1000) * Count how many XLD contours there are in the current model and in the * already stored models. This is necessary to compute IndexS and IndexE. count_obj (ModelContours, NumModel) count_obj (Models, NumModels) concat_obj (Models, ModelContours, Models) IndexS := [IndexS,NumModels + 1] IndexE := [IndexE,NumModels + NumModel] ModelIDs := [ModelIDs,ModelID] endfor dev_set_color ('yellow') set_tposition (WindowHandle, 50, 20) write_string (WindowHandle, 'Press left button to start') set_tposition (WindowHandle, 80, 20) write_string (WindowHandle, 'and stop the demo.') get_mbutton (WindowHandle, Row3, Column3, Button1) wait_seconds (0.5) dev_set_color ('red') Button := 0 ImgNo := 1 while (Button != 1) read_image (Image, 'C:/Users/Administrator/Desktop/t1t/metal-parts-' + ImgNo$'02d') count_seconds (S1) find_shape_models (Image, ModelIDs, rad(0), rad(360), 0.4, 0, 0.5, 'least_squares', 0, 0.8, Row, Column, Angle, Score, Model) count_seconds (S2) Time := (S2 - S1) * 1000 dev_display (Image) Num := |Score| for J := 0 to Num - 1 by 1 * Select the correct XLD contours from the Models object. copy_obj (Models, ModelSelected, IndexS[Model[J]], IndexE[Model[J]] - IndexS[Model[J]] + 1) * Compute the transformation from the model object to the current instance. vector_angle_to_rigid (0, 0, 0, Row[J], Column[J], Angle[J], HomMat2D) affine_trans_contour_xld (ModelSelected, ModelTrans, HomMat2D) dev_set_color (Colors[Model[J]]) dev_display (ModelTrans) endfor dev_set_color ('yellow') set_tposition (WindowHandle, 20, 20) if (Num == 1) write_string (WindowHandle, Num$'1d' + ' object found in ' + Time$'4.2f' + 'ms') else write_string (WindowHandle, Num$'1d' + ' objects found in ' + Time$'4.2f' + 'ms') endif ImgNo := ImgNo + 1 if (ImgNo > 3) ImgNo := 1 endif dev_error_var (Error, 1) dev_set_check ('~give_error') get_mposition (WindowHandle, R, C, Button) dev_error_var (Error, 0) dev_set_check ('give_error') if (Error != H_MSG_TRUE) Button := 0 endif stop() endwhile for J := 0 to |ModelIDs| - 1 by 1 clear_shape_model (ModelIDs[J]) endfor
要点:
先选个清晰的做模板,然后生成模板数据
我做的时候是大小相同的图片做的模板,不知道缩放之后的图片会不会有问题...
登录后方可回帖