开发环境
- halcon-18.05.0.2-windowsx64位开发环境
- BumblebeeXB3相机(获取Disparity信息)
功能
- 实现对国标充电口的检测
- 得到充电口平面法向量
- 配合机械臂完成充电任务
代码如下:
*************************************************************************************
************************** FileName: 充电口识别 ******************************
************************** Author: YapethsDY *******************************
************************** Date: 2020/09/07 *******************************
*************************************************************************************
dev_update_off ()
*关闭窗体显示
dev_close_window ()
read_image (Image, 'C:/Users/siasun/Desktop/3.jpg')
*显示三种通道下的颜色
decompose3(Image, red, green, blue)
*得到图像大小
get_image_size (Image, Width, Height)
*定义窗体显示打开一个与图像等大小的背景为白色的窗口
*定义显示区域颜色外边框画笔宽度显示字体类型
dev_open_window (0, 0, Width, Height, 'white', WindowHandle)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_set_color ('yellow')
dev_set_draw ('margin')
dev_set_line_width (2)
dev_display(Image)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop()
*************************************hsi颜色空间*************************************
*得到hsi颜色空间分量
*get_hsi(WindowHandle,Hue, Saturation, Intensity)
*将一幅rgb图像转换到hsi颜色空间模型下
trans_from_rgb(red, green, blue, ImageHue, ImageSaturation, ImageIntensity, 'hsi')
dev_display(ImageHue) //颜色的频率 即色调
dev_display(ImageSaturation) //颜色的深浅程度 即饱和度
dev_display(ImageIntensity) //颜色的强度 即亮度
stop ()
****************************************ROI******************************************
*roi设置
Row1:=200
Column1:= 100
Row2:=500
Column2:=450
gen_rectangle1 (ROI, Row1, Column1, Row2, Column2)
*区域获取
reduce_domain (ImageHue, ROI, ImageROI)
****************************************滤波二值化************************************
*中值滤波
median_image(ImageROI, FilterImage, 'circle', 3, 'mirrored')
*均值滤波处理
*mean_image(ImageROI,FilterImage, 3, 3)
dev_display(FilterImage)
*二值化处理 自动阈值效果最好
*auto_threshold(ImageMedian, Region, 4)
*区域选择
*select_shape(Region, SelectedRegions, 'circularity', 'and', 0, 99999)
*boundary(SelectedRegions, RegionBorder, 'inner_filled')
*膨胀腐蚀处理
*dilation_circle(RegionBorder, RegionDilation, 3)
*union1(RegionDilation, RegionUnion)
*区域连接
*connection(Region, ConnectedRegions)
********************************以上使用效果不太好*************************************
*灰度处理
rgb1_to_gray(FilterImage,GrayImage)
*亚像素边缘梯度算子 alpha值越小 边缘信息越多 canny特性
edges_sub_pix(GrayImage, Edges, 'canny', 1, 65, 70)
*分割XLD轮廓
segment_contours_xld(Edges, ContoursSplit, 'lines_circles', 4, 4, 4)
*根据特征选择轮廓
select_contours_xld (ContoursSplit, SelectedContours, 'contour_length', 60, 90, -0.5, 0.5)
count_obj (SelectedContours, NumberContours)
gen_empty_obj (Circles)
for i := 1 to NumberContours by 1
select_obj (SelectedContours, ObjectSelected, i)
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
if (Attrib == 1)
concat_obj (Circles, ObjectSelected, Circles)
endif
endfor
*合并共圆轮廓
union_cocircular_contours_xld (Circles, UnionContours, rad(60), rad(10), rad(30), 100, 50, 10, 'true', 1)
dev_clear_window ()
dev_set_color ('black')
dev_display (UnionContours)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
count_obj (UnionContours, NumberCircles)
CenterRow := []
CenterColumn := []
dev_clear_window ()
dev_set_color ('black')
set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
dev_display (SelectedContours)
for i := 1 to NumberCircles by 1
select_obj (UnionContours, ObjectSelected, i)
*圆拟合xld轮廓
fit_circle_contour_xld (ObjectSelected, 'atukey', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
*创建一个或多个闭合圆弧
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.5)
dev_set_color ('red')
dev_display (ContCircle)
if (i == 1)
if(Radius<14 and Radius>13)
Row2 := Row + Radius * sin(rad(-45))
Column2 := Column + Radius * cos(rad(-45))
set_tposition (WindowHandle, Row2 - 35, Column2 + 5)
disp_arrow (WindowHandle, Row, Column, Row2, Column2, 2)
write_string (WindowHandle, i)
endif
endif
if (i > 1)
exist := 0
for j := 0 to i - 2 by 1
distance_pp (Row, Column, CenterRow[j], CenterColumn[j], DistanceCenters)
if (DistanceCenters < 20)
exist := 1
endif
endfor
if (exist == 1)
if(Radius<14 and Radius>13)
Row2 := Row + Radius * sin(rad(-135))
Column2 := Column + Radius * cos(rad(-135))
set_tposition (WindowHandle, Row2 - 40, Column2 - 30)
disp_arrow (WindowHandle, Row, Column, Row2, Column2, 2)
write_string (WindowHandle, i)
endif
else
if(Radius<14 and Radius>13)
Row2 := Row + Radius * sin(rad(-45))
Column2 := Column + Radius * cos(rad(-45))
set_tposition (WindowHandle, Row2 - 35, Column2 + 5)
disp_arrow (WindowHandle, Row, Column, Row2, Column2, 2)
write_string (WindowHandle, i)
endif
endif
endif
if (i < 8)
if(Radius<14 and Radius>13)
disp_message (WindowHandle, 'R' + i + ' = ' + Radius$'.4', 'window', i * 20, 130, 'black', 'false')
endif
else
if(Radius<14 and Radius>13)
disp_message (WindowHandle, 'R' + i + ' = ' + Radius$'.4', 'window', (i - 7) * 20, 400, 'black', 'false')
endif
endif
CenterRow := [CenterRow,Row]
CenterColumn := [CenterColumn,Column]
endfor
dev_update_window ('on')
该图为测试图片
结果图
explain
- 获取了RGB三通道
- 转换了HSIy颜色空间模型(H:色调S:饱和度I:亮度)
- 膨胀腐蚀区域连接等形态学处理
- ROI处理
- XLD匹配
- triclops三维视差获取无硬件支持无法出效果图
- 计算最终演示视频后续更新