当前位置:网站首页>Halcon glue line detection - template matching, pose transformation, glue width, glue continuity detection

Halcon glue line detection - template matching, pose transformation, glue width, glue continuity detection

2022-06-23 04:19:00 beyond951

First , Set the specifications for caliper measurement , At the same time, create a template based on image features , Used to identify the positioning image .

* Minimum glue width 
MinValue:=19
MaxValue:=20
* Half the length of the caliper rectangle 
MeasureLength:=20
* Half the width of the caliper rectangle 
MeasureWidth:=10
* Smoothing factor 
Sigma:=3
* threshold 
MeasureThreshold:=80
read_image (Image1, 'D:/00-halcon Program code /1/0.bmp')
rgb1_to_gray (Image1, Image0)
get_image_size (Image0, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width/4, Height/4, 'black', WindowHandle)
dev_display (Image0)
* Create a template 
gen_rectangle2 (ROI_0, 1215.5, 807.5, rad(2.32782), 492.406, 501.267)
reduce_domain (Image0, ROI_0, ImageReduced)
dev_clear_window ()
dev_display (ImageReduced)
rgb1_to_gray (ImageReduced, ImageGray)
threshold (ImageGray, Region, 220, 255)
opening_rectangle1 (Region, RegionOpening, 5, 5)
closing_rectangle1 (RegionOpening, RegionClosing, 5, 5)
connection (RegionClosing, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 26051.2, 45246.8)
dilation_rectangle1 (SelectedRegions, RegionDilation, 7, 7)
area_center (RegionDilation, Area, Row, Column)
reduce_domain (ImageGray, RegionDilation, ImageReduced1)
create_scaled_shape_model (ImageReduced1, 'auto', -0.78, 0.78, 'auto', 0.7, 1.3, 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)
vector_angle_to_rigid (0, 0, 0, Row, Column, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, ContoursAffinTrans, HomMat2D)

Created template outline , Projected onto the template image through radial transformation , It is shown in the figure :

Then based on image feature matching , After affine transformation , Maintain the pose of the template image , Create in the glue line area ROI, Measure the glue width and glue path continuity based on caliper tool .

*  Read picture file 
LineStartRs:=[]
LineStartCs:=[]
LineEndRs:=[]
LineEndCs:=[]
list_files ('D:/00-halcon Program code /1', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    rgb1_to_gray (Image, Image)
    *  Template matching 
    find_scaled_shape_model (Image, ModelID, -0.78, 0.78, 0.7, 1.3, 0.8, 1, 0.5, 'least_squares_high', 0, 0.9, Row1, Column1, Angle, Scale, Score)
    * Pose correction 
    * Image affine transformation 
    vector_angle_to_rigid (Row1, Column1, Angle,Row,Column,0, HomMat2D1)
    affine_trans_image (Image, ImageAffinTrans, HomMat2D1, 'constant', 'false')
    * Template contour affine transformation 
    hom_mat2d_translate (HomMat2D1, Row1, Column1, HomMat2DTranslate)
    hom_mat2d_rotate (HomMat2DTranslate, Angle, Row1, Column1, HomMat2DRotate)
    hom_mat2d_scale (HomMat2DRotate, Scale, Scale, Row1, Column1, HomMat2DScale)
    affine_trans_contour_xld (ModelContours, ModelTrans, HomMat2DScale)
    * Display the transformed picture and outline 
    dev_clear_window ()
    dev_display (ImageAffinTrans)
    dev_set_color ('red')
    dev_set_line_width (3)
    dev_display (ModelTrans)
    * Four corners with glue area 
    
    gen_rectangle2 (ROI_3, 1580.5, 1202.5, rad(4.28915), 80.2247, 80.1387)
    gen_rectangle2 (TMP_Region, 794.5, 1176.5, rad(-86.4237), 64.1249, 75.9578)
    union2 (ROI_3, TMP_Region, ROI_3)
    gen_rectangle2 (TMP_Region, 824.5, 396.5, rad(-176.906), 74.108, 74.216)
    union2 (ROI_3, TMP_Region, ROI_3)
    gen_rectangle2 (TMP_Region, 1612.5, 420.5, rad(4.28915), 80.2247, 78.9717)
    union2 (ROI_3, TMP_Region, ROI_3)
    * Extract the area containing glue 
    gen_circle (ROI_1, 1206.96, 804.712, 426.491)
    union2 (ROI_1, ROI_3, RegionUnion)
    difference (ROI_0, RegionUnion, RegionDifference)
    reduce_domain (ImageAffinTrans, RegionDifference, ImageReduced2)
    threshold (ImageReduced2, Region1, 0, 40)
    opening_rectangle1 (Region1, RegionOpening1, 7, 7)
    connection (RegionOpening1, ConnectedRegions1)
    select_shape (ConnectedRegions1, SelectedRegions2, 'contlength', 'and', 1000, 2000)
    dilation_rectangle1 (SelectedRegions2, RegionDilation1, 21, 21)
    * Perform skeleton operation on the area containing glue 
    * Measure with caliper according to the end point of the skeleton 
    for Index2 := 1 to 4 by 1
        select_obj (RegionDilation1, ObjectSelected, Index2)
        skeleton (ObjectSelected, Skeleton)
        get_region_points (Skeleton, Rows1, Columns1)
        LineStartRs[Index2-1]:=Rows1[0]
        LineEndRs[Index2-1]:=Rows1[|Rows1|-1]
        LineStartCs[Index2-1]:=Columns1[0]
        LineEndCs[Index2-1]:=Columns1[|Columns1|-1]
    endfor
    * Start measuring with caliper 
    create_metrology_model (MetrologyHandle)
    * The image size is preset , To speed up apply_metrology_model The first call of . 
    set_metrology_model_image_size (MetrologyHandle, Width, Height)
    * Create two gauge line objects and set parameters  
    add_metrology_object_line_measure (MetrologyHandle, LineStartRs, LineStartCs, LineEndRs, LineEndCs, MeasureLength, MeasureWidth, Sigma, MeasureThreshold, [], [], Index3)
    ***** Perform a linear caliper measurement 
    apply_metrology_model (ImageAffinTrans, MetrologyHandle)
    ***** Obtain a linear caliper measuring tool 
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'all_param', Parameter)
    get_metrology_object_measures (Contours5, MetrologyHandle, 'all', 'all', Row5, Column5)
    * Obtain the measurement result contour 
    get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)
    
    dev_clear_window ()
    gen_cross_contour_xld (Cross, Row5, Column5, 6, 0.785398)
    * Start to judge whether the glue width meets the requirements , Whether there is glue break 、 Wave glue 
    
    ***** Show 
    dev_set_color ('green')
    dev_set_line_width (1)
    dev_display (ImageAffinTrans)
    dev_display (Cross)
    dev_set_color ('blue')
    dev_display (MeasuredLines)
    ***** Clear the caliper tool handle 
    clear_metrology_model (MetrologyHandle)
    
    
    
endfor

  The measurement results are shown in the figure :

 

原网站

版权声明
本文为[beyond951]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206222246136830.html