***********************************************Halcon案例***********************************************************

*这个例子演示了如何从空中提取道路。

*对于道路提取,假定道路由具有均匀灰度值和直线的两个平行边组成,首先提取空中图像中的道路中心,然后显示第一航空影像的放大部分的第二图像;

*读取和连续的道路边缘的轮廓,第二幅图像中的平行道路边缘和道路中心确定。最后把这些道路假设结合起来。

read_image (Image, 'mreut4_3')

threshold (Image, Region, 160, 255)

reduce_domain (Image, Region, ImageReduced) 

* 输入要提取的线宽,直线的对比度,计算参数并提取线

MaxLineWidth := 5

Contrast := 70

calculate_lines_gauss_parameters (MaxLineWidth, Contrast, Sigma, Low, High)

lines_gauss (ImageReduced, RoadCenters, Sigma, Low, High, 'light', 'true', 'bar-shaped', 'true')


* 读取第二个图像,显示第一个图像的放大图像部分

read_image (ImagePart, 'mreut_y')

get_image_size (ImagePart, PartWidth, PartHeight)

* 提取、分割轮廓

edges_image (ImagePart, PartAmp, PartDir, 'mderiche2', 0.3, 'nms', 20, 40)

threshold (PartAmp, EdgeRegion, 1, 255)

* 裁剪轮廓区域

clip_region (EdgeRegion, ClippedEdges, 2, 2, PartWidth - 3, PartHeight - 3)

*提取骨架

skeleton (ClippedEdges, EdgeSkeleton)

*转化为XLD轮廓

gen_contours_skeleton_xld (EdgeSkeleton, EdgeContours, 1, 'filter') 

* 轮廓-多边形(轮廓-直线段)

gen_polygons_xld (EdgeContours, EdgePolygons, 'ramer', 2)

* 通过提取平行的XLD多边形来确定道路的边缘。为了消除错选,提取的并行多边形被进一步修改和扩展。

* 提取所有平行XLD多边形

gen_parallels_xld (EdgePolygons, ParallelEdges, 10, 30, 0.15, 'true')

* 提取平行的XLD多边形包围一个均匀区域

mod_parallels_xld (ParallelEdges, ImagePart, ModParallelEdges, ExtParallelEdges, 0.3, 160, 220, 10)

* 通过应用仿射变换从第一张图像的提取的道路中心提取道路中心

* 生成一个齐次变换矩阵

hom_mat2d_identity (HomMat2DIdentity)

*缩放,x、y方向的缩放因子

hom_mat2d_scale (HomMat2DIdentity, 8, 8, 0, 0, HomMat2DScale)

*平移,x、y方向的平移

hom_mat2d_translate (HomMat2DScale, -250, -600, HomMat2DTranslate)

*将仿射变换应用到道路中心轮廓

affine_trans_contour_xld (RoadCenters, ScaledRoadCenters, HomMat2DTranslate)

* 修剪缩放的道路中心的轮廓然后用多边形来近似

clip_contours_xld (ScaledRoadCenters, ClippedRoadCenters, 0, 0, 512, 512)

gen_polygons_xld (ClippedRoadCenters, RoadCenterPolygons, 'ramer', 2)

*提取道路边

combine_roads_xld (EdgePolygons, ModParallelEdges, ExtParallelEdges, RoadCenterPolygons, RoadSides, rad(40), rad(20), 40, 40)