Halcon轮廓提取

 简介:

              对目标区域进行轮廓提取,可以用于判定形状等。

 

介绍:

boundary(获取一个区域的边界)
Region (input_object)   Regions for which the boundary is to be computed. 区域
RegionBorder (output_object) Resulting boundaries. 输出的边界
BoundaryType (input_control)

Boundary type.
Default value: 'inner'
List of values: 'inner', 'inner_filled', 'outer'

linner 内边界。
inner_filled 内边界上的孔。
outer 外边界。
边界类型
gen_contour_region_xld(生成区域的XLD轮廓)
Regions (input_object)   Input regions. 区域
Contours (output_object)  Resulting contours. 输出的边界
Mode (input_control)  

Mode of contour generation.
Default value: 'border'
List of values: 'border', 'border_holes', 'center'

border 边界像素的外边界作为轮廓点。
border_holes 除了输入区域的外边界,还可以得到所有孔的轮廓。
center 边界像素的中心用作轮廓点。
轮廓生成方式
smallest_rectangle2_xld(任意方向的轮廓或多边形的最小外接矩形)
XLD (input_object) Contours or polygons to be examined. XLD
Row (output_control) Row coordinate of the center point of the enclosing rectangle. 输出的行坐标
Column (output_control)   Column coordinate of the center point of the enclosing rectangle. 输出的列坐标
Phi (output_control)   Orientation of the enclosing rectangle (arc measure).
Assertion: ((- pi / 2) < Phi) && (Phi <= (pi / 2))
弧度
Length1 (output_control)   First radius (half length) of the enclosing rectangle.
Assertion: Length1 >= 0.0
第一个边长
Length2 (output_control)   Second radius (half width) of the enclosing rectangle.
Assertion: (Length2 >= 0.0) && (Length2 <= Length1)
第二个边长
gen_rectangle2_contour_xld(创建一个矩形形状的XLD轮廓)
Rectangle (output_object)   Rectangle contour. 输出的矩形轮廓
Row (input_control)   Row coordinate of the center of the rectangle.
Default value: 300.0
行坐标
Column (input_control)   Column coordinate of the center of the rectangle.
Default value: 200.0
列坐标
Phi (input_control)   Orientation of the main axis of the rectangle [rad].
Default value: 0.0
Restriction: ((- pi / 2) < Phi) && (Phi <= (pi / 2))
弧度
Length1 (input_control)   First radius (half length) of the rectangle.
Default value: 100.5
第一个边长
Length2 (input_control) Second radius (half width) of the rectangle.
Default value: 20.5
第二个边长
segment_contours_xld(将XLD分割成线段和圆弧或椭圆圆弧)
Contours (input_object)  Contours to be segmented. 要分割的轮廓
ContoursSplit (output_object)   Segmented contours. 输出的分割轮廓
Mode (input_control)  Mode for the segmentation of the contours.
Default value: 'lines_circles'
List of values: 'lines', 'lines_circles', 'lines_ellipses'
轮廓分割模式
SmoothCont (input_control) Number of points used for smoothing the contours.
Default value: 5
Suggested values: 0, 3, 5, 7, 9
Restriction: (SmoothCont == 0) || ((SmoothCont >= 3) && odd(SmoothCont))
用于平滑轮廓的点的数目
MaxLineDist1 (input_control)   Maximum distance between a contour and the approximating line (first iteration).
Default value: 4.0
Suggested values: 1.0, 1.5, 2.0, 2.5, 3.0, 3.5
Restriction: MaxLineDist1 >= 0.0
第一次迭代时轮廓线与近似线之间的最大距离
MaxLineDist2 (input_control)  

Maximum distance between a contour and the approximating line (second iteration).
Default value: 2.0
Suggested values: 1.0, 1.5, 2.0, 2.5, 3.0, 3.5
Restriction: MaxLineDist2 >= 0.0

第二次迭代时轮廓线与近似线之间的最大距离
select_contours_xld(根据特征选择XLD轮廓)
Contours (input_object)  Input XLD contours. XLD轮廓
SelectedContours (output_object)  Output XLD contours. 输出的XDL轮廓
Feature (input_control)  

Feature to select contours with.
Default value: 'contour_length'
List of values: 'closed', 'contour_length', 'curvature', 'direction', 'maximum_extent', 'open'

