Halcon胶线检测—模板匹配、位姿变换、胶宽,胶连续性检测

首先,设置卡尺测量的规格,同时基于图像特征创建模板,用于识别定位图像。

*胶宽最小值
MinValue:=19
MaxValue:=20
*卡尺矩形的一半长度
MeasureLength:=20
*卡尺矩形的一半宽度
MeasureWidth:=10
*平滑系数
Sigma:=3
*阈值
MeasureThreshold:=80
read_image (Image1, 'D:/00-halcon程序代码/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)
*创建模板
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)

创建后的模板轮廓,经过放射变换投射到模板图像上,其如图所示:

Halcon胶线检测—模板匹配、位姿变换、胶宽,胶连续性检测_第1张图片

然后基于图像特征匹配后,经过仿射变换,保持模板图像的位姿,在胶线区域创建ROI,基于卡尺工具进行胶宽和胶路连续性的测量。

* 读取图片文件
LineStartRs:=[]
LineStartCs:=[]
LineEndRs:=[]
LineEndCs:=[]
list_files ('D:/00-halcon程序代码/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)
    * 进行模板匹配
    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)
    *位姿补正
    *图片仿射变换
    vector_angle_to_rigid (Row1, Column1, Angle,Row,Column,0, HomMat2D1)
    affine_trans_image (Image, ImageAffinTrans, HomMat2D1, 'constant', 'false')
    *模板轮廓仿射变换
    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)
    *显示变换后的图片和轮廓
    dev_clear_window ()
    dev_display (ImageAffinTrans)
    dev_set_color ('red')
    dev_set_line_width (3)
    dev_display (ModelTrans)
    *四角含胶区域
    
    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)
    *提取含胶的区域
    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)
    *对含胶区域进行骨架操作
    *根据骨架完的点进行卡尺测量
    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
    *开始用卡尺进行测量
    create_metrology_model (MetrologyHandle)
    *预先设置了图像大小,以加速apply_metrology_model的第一次调用。 
    set_metrology_model_image_size (MetrologyHandle, Width, Height)
    *创建两个计量线对象并设置参数 
    add_metrology_object_line_measure (MetrologyHandle, LineStartRs, LineStartCs, LineEndRs, LineEndCs, MeasureLength, MeasureWidth, Sigma, MeasureThreshold, [], [], Index3)
    *****执行直线卡尺测量
    apply_metrology_model (ImageAffinTrans, MetrologyHandle)
    *****获取直线卡尺测量工具
    get_metrology_object_result (MetrologyHandle, 'all', 'all', 'result_type', 'all_param', Parameter)
    get_metrology_object_measures (Contours5, MetrologyHandle, 'all', 'all', Row5, Column5)
    *获取测量结果轮廓
    get_metrology_object_result_contour (MeasuredLines, MetrologyHandle, 'all', 'all', 1.5)
    
    dev_clear_window ()
    gen_cross_contour_xld (Cross, Row5, Column5, 6, 0.785398)
    *开始判断胶宽是否满足要求,是否存在断胶、波浪胶的情况
    
    *****显示
    dev_set_color ('green')
    dev_set_line_width (1)
    dev_display (ImageAffinTrans)
    dev_display (Cross)
    dev_set_color ('blue')
    dev_display (MeasuredLines)
    *****清除卡尺工具句柄
    clear_metrology_model (MetrologyHandle)
    
    
    
endfor

 测量结果如图所示:

Halcon胶线检测—模板匹配、位姿变换、胶宽,胶连续性检测_第2张图片

 

你可能感兴趣的:(halcon缺陷检测,图像处理,opencv,几何学)