halcon封装measure_pos为测量直线函数

        measure_pos为halcon测量单个矩形内边缘点的算子,将多个算子用for循环一起使用的话,就可以形成类似metrology系列的算子。

        两者功能其实是差不多的,但是在有的时候筛选边缘点位的时候,会用到自己编写的规则,这个时候就需要使用measure_pos了。基于这种情况,所以我封装了这个函数。

函数封装:

 

 函数使用:
[RowBegin, ColumnBegin] [RowEnd, ColumnEnd] 测量直线的起始点
NumDistance(矩形之间间距) 

RecHeight(矩形半高)

RecWidth(矩形半宽)

Sigma(平滑系数)
Threshold(灰度值)

Transition(极性)

Select(线选择)

Direction(找线朝向)

        上述参数基本都是找线卡尺常规参数,只有最后这个Direction(找线朝向)是单加的。这个是控制卡尺找线方向的,可以直接更人性的使用从左往右,从右往左这样,更加简单直接。

函数代码: 

* 测量直线的起始点
* [RowBegin, ColumnBegin] [RowEnd, ColumnEnd]
* 参数设置
* NumDistance(矩形之间间距),RecHeight(矩形半高),RecWidth(矩形半宽),Sigma(平滑系数)
* Threshold(灰度值),Transition(极性),Select(线选择),Direction(找线朝向)
gen_empty_obj (RecContour)
RowEdges := []
ColumnEdges := []
RowEdgeSelect := []
ColumnEdgesSelect := []

try
    get_image_size (Image, Width, Height)
    distance_pp (RowBegin, ColumnBegin, RowEnd, ColumnEnd, Distance)
    line_orientation (RowBegin, ColumnBegin, RowEnd, ColumnEnd, Phi)
    
    for Index := 0 to Distance by NumDistance
        
        if (Direction == 'UtoD')
            if (Phi == 0 and ColumnBegin < ColumnEnd)
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi == 0 and ColumnBegin > ColumnEnd)
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif ((Phi > 0 and RowBegin > RowEnd) or (Phi < 0 and RowBegin < RowEnd))
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif ((Phi > 0 and RowBegin < RowEnd) or (Phi < 0 and RowBegin > RowEnd))
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]                        
            endif   
        elseif (Direction == 'DtoU')
            if (Phi == 0 and ColumnBegin < ColumnEnd)
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi == 0 and ColumnBegin > ColumnEnd)
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif ((Phi > 0 and RowBegin > RowEnd) or (Phi < 0 and RowBegin < RowEnd))
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif ((Phi > 0 and RowBegin < RowEnd) or (Phi < 0 and RowBegin > RowEnd))
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]      
            endif   
        elseif (Direction == 'LtoR')
            if (Phi == 0 and ColumnBegin < ColumnEnd)
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi == 0 and ColumnBegin > ColumnEnd)
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi > 0 and RowBegin > RowEnd)
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi > 0 and RowBegin < RowEnd)
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi < 0 and RowBegin < RowEnd)
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi < 0 and RowBegin > RowEnd)
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            endif   
        elseif (Direction == 'RtoL')
            if (Phi == 0 and ColumnBegin < ColumnEnd)
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi == 0 and ColumnBegin > ColumnEnd)
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi > 0 and RowBegin > RowEnd)
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            elseif (Phi > 0 and RowBegin < RowEnd)               
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi + rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]  
            elseif (Phi < 0 and RowBegin < RowEnd)
                RowCentre := RowBegin - Index * sin(Phi)
                ColumnCentre := ColumnBegin + Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]            
            elseif (Phi < 0 and RowBegin > RowEnd)
                RowCentre := RowBegin + Index * sin(Phi)
                ColumnCentre := ColumnBegin - Index * cos(Phi)
                gen_rectangle2 (Rectangle, RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth)
                gen_measure_rectangle2 (RowCentre, ColumnCentre, Phi - rad(90), RecHeight, RecWidth, Width, Height, 'nearest_neighbor', MeasureHandle)
                measure_pos (Image, MeasureHandle, Sigma, Threshold, Transition, Select, RowEdge, ColumnEdge, Amplitude, Distance1)
                gen_contour_region_xld (Rectangle, Contours, 'border')
                concat_obj (RecContour, Contours, RecContour)
                RowEdges := [RowEdges, RowEdge]
                ColumnEdges := [ColumnEdges, ColumnEdge]
            endif   
        endif              
        
    endfor
    
    
    * 显示点位
    gen_cross_contour_xld (Cross, RowEdges, ColumnEdges, 6, Phi + rad(45))
    concat_obj (RecContour, Cross, RecContour)   
    
    * 拟合直线    
    gen_contour_polygon_xld (Contour, RowEdges, ColumnEdges)
    fit_line_contour_xld (Contour, 'tukey', -1, 0, 5, 1, FitRowBegin, FitColBegin, FitRowEnd, FitColEnd, Nr, Nc, Dist)    
    gen_contour_polygon_xld (Line, [FitRowBegin, FitRowEnd], [FitColBegin, FitColEnd])
    
    
catch (Exception)
    RowEdges := []
    ColumnEdges := []
endtry

return ()

        这个函数整体封装的其实比较粗糙,但是一直没时间去优化,后续看有时间去优化一下。 

  • 9
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值