HOLCON源码学习

来源:互联网 发布:益盟bs指标源码 编辑:程序博客网 时间:2024/06/06 22:45

真是日了狗了,兜兜转转又来到了图像识别

*  ** Part 1** Create a sheet-of-light model to collect the profiles* and set the scaling factors for the reconstruction.* Note, that through the scalings, only approximate* 3d coordinates are obtained (scaling does not* replace calibration).* Fortunately, in this case, this is completely sufficient* for the task, as we only want to compare the parts* qualitatively.** Create sheet-of-light model with following parametersNumProfiles := 441DisparityRange := 512ProfileWidth := 626* In this example, the profiles are not measured by HALCON.* Instead, they are obtained directly from the sheet-of-light* sensor. Nevertheless the profile width is defined* within create_sheet_of_light_model by the width of a rectangle.get_system ('clip_region', ClipRegion)set_system ('clip_region', 'false')gen_rectangle1 (Rectangle, 0, 0, DisparityRange - 1, ProfileWidth - 1)set_system ('clip_region', ClipRegion)create_sheet_of_light_model (Rectangle, 'calibration', 'offset_scale', SheetOfLightModelID)* Set the scaling factors such that the distance* of neighboring 3D points is approximately the same* as in the real object.ScaleX := 2ScaleY := 8ScaleZ := 1Clip := 435set_sheet_of_light_param (SheetOfLightModelID, 'scale_x', ScaleX)set_sheet_of_light_param (SheetOfLightModelID, 'scale_y', ScaleY)set_sheet_of_light_param (SheetOfLightModelID, 'scale_z', ScaleZ)** Init displaydev_update_off ()dev_close_window ()dev_open_window_fit_size (0, 0, ProfileWidth, NumProfiles, -1, -1, WindowHandle)set_display_font (WindowHandle, 16, 'mono', 'true', 'false')** Create an image to display the disparitiesgen_image_const (Image, 'uint2', ProfileWidth, NumProfiles)** Create the reference object by collecting* the measured profiles in a sheet-of-light model*for Index := 0 to NumProfiles - 1 by 1    * Add the next profile to the sheet of light model    read_image (ImageModel, 'sheet_of_light/metal_part_1_disparity_line_' + Index$'03d')    set_profile_sheet_of_light (ImageModel, SheetOfLightModelID, [])    * Visualize accumulated profiles    get_grayval (ImageModel, gen_tuple_const(ProfileWidth,0), [0:ProfileWidth - 1], Grayval)    set_grayval (Image, gen_tuple_const(ProfileWidth,Index), [0:ProfileWidth - 1], Grayval)    if (Index % 5 == 0)        dev_display (Image)    endif    Message := 'Measure reference object'    Message[1] := 'Add profile ' + (Index + 1) + '/' + NumProfiles    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')endfor*Message := 'Prepare the model for distance measurement ...'disp_message (WindowHandle, Message, 'window', 60, 12, 'black', 'true')Status := ' ... triangulate object ...'Status[1] := ' ... subsample object ...'Status[2] := ' ... create surface model for alignment ...'* Select the relevant part from the sceneget_sheet_of_light_result_object_model_3d (SheetOfLightModelID, Model3DAll)select_points_object_model_3d (Model3DAll, 'point_coord_z', Clip, 800, Model3D)* Clean up memoryclear_object_model_3d (Model3DAll)** Prepare reference object for a more robust* surface comparison.** To get a more robust result, the reference object is* subsampled to achieve a better resolution for the* measurement. The subsampling needs a triangulation* as a preprocessing step.** Triangulate the model in a simple waydisp_message (WindowHandle, Status[0], 'window', 100, 12, 'white', 'false')surface_normals_object_model_3d (Model3D, 'mls', 'mls_force_inwards', 'true', ObjectModel3DNormals)triangulate_object_model_3d (ObjectModel3DNormals, 'greedy', 'greedy_remove_small_surfaces', 200, TriangulatedObjectModel3D, Information)* Clean up memoryclear_object_model_3d (ObjectModel3DNormals)* Sample the object for distance measurementsdisp_message (WindowHandle, [Status[0] + ' ready.',Status[1]], 'window', 100, 12, 'white', 'false')max_diameter_object_model_3d (TriangulatedObjectModel3D, Diameter)sample_object_model_3d (TriangulatedObjectModel3D, 'fast', Diameter * 0.002, [], [], Model3DSampled)disp_message (WindowHandle, [Status[0:1] + ' ready.',Status[2]], 'window', 100, 12, 'white', 'false')* Clean up memoryclear_object_model_3d (Model3D)** Create a surface model for alignmentcreate_surface_model (TriangulatedObjectModel3D, 0.01, 'model_invert_normals', 'true', SurfaceModelID)* Display statusMessage := Message + ' ready.'disp_message (WindowHandle, Status + ' ready.', 'window', 100, 12, 'white', 'false')disp_continue_message (WindowHandle, 'black', 'true')stop ()* Display reference modeldev_clear_window ()create_pose (1100, 1300, 35000, 140, 350, 55, 'Rp+T', 'gba', 'point', PoseDisplay)Title := 'Reference object (uncalibrated measurement)'Instructions[0] := 'Rotate: Left button'Instructions[1] := 'Zoom:   Shift + left button'Instructions[2] := 'Move:   Ctrl  + left button'visualize_object_model_3d (WindowHandle, TriangulatedObjectModel3D, [], PoseDisplay, [], [], Title, [], Instructions, PoseOut)** Main loop** Perform a surface comparison on a number of objects*NumScenes := 4for SceneIndex := 1 to NumScenes by 1    * Reset the dispartities in the sheet of light model. All other settings can be reused    reset_sheet_of_light_model (SheetOfLightModelID)    * Collect and display the disparities from the sensor    gen_image_const (Image, 'uint2', ProfileWidth, NumProfiles)    for Index := 0 to NumProfiles - 1 by 1        read_image (ImageSearch, 'sheet_of_light/metal_part_' + (SceneIndex + 1) + '_disparity_line_' + Index$'03d')        get_grayval (ImageSearch, gen_tuple_const(ProfileWidth,0), [0:ProfileWidth - 1], Grayval)        set_grayval (Image, gen_tuple_const(ProfileWidth,Index), [0:ProfileWidth - 1], Grayval)        if (Index % 5 == 0)            dev_display (Image)        endif        Message := 'Measure scene ' + SceneIndex + '/' + NumScenes        Message[1] := 'Add profile ' + (Index + 1) + '/' + NumProfiles        disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')        * Add the next profile to the sheet-of-light model        * The offset is considered constant (= ScaleY)        set_profile_sheet_of_light (ImageSearch, SheetOfLightModelID, [])    endfor    * Select the relevant part from the scene    get_sheet_of_light_result_object_model_3d (SheetOfLightModelID, Scene3DAll)    select_points_object_model_3d (Scene3DAll, 'point_coord_z', Clip, 800, Scene3D)    * Clean up memory    clear_object_model_3d (Scene3DAll)    * Match the two objects    * Note that the RelSamplingDistance has been set fairly small.    * This is necessary because the object has only little 3D variations    * that can be used by the matching process.    disp_message (WindowHandle, 'Match model and scene ...', 'window', 60, 12, 'black', 'true')    find_surface_model (SurfaceModelID, Scene3D, 0.02, 0.2, 0, 'false', [], [], Pose, Score, NotUsed)    pose_invert (Pose, PosesInvert)    * Transform the scene to match the reference object    rigid_trans_object_model_3d (Scene3D, PosesInvert, ObjectModel3DRigidTrans)    * Measure the distances between the scene and the model    distance_object_model_3d (ObjectModel3DRigidTrans, Model3DSampled, [], 0.0, 'signed_distances', 'true')    * Select points with a high distance below the model (negative distance)    * and the points above the model (positive distance)    select_points_object_model_3d (ObjectModel3DRigidTrans, '&distance', -1000, -ScaleY * 1.5, ObjectModel3DThresholdedUp)    select_points_object_model_3d (ObjectModel3DRigidTrans, '&distance', ScaleY * 1.5, 1000, ObjectModel3DThresholdedDown)    * Calculate connected components of the points below (negative distance) and    * the connected components of the points above (positive distance) the model.    * The distance threshold should be greater than the distance between    * two scan lines (> ScaleY)    connection_object_model_3d (ObjectModel3DThresholdedUp, 'distance_3d', ScaleY + 5, ObjectModel3DConnectedUp)    connection_object_model_3d (ObjectModel3DThresholdedDown, 'distance_3d', ScaleY + 5, ObjectModel3DConnectedDown)    * Keep the large components, discard small ones as noise    select_object_model_3d (ObjectModel3DConnectedUp, 'num_points', 'and', 200, 1000000, ObjectModel3DSelectedUp)    select_object_model_3d (ObjectModel3DConnectedDown, 'num_points', 'and', 200, 1000000, ObjectModel3DSelectedDown)    * Set the visualization parameters and values depending on the    * position of the error points.    NumErrors := |ObjectModel3DSelectedDown| + |ObjectModel3DSelectedUp|    Title := 'Found ' + NumErrors + ' error(s)'    ErrorColorUp := 'magenta'    ErrorColorDown := 'red'    ErrorColorNames := 'color_' + [2:NumErrors + 1]    VisParamNames := ['point_size','point_size_1','color_0','color_1',ErrorColorNames,'alpha_' + NumErrors,'disp_background']    VisParamValues := [5.0,2.0,'white','green',gen_tuple_const(|ObjectModel3DSelectedUp|,ErrorColorUp),gen_tuple_const(|ObjectModel3DSelectedDown|,ErrorColorDown),0.8,'true']    * Display the errorneous regions    dev_clear_window ()    disp_message (WindowHandle, ['Bent up','Bent down'], 'window', 12, 500, [ErrorColorUp,ErrorColorDown], 'false')    visualize_object_model_3d (WindowHandle, [TriangulatedObjectModel3D,ObjectModel3DRigidTrans,ObjectModel3DSelectedUp,ObjectModel3DSelectedDown], [], PoseDisplay, VisParamNames, VisParamValues, Title, ['','','#' + [1:NumErrors]], Instructions, PoseOut1)    * Clean up memory    clear_object_model_3d ([Scene3D,ObjectModel3DConnectedUp,ObjectModel3DConnectedDown,ObjectModel3DSelectedUp,ObjectModel3DSelectedDown,ObjectModel3DRigidTrans,ObjectModel3DThresholdedUp,ObjectModel3DThresholdedDown])endfor* Clean up memoryclear_object_model_3d ([Model3DSampled,TriangulatedObjectModel3D])clear_sheet_of_light_model (SheetOfLightModelID)clear_surface_model (SurfaceModelID)


0 0