[halcon案例2] 足球场的提取和射影变换

[halcon案例2] 足球场的提取和射影变换_第1张图片

[halcon案例2] 足球场的提取和射影变换_第2张图片

一、提要 

        值得注意的是,在自然环境中,提取对象的算法都不是普适的,是有条件的。针对某种特殊的属性设计算法算法。本例是对足球场进行提取,其对应的特殊条件是:足球场是矩形、足球场的草坪是绿的。而针对矩形物需要矩形匹配,着重找出四个边角,针对绿色,需要颜色匹配。

       

二、算法要点描述

        这是通过提取四个从体育场的斜视图看足球场的角落。四个角定义一个四边形,然后映射到一个矩形具有正确的纵横比。如果场的尺寸是已知度量,该程序可以扩展为执行度量在足球场和体育场跑道上进行测量。

2.1 绿颜色矩形区域的提取

1)加载彩色图像并显示

dev_update_off ()
read_image (Image, 'olympic_stadium')
dev_display (Image)

2)将RGB转化成HSV,其中H在(60,90)范围是绿色

decompose3 (Image, ImageR, ImageG, ImageB)
trans_from_rgb (ImageR, ImageG, ImageB, ImageH, ImageS, ImageV, 'hsv')
threshold (ImageH, GreenRegion, 60, 90)
connection (GreenRegion, ConnectedRegions)

此处RGB<-->HSV的知识,可以参考这个地址。 

3)从粗选区域中选出满足的区域

  • 80%近似矩形的,
  • 面积大于190像素的区域,
  • 并取出凸包含,
  • 取出边缘。
  • 边缘膨胀7个像素。 
select_shape_std (ConnectedRegions, RectangleRegions, 'rectangle2', 80)
select_shape_std (RectangleRegions, Field, 'max_area', 190)
shape_trans (Field, FieldConvex, 'convex')
boundary (FieldConvex, FieldBorder, 'inner')
dilation_rectangle1 (FieldBorder, FieldDilation, 7, 7)

[halcon案例2] 足球场的提取和射影变换_第3张图片

4)精度边界提取出来,将原图贴回,提取亚像素边界。

reduce_domain (Image, FieldDilation, ImageReduced)
edges_color_sub_pix (ImageReduced, Edges, 'canny', 1.5, 20, 40)

算子解释: edges_color_sub_pix。

  • edges_color_sub_pix(Image : Edges : Filter, Alpha, Low, High : )

         edge_color_sub_pix 从输入图像Image中提取亚像素精确的颜色边缘。颜色边缘的定义在edges_color的描述中给出。可以选择与edges_color中相同的边缘过滤器:'canny'、'deriche1'、'deriche2'和' shen'。此外,可以使用'sobel_fast'选择快速Sobel过滤器。

        可以任意选择“过滤器宽度”(即平滑量)。有关此参数的详细说明,请参阅edges_color。过滤器='sobel_fast'时忽略此参数。

        提取的边缘在 Edges 中作为亚像素精确 XLD 轮廓返回。对于除“sobel_fast”之外的所有边缘算子,为每个边缘点定义以下属性(参见 get_contour_attrib_xld):

5)边界线回归

select_shape_xld (Edges, SelectedEdges, 'contlength', 'and', 15, 500)
segment_contours_xld (SelectedEdges, ContoursSplit, 'lines', 5, 4, 2)
regress_contours_xld (ContoursSplit, RegressContours, 'no', 1)

6)将矩形的圆角消除

* Weed out edges that are not straight enough.
select_contours_xld (RegressContours, SelectedContours, 'curvature', 0, 0.5, 0, 0)
* Select edges that are long enough to provide meaningful direction information.
select_shape_xld (SelectedContours, SelectedEdges, 'contlength', 'and', 15, 500)


select_contours_xld(Contours : SelectedContours : Feature, Min1, Max1, Min2, Max2 : )

select_shape_xld (SelectedContours, SelectedEdges, 'contlength', 'and', 15, 500)

union_collinear_contours_xld (SelectedEdges, UnionContours, 30, 1, 4, 0.1, 'attr_forget')

select_shape_xld (UnionContours, HorizontalEdges, 'phi', 'and', rad(-20), rad(20))
select_shape_xld (UnionContours, VerticalEdges, ['phi','phi'], 'or', [rad(-90),rad(60)], [rad(-60),rad(90)])


fit_line_contour_xld (HorizontalEdges, 'tukey', -1, 0, 10, 2, RowBegHor, ColBegHor, RowEndHor, ColEndHor, NrHor, NcHor, DistHor)
IndexHor := sort_index(RowBegHor)
fit_line_contour_xld (VerticalEdges, 'tukey', -1, 0, 10, 2, RowBegVer, ColBegVer, RowEndVer, ColEndVer, NrVer, NcVer, DistVer)
IndexVer := sort_index(ColBegVer)

intersection_lines (RowBegHor[IndexHor[0]], ColBegHor[IndexHor[0]], RowEndHor[IndexHor[0]], ColEndHor[IndexHor[0]], RowBegVer[IndexVer[0]], ColBegVer[IndexVer[0]], RowEndVer[IndexVer[0]], ColEndVer[IndexVer[0]], RowUL, ColUL, IsOverlapping)

gen_contour_polygon_xld (FieldBorder, [RowUL,RowUR,RowLR,RowLL,RowUL], [ColUL,ColUR,ColLR,ColLL,ColUL])


vector_to_proj_hom_mat2d ([RowUL,RowUR,RowLR,RowLL] + 0.5, [ColUL,ColUR,ColLR,ColLL] + 0.5, [160,160,340,340] + 0.5, [250,550,550,250] + 0.5, 'normalized_dlt', [], [], [], [], [], [], HomMat2D, Covariance)
projective_trans_image_size (Image, TransImage, HomMat2D, 'bilinear', 800, 500, 'false')
projective_trans_contour_xld (FieldBorder, FieldBorderTrans, HomMat2D)

代码实例

你可能感兴趣的:(机器视觉,Halcon,视觉检测)