Halcon提取中心线

Halcon中roads.hdev的学习标注及与之相关的提取中心线

* This example demonstrates how to extract roads from an aerial
* image.
* For the road extraction it is assumed that a road consists of
* two parallel edges with homogeneous gray values and a line
* segment in between.
* First the road centers in an aerial image are extracted. Then
* a second image showing a zoomed part of the first aerial image
* is read and successively the contours of the road edges,
* parallel road edges and the road centers in the second image are
* determined. At the end these road hypotheses are combined and
* the road sides in the image are extracted.
* 
* 
*定义程序执行打开和关闭期间,图像对象是否在图形窗口中显示
*将dev_update_pc,dev_update_var和dev_update窗口切换到“关闭”。
*一般dev_update_off放在开始,dev_update_on放在程序结束
dev_update_off ()
dev_close_window ()
dev_close_window ()
* 
* Read an aerial image
read_image (Image, 'mreut4_3')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle1)
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
dev_display (Image)
disp_message (WindowHandle1, 'Extract the roads from an aerial image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 
* Segment the image and reduce the domain
*reduce_domain ( Image, Region : ImageReduced : : ) 
*其中,Image是输入的图像;Region是输入的区域;ImageReduced是输出的图像,是Image中Region的那部分图像。
*通过reduce_domain确实能获得特定区域Region位置的图像,但是,reduce_domain是缩小一个图像的定义域,并不缩小图像的实际尺寸,
*即新图像ImageReduced尺寸大小并未发生变化。如果使用get_image_size来计算ImageReduced图像的尺寸,其尺寸和 原图Image一样。
threshold (Image, Region, 160, 255)
reduce_domain (Image, Region, ImageReduced)
* 
* Detect the lines that represent the road centers
*MaxLineWidth (input_control) : lines_gauss要提取线条的最大宽度
*对于参数Contrast 需要说明的是,如果只选择一个值时,最小对比度将会默认为最大对比度的1/3,
*最小对比度越小,线条将会延伸到对比度较低的区域,即线条越长。反之,值越高,线条越短,但越突出。
*lines_gauss 主要功能是提取图像上的线条,提取的结果属于亚像素精度的XLD轮廓
*深入了解改算子请自行百度halcon lines_gauss算子详解:https://blog.csdn.net/qq_18620653/article/details/105446922
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')
dev_set_color ('red')
dev_display (Image)
dev_display (RoadCenters)
disp_message (WindowHandle1, 'Road centers', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 
* Read a second image showing a zoomed image part
* of the first image
read_image (ImagePart, 'mreut_y')
get_image_size (ImagePart, PartWidth, PartHeight)
dev_resize_window_fit_image (ImagePart, 0, 0, -1, -1)
*图像自适应窗口
dev_set_part (0, 0, PartWidth - 1, PartHeight - 1)
dev_set_line_width (2)
dev_display (ImagePart)
disp_message (WindowHandle1, 'Zoomed image part of the aerial image', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 
* Extract the edge contours
* -------------------------
* Extract and segment edges
edges_image (ImagePart, PartAmp, PartDir, 'mderiche2', 0.3, 'nms', 20, 40)
threshold (PartAmp, EdgeRegion, 1, 255)
* 
* Clip the edge region, compute the skeleton
* and convert it into XLD contours
*剪切区域
*计算骨架,即输入区域的中间轴
*骨架的构造方式是,它上面的每个点都可以被看作一个半径最大的圆的中心点,
*同时这个中心又完全包含在该区域内。
*gen_contours_skeleton_xld将输入骨架(例如边)转换成XLD轮廓,
*该骨架被认为包含一个像素宽的区域(参见 函数skeleton)。
clip_region (EdgeRegion, ClippedEdges, 2, 2, PartWidth - 3, PartHeight - 3)
skeleton (ClippedEdges, EdgeSkeleton)
gen_contours_skeleton_xld (EdgeSkeleton, EdgeContours, 1, 'filter')
* 
* Approximate the contours by polygons
gen_polygons_xld (EdgeContours, EdgePolygons, 'ramer', 2)
* 
* Display the road edges
dev_set_color ('blue')
dev_display (ImagePart)
dev_display (EdgePolygons)
disp_message (WindowHandle1, 'Approximated edges', 'window', 12, 12, 'blue', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 
* Determine road edges
* -----------------------------------------------------
* Determine the road edges by extracting parallel XLD
* polygons. To eliminate wrong candidates, the extracted
* parallel polygons are further modified and extended.
* 
* Extract all parallel XLD polygons
gen_parallels_xld (EdgePolygons, ParallelEdges, 10, 30, 0.15, 'true')
* 
* Modify the parallel XLD polygons to obtain only those XLD
* polygons with a quality factor > 0.3 (ModParallelRoadEdges).
* These polygons are then extended by adjacent polygon segments
* that enclose a homogeneous area (ExtParallelRoadEdges).
*提取一个包括同质区域的并行XLD多边形(polygon)
mod_parallels_xld (ParallelEdges, ImagePart, ModParallelEdges, ExtParallelEdges, 0.3, 160, 220, 10)
* 
* Display the parallel road edges
dev_set_color ('green')
dev_display (ModParallelEdges)
disp_message (WindowHandle1, 'Parallel edges', 'window', 35, 12, 'forest green', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
dev_set_color ('orange')
dev_display (ExtParallelEdges)
disp_message (WindowHandle1, 'Extended parallel edges', 'window', 35, 12, 'orange', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 
* Extract the road centers
* --------------------------------------------------------
* Extract the road centers from the extracted road centers
* of the first image by applying an affine transformation
* 
* Generate a homogeneous transformation matrix
* and then add a scaling and a translation
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_scale (HomMat2DIdentity, 8, 8, 0, 0, HomMat2DScale)
hom_mat2d_translate (HomMat2DScale, -250, -600, HomMat2DTranslate)
* 
* Apply the affine transformation to the road center contours
affine_trans_contour_xld (RoadCenters, ScaledRoadCenters, HomMat2DTranslate)
* 
* Clip the contours of the scaled road centers
* and then approximate them by polygons
*clip_contours_xld裁剪在Contours中给定的所有XLD轮廓,
*即仅在输出中返回Row1,Column1,Row2和Column2给定的矩形中包含的轮廓点。 
*请注意,矩形的行为就像一个区域,即矩形完全包围了像素。 
*如有必要,可以分割轮廓,并生成几个新的轮廓。 生成的轮廓将在ClippedContours中返回。
clip_contours_xld (ScaledRoadCenters, ClippedRoadCenters, 0, 0, 512, 512)
gen_polygons_xld (ClippedRoadCenters, RoadCenterPolygons, 'ramer', 2)
* 
* Display the road centers
dev_set_color ('red')
dev_display (RoadCenterPolygons)
disp_message (WindowHandle1, 'Road centers', 'window', 58, 12, 'red', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 
* Extract road sides
*提取路边
* ------------------
combine_roads_xld (EdgePolygons, ModParallelEdges, ExtParallelEdges, RoadCenterPolygons, RoadSides, rad(40), rad(20), 40, 40)
* 
* Display the road sides in a second window
dev_open_window (0, PartWidth + 10, 512, 512, 'black', WindowHandle2)
dev_set_part (0, 0, PartWidth - 1, PartHeight - 1)
set_display_font (WindowHandle2, 16, 'mono', 'true', 'false')
dev_set_line_width (2)
dev_set_color ('green')
dev_display (ImagePart)
dev_display (RoadSides)
disp_message (WindowHandle2, 'Extracted road sides', 'window', 12, 12, 'black', 'true')
* 
* Display the edge contours, extended parallel edges
* and road centers again for visualization
dev_set_window (WindowHandle1)
dev_display (ImagePart)
dev_set_color ('blue')
dev_display (EdgePolygons)
dev_set_color ('orange')
dev_display (ExtParallelEdges)
dev_set_color ('red')
dev_display (RoadCenterPolygons)
disp_message (WindowHandle1, 'Approximated edges', 'window', 12, 12, 'blue', 'true')
disp_message (WindowHandle1, 'Extended parallel edges', 'window', 35, 12, 'orange', 'true')
disp_message (WindowHandle1, 'Road centers', 'window', 58, 12, 'red', 'true')

 提取条纹中心线时有一下两种方法:

1.line_gauss

2.skeleton

1.

*关闭窗口、导入图片、转灰度
dev_close_window ()
read_image (Image, '激光线.png')
rgb1_to_gray(Image, GrayImage)
*gauss平滑
gauss_filter (GrayImage, C6, 5)
get_image_size (C6, Width, Height)
*图像灰度值翻转,阈值分割
invert_image (C6, ImageInvert)
threshold (ImageInvert, Region, 105, 148)

* binary_threshold (C6, Region, 'smooth_histo', 'light', UsedThreshold)
* complement (Region, RegionComplement)
*提取区域横纵坐标的数组
get_region_contour (Region, Rows, Columns)
smallest_rectangle1 (Region, Row1, Column1, Row2, Column2)
*裁剪图像
reduce_domain(C6,Region,Mask)
crop_domain(Mask, ROI)
write_image(ROI, 'bmp', 0, '1.bmp')
read_image (Angio, '1.bmp')
get_image_size (Angio, Width, Height)
dev_open_window (0, 0,  Width/3 , Height/3, 'black', WindowID)
dev_display (Angio)
dev_set_color ('blue')
MaxLineWidth := 15
Contrast := 55
*该步可以提取出中心线
calculate_lines_gauss_parameters (MaxLineWidth, [Contrast,0], Sigma, Low, High)
lines_gauss (Angio, Lines, Sigma, Low, High, 'dark', 'true', 'parabolic', 'true')
* get_contour_xld (Lines, a, b)
count_obj (Lines, Number)
*检测有几条曲线*
dev_update_pc ('off')
dev_update_var ('off')
for I := 1 to Number by 1
    select_obj (Lines, Line, I)
    get_contour_xld (Line, Row, Col)
    get_contour_attrib_xld (Line, 'angle', Angle)
    get_contour_attrib_xld (Line, 'width_left', WidthL)
    get_contour_attrib_xld (Line, 'width_right', WidthR)
    * To display the lines, the point at which the gray value drops to
    * 25% of the contrast between the line and the background will be
    * displayed.  This point is given by sqrt(3/4) for the parabolic
    * line model.
    *计算左右两边轮廓
    RowR := Row + cos(Angle) * WidthR * sqrt(0.75)
    ColR := Col + sin(Angle) * WidthR * sqrt(0.75)
    RowL := Row - cos(Angle) * WidthL * sqrt(0.75)
    ColL := Col - sin(Angle) * WidthL * sqrt(0.75)
    dev_set_color ('red')
    dev_display (Line)
    dev_set_color ('green')
    disp_polygon (WindowID, RowL, ColL)
    disp_polygon (WindowID, RowR, ColR)
endfor
gen_cross_contour_xld(Cross, Row[1], Col[1],6, 0.785398)

2.

*读入图像并转为灰度图
read_image (Image, '激光线.png')
rgb1_to_gray (Image, GrayImage)
*图像预处理部分在这个里面没有用
* rgb1_to_gray (Image, GrayImage)
* emphasize (GrayImage, ImageEmphasize, Width, Height, 1)
* dev_display (ImageEmphasize)
* invert_image (ImageEmphasize, ImageInvert)
* gauss_filter (ImageInvert, ImageGauss, 5)
* dev_display (ImageGauss)
* threshold (ImageGauss, Regions, 98, 146)
* complement (Regions, RegionComplement)
* connection (RegionComplement, ConnectedRegions)
* select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
*自动阈值分割提取区域后闭运算,不然提取出的线非常惨不忍睹
binary_threshold (GrayImage, Region, 'smooth_histo', 'light', UsedThreshold)
gen_circle (Circle, 10, 10, 10)
closing (Region, Circle, RegionClosing)
*提取区域的骨架
skeleton (Region, Skeleton)
gen_contours_skeleton_xld (Skeleton, Contours, 1, 'filter')
area_center (Skeleton, Area, Row, Column)
*显示提取出的中心线
dev_clear_window ()
dev_display(Contours)
dev_display (Contours)

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值