基于Halcon学习的测量拟合----实战【一】

此例子是通过把旋转后的充电头矫正,测量矩形位置不变的情况下;

可以与前两篇的测量芯片引脚的例子作为对比

* Image Acquisition 01: Code generated by Image Acquisition 01
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'gray', -1, 'false', 'default', '[0] Web Camera', 0, -1, AcqHandle)
grab_image_start (AcqHandle, -1)
while (true)
    grab_image_async (Image, AcqHandle, -1)
    
    *定位
    
    *二值化
    threshold (Image, Regions, 115, 253)
    *形成不同的连通域
    connection (Regions, ConnectedRegions)
    *利用面积特征选择区域
    
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 45321.1, 200000)
    *获取区域的行列坐标
    area_center (SelectedRegions, Area, Row, Column)
    *获得区域的角度
    orientation_region (SelectedRegions, Phi)
    
    *仿射变换
    vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(90), HomMat2D)
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    affine_trans_region (SelectedRegions, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
    *抠图
    reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)
    
    
    
    * Measure 01: Code generated by Measure 01
    * Measure 01: Prepare measurement
    AmplitudeThreshold := 12
    RoiWidthLen2 := 21
    set_system ('int_zooming', 'true')
    * Measure 01: Coordinates for line Measure 01 [0]
    LineRowStart_Measure_01_0 := 244.681
    LineColumnStart_Measure_01_0 := 874.067
    LineRowEnd_Measure_01_0 := 241.673
    LineColumnEnd_Measure_01_0 := 1323.72
    * Measure 01: Convert coordinates to rectangle2 type
    TmpCtrl_Row := 0.5*(LineRowStart_Measure_01_0+LineRowEnd_Measure_01_0)
    TmpCtrl_Column := 0.5*(LineColumnStart_Measure_01_0+LineColumnEnd_Measure_01_0)
    TmpCtrl_Dr := LineRowStart_Measure_01_0-LineRowEnd_Measure_01_0
    TmpCtrl_Dc := LineColumnEnd_Measure_01_0-LineColumnStart_Measure_01_0
    TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
    TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
    TmpCtrl_Len2 := RoiWidthLen2
    * Measure 01: Create measure for line Measure 01 [0]
    * Measure 01: Attention: This assumes all images have the same size!
    gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1920, 1080, 'nearest_neighbor', MsrHandle_Measure_01_0)

    
    *设置绘制的方式
    dev_set_draw ('margin')
    gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1920, 1080, 'nearest_neighbor', MsrHandle_Measure_02_0)
    *获得测量矩形
    gen_rectangle2 (Rectangle, TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2)
    
    * Measure 01: Execute measurements
    measure_pos (Image, MsrHandle_Measure_01_0, 1, AmplitudeThreshold, 'all', 'all', Row_Measure_01_0, Column_Measure_01_0, Amplitude_Measure_01_0, Distance_Measure_01_0)
    * Measure 01: Do something with the results
    * Measure 01: Clear measure when done
    close_measure (MsrHandle_Measure_01_0)
    
    
    
endwhile
close_framegrabber (AcqHandle)

打开相机进行操作:

* Image Acquisition 01: Code generated by Image Acquisition 01
open_framegrabber ('DirectShow', 1, 1, 0, 0, 0, 0, 'default', 8, 'gray', -1, 'false', 'default', '[0] Web Camera', 0, -1, AcqHandle)
grab_image_start (AcqHandle, -1)
while (true)
    grab_image_async (Image, AcqHandle, -1)
    
    endwhile
close_framegrabber (AcqHandle)

基于Halcon学习的测量拟合----实战【一】_第1张图片

    *二值化
    threshold (Image, Regions, 115, 253)
    *形成不同的连通域
    connection (Regions, ConnectedRegions)
    *利用面积特征选择区域
    
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 45321.1, 200000)
    *获取区域的行列坐标
    area_center (SelectedRegions, Area, Row, Column)
    *获得区域的角度
    orientation_region (SelectedRegions, Phi)
    
    *仿射变换
    vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(90), HomMat2D)
    affine_trans_image (Image, ImageAffineTrans, HomMat2D, 'constant', 'false')
    affine_trans_region (SelectedRegions, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
    *抠图
    reduce_domain (ImageAffineTrans, RegionAffineTrans, ImageReduced)

基于Halcon学习的测量拟合----实战【一】_第2张图片

基于Halcon学习的测量拟合----实战【一】_第3张图片

 打开测量助手插入代码:

    * Measure 01: Code generated by Measure 01
    * Measure 01: Prepare measurement
    AmplitudeThreshold := 12
    RoiWidthLen2 := 21
    set_system ('int_zooming', 'true')
    * Measure 01: Coordinates for line Measure 01 [0]
    LineRowStart_Measure_01_0 := 244.681
    LineColumnStart_Measure_01_0 := 874.067
    LineRowEnd_Measure_01_0 := 241.673
    LineColumnEnd_Measure_01_0 := 1323.72
    * Measure 01: Convert coordinates to rectangle2 type
    TmpCtrl_Row := 0.5*(LineRowStart_Measure_01_0+LineRowEnd_Measure_01_0)
    TmpCtrl_Column := 0.5*(LineColumnStart_Measure_01_0+LineColumnEnd_Measure_01_0)
    TmpCtrl_Dr := LineRowStart_Measure_01_0-LineRowEnd_Measure_01_0
    TmpCtrl_Dc := LineColumnEnd_Measure_01_0-LineColumnStart_Measure_01_0
    TmpCtrl_Phi := atan2(TmpCtrl_Dr, TmpCtrl_Dc)
    TmpCtrl_Len1 := 0.5*sqrt(TmpCtrl_Dr*TmpCtrl_Dr + TmpCtrl_Dc*TmpCtrl_Dc)
    TmpCtrl_Len2 := RoiWidthLen2
    * Measure 01: Create measure for line Measure 01 [0]
    * Measure 01: Attention: This assumes all images have the same size!
    gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1920, 1080, 'nearest_neighbor', MsrHandle_Measure_01_0)
 
    * Measure 01: Execute measurements
    measure_pos (Image, MsrHandle_Measure_01_0, 1, AmplitudeThreshold, 'all', 'all', Row_Measure_01_0, Column_Measure_01_0, Amplitude_Measure_01_0, Distance_Measure_01_0)
    * Measure 01: Do something with the results
    * Measure 01: Clear measure when done
    close_measure (MsrHandle_Measure_01_0)

基于Halcon学习的测量拟合----实战【一】_第4张图片

添加显示测量矩形的代码: 

     gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1920, 1080, 'nearest_neighbor', MsrHandle_Measure_01_0)
    
    *设置绘制的方式
    dev_set_draw ('margin')
    gen_measure_rectangle2 (TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2, 1920, 1080, 'nearest_neighbor', MsrHandle_Measure_02_0)
    *获得测量矩形
    gen_rectangle2 (Rectangle, TmpCtrl_Row, TmpCtrl_Column, TmpCtrl_Phi, TmpCtrl_Len1, TmpCtrl_Len2)

基于Halcon学习的测量拟合----实战【一】_第5张图片


充电头绕着中心点旋转多少度,都能够自动矫正,并画出测量矩形

基于Halcon学习的测量拟合----实战【一】_第6张图片

基于Halcon学习的测量拟合----实战【一】_第7张图片

 

你可能感兴趣的:(Halcon学习笔记,图像识别,图像处理)