closed 只返回起点和终点之间的距离小于或等于Max1像素的轮廓。(Min1、Min2和Max2在这里没有影响。)
contour_length 所有长度小于Min1或大于Max1的轮廓都不会返回(Min2和Max2在这里没有影响)。
curvature 只返回与回归线的平均距离在Min1和Max1之间、距离的标准差在Min2和Max2之间的轮廓。
direction 只返回回归线方向在Min1和Max1之间的等值线(以弧度为单位,逆时针方向)。Min1和Max1映射到[0,2*PI]的范围。(Min2和Max2在这里没有影响)。
maximum_extent 轮廓中最大的长度小于Min1或大于Max1不返回(Min2和Max2在这里没有影响)。
open 只返回起点和终点之间的距离大于Min1像素的轮廓线。(Max1、Min2和Max2在这里没有影响)。
特征选择
Min1 (input_control)   Lower threshold.
Default value: 0.5
最小值1
Max1 (input_control)   Upper threshold.
Default value: 200.0
最大值1
Min2 (input_control)   Lower threshold.
Default value: -0.5
最小值2
Max2 (input_control)   Upper threshold.
Default value: 0.5
最大值2
sort_contours_xld(轮廓排序)
Contours (input_object)  Contours to be sorted. 要排序的轮廓
SortedContours (output_object)  Sorted contours. 输出的排序轮廓
SortMode (input_control)  

Kind of sorting.
Default value: 'upper_left'
List of values: 'character', 'lower_left', 'lower_right', 'upper_left', 'upper_right'

character 位置由周围矩形的左上角决定。与'upper_left'相反,如果轮廓线与参数RowOrCol指定的坐标方向重叠,轮廓线也会根据剩余坐标进行排序。
lower_left 位置由周围矩形的左上角决定。
lower_right 位置由周围矩形的右上角决定。
upper_left 位置由周围矩形的左下角决定。
upper_right 位置由周围矩形的右下角决定。
排序种类
Order (input_control)  Increasing or decreasing sorting order.
Default value: 'true'
List of values: 'false', 'true'
减或增序
RowOrCol (input_control)   Sorting first with respect to row, then to column.
Default value: 'row'
List of values: 'column', 'row'
行或列
get_contour_global_attrib_xld(返回XLD轮廓的全局属性值)
Contour (input_object)  Input XLD contour. XLD轮廓
Name (input_control) Name of the attribute.
Default value: 'regr_norm_row'
Suggested values: 'regr_norm_row', 'regr_norm_col', 'regr_mean_dist', 'regr_dev_dist', 'cont_approx', 'bright_dark', 'is_hole'
属性名
Attrib (output_control)   Attribute values. 属性值
fit_line_contour_xld(用线段拟合XLD轮廓)
Contours (input_object)  Input contours. XLD轮廓
Algorithm (input_control)   Algorithm for the fitting of lines.
Default value: 'tukey'
List of values: 'drop', 'gauss', 'huber', 'regression', 'tukey'
drop “最小二乘”线拟合,其中的异常值被忽略。其中,所有距轮廓线较远的轮廓点,均忽略其到回归线的平均距离乘上(ClippingFactor),计算无畸变回归线。
gauss 加权“最小二乘”直线拟合,根据所有轮廓点到近似线距离的均值和标准差,减小异常值的影响。
huber 加权“最小二乘”线拟合,采用Huber方法减小了异常值的影响。
regression 标准最小二乘直线拟合。
tukey 加权“最小二乘”线拟合,基于Tukey方法忽略异常值。
直线拟合算法
MaxNumPoints (input_control)  

Maximum number of contour points used for the computation (-1 for all points).
Default value: -1
Restriction: MaxNumPoints >= 2

用于计算的最大轮廓点数量(-1表示所有点)
ClippingEndPoints (input_control) Number of points at the beginning and at the end of the contours to be ignored for the fitting.
Default value: 0
Restriction: ClippingEndPoints >= 0
拟合时忽略轮廓开始点和结束点的个数
Iterations (input_control)   Maximum number of iterations (unused for 'regression').
Default value: 5
Restriction: Iterations >= 0
最大迭代次数(regression不使用)
ClippingFactor (input_control) Clipping factor for the elimination of outliers (typical: 1.0 for 'huber' and 'drop' and 2.0 for 'tukey').
Default value: 2.0
List of values: 1.0, 1.5, 2.0, 2.5, 3.0
Restriction: ClippingFactor > 0
裁剪因子用于消除异常值(典型的:“huber”和“drop”时使用1,“tukey”时使用2)
RowBegin (output_control) Row coordinates of the starting points of the line segments. 线段起始点的行坐标
ColBegin (output_control)   Column coordinates of the starting points of the line segments. 线段起始点的列坐标
RowEnd (output_control)   Row coordinates of the end points of the line segments. 线段结束点的行坐标
ColEnd (output_control)   Column coordinates of the end points of the line segments. 线段结束点的列坐标
Nr (output_control) Line parameter: Row coordinate of the normal vector. 线参数:向量的行坐标
Nc (output_control)   Line parameter: Column coordinate of the normal vector. 线参数:向量的列坐标
Dist (output_control)  Line parameter: Distance of the line from the origin. 线参数:直线到原点的距离
gen_contour_polygon_xld(根据多边形的参数生成XDL轮廓)
Contour (output_object) Resulting contour. 输出的轮廓
Row (input_control)   Row coordinates of the polygon.
Default value: [0,1,2,2,2]
Suggested values: 0, 1, 2, 3, 4, 5, 10, 20, 50, 100, 200, 500
行坐标
Col (input_control)  Column coordinates of the polygon.
Default value: [0,0,0,1,2]
Suggested values: 0, 1, 2, 3, 4, 5, 10, 20, 50, 100, 200, 500
列坐标

 

使用:

  1. 彩色图像要转成灰色图像。
  2. 通过阈值等方法获取目标区域。
  3. 获取区域的边界轮廓。
  4. 规则的形状可以使用最小外界矩形等获取形状的信息后画出形状。
  5. 不规则的可以对轮廓分段、选择、排序。获取轮廓线的属性,根据属性,获取坐标等然后拟合线(圆或椭圆)。
read_image (Image, 'E:/照片/轮廓.png')
decompose3 (Image, Image1, Image2, Image3)
trans_from_rgb (Image1, Image2, Image3, ImageResult1, ImageResult2, ImageResult3, 'hsv')
auto_threshold (ImageResult3, Regions, 2)
area_center (Regions, Area, Row, Column)
connection (Regions, ConnectedRegions)
count_obj (ConnectedRegions, Number)

dev_set_draw ('margin')
for Index := 1 to Number by 1
    select_obj (ConnectedRegions, ObjectSelected, Index)
    area_center (ObjectSelected, Area1, Row1, Column1)
    *提取感兴趣的特征
    if (Area1 > 300 and Area1 < 500)
        dev_clear_window()
        dev_display (ObjectSelected)      
        boundary (ObjectSelected, RegionBorder, 'outer')     
        gen_contour_region_xld (RegionBorder, Contours, 'border')
 
        *规则形状-矩形
        smallest_rectangle2_xld (Contours, RowRec, ColumnRec, Phi1, LengthRec1, LengthRec2)
        gen_rectangle2_contour_xld (Rectangle, RowRec, ColumnRec, Phi1, LengthRec1, LengthRec2)
        
        *不规则形状-多边形
        segment_contours_xld (Contours, ContoursSplit, 'lines', 0, 0, 0)
        select_contours_xld (ContoursSplit, SelectedContours, 'contour_length', 0.5, 200, -0.5, 0.5)       
        sort_contours_xld (SelectedContours, SortedContours, 'upper_left', 'true', 'row')
        
        count_obj (SortedContours, Number1)
        gen_empty_obj (Lines)
        for Index1 := 1 to Number1 by 1
            select_obj (SortedContours, ObjectSelected1, Index1)
            get_contour_global_attrib_xld (ObjectSelected1, 'cont_approx', Attrib)
            *-1:线 0:椭圆 1:圆
            if (Attrib == -1)
                fit_line_contour_xld (ObjectSelected1, 'tukey', 2, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
                gen_contour_polygon_xld (Polygon, [RowBegin,RowEnd], [ColBegin,ColEnd])
                concat_obj (Lines, Polygon, Lines)   
            endif
        endfor       
        break
    endif
endfor
dev_clear_window ()
dev_display (Lines)


 

Halcon轮廓提取_第1张图片 轮廓.png
Halcon轮廓提取_第2张图片 矩形

 

Halcon轮廓提取_第3张图片 不规则

 

 

你可能感兴趣的:(halcon,轮廓,边界,XLD,拟合,Halcon,-,集合,Halcon)