Halcon可以利用深度图做相关检测,也可以直接利用点云数据做检测。但是如果是利用点云数据进行检测,PCL更合适。本文写的是利用深度图检测的方法。
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
dev_set_draw ('margin')
*X,Y,Z轴的分辨率
XResolution:=0.03
YResolution:=0.03
ZResolution:=0.0002
*0-65535是深度图的灰度值范围
*32768是倾斜校正的加系数(可以理解为高度为0,水平面的高度)
read_image (InputImage, 'imgTest.tiff')
********倾斜校正,生成校正后的图像OutputImage,*把倾斜平面矫正到水平面,进行后续的3D测量*********************
*绘制基准区域(需要被测量的区域,做倾斜矫正)
gen_rectangle1 (ROI_0, 183.411, 410.389, 242.444, 432.361)
gen_rectangle1 (ROI_1, 419.544, 414.783, 478.577, 432.361)
concat_obj (ROI_0, ROI_1, RegionInput)
union1 (RegionInput,RegionSurface)
*****保留死角区域,避免受影响*****
threshold (InputImage, Region, 0, 0)
get_image_size (InputImage, Width, Height)
*****根据输入区域进行平面拟合,输出起算面信息(X倾斜、Y倾斜和Z截距)*****
fit_surface_first_order (RegionSurface, InputImage, 'huber', 5, 1, Alpha, Beta, Gamma)
*****根据X倾斜、Y倾斜,计算平面与水平面夹角******
tuple_sqrt(Alpha*Alpha + Beta*Beta,AlphaBeta)
tuple_atan ((ZResolution*AlphaBeta)/XResolution, Rad)
tuple_cos(Rad, Cos)
*****根据X倾斜、Y倾斜创建基准面图像(纯平面)
area_center (RegionSurface,Area, CenterRow, CenterColumn)
gen_image_surface_first_order(ImageSurface, 'uint2', Alpha, Beta, Gamma, CenterRow, CenterColumn, Width, Height)
*****原始图像与基准面作差,并根据与水平面夹角进行高度修正,并在结果上加32768,即校正后零的灰度值为32768)
sub_image (InputImage, ImageSurface, OutputImage, Cos, 32768)
*****将无效数据区域数值重新置零*****
overpaint_region(OutputImage,Region,0,'fill')
*********************************************************************************
****************************3D测量代码*****************************************************
**************测量两个平面间的高度差(断差)*************************************
gen_rectangle1 (plan1, 178.87, 379.627, 230.335, 463.123)
gen_rectangle1 (plan2, 306.019, 407.459, 357.483, 445.545)
intensity (plan1, OutputImage, Mean, Deviation)
intensity (plan2, OutputImage, Mean1, Deviation1)
HeightDifference:=abs(Mean-Mean1)*ZResolution
******************************测量单个平面上的凸点和凹坑*****************************
*先计算基准高度
gen_circle (plan3, 341.839, 418.171, 13.1093)
intensity (plan3, OutputImage, Mean2, Deviation2)
*找高于基准0.1mm的凸点和低于基准0.1mm的凹坑
TuDian_Offset:=0.1/ZResolution
AoKeng_Offset:=-0.1/ZResolution
gen_circle (RegionTest, 330.237, 422.107, 84.9124)
reduce_domain (OutputImage, RegionTest, ImageReduced)
*通过与基准高度的偏差参数,找凸点和凹坑
threshold (ImageReduced, Region1, Mean2+TuDian_Offset, 999999999)
threshold (ImageReduced, Region2, 0, Mean2+AoKeng_Offset)
**************************测量单个平面的平面度***************************************************
gen_ellipse (plan4, 336.292, 423.572, rad(90), 142.285, 101.387)
gen_circle (plan5, 336.292, 425.037, 58.6328)
difference (plan4, plan5, RegionDifference)
*缩放图像的灰度值到mm,3D重建,并显示(如果深度图的灰度信息已经是mm,则不需要缩放)
scale_image (OutputImage, ImgScal, ZResolution, 0)
convert_image_type (ImgScal, zMap, 'real')
gen_image_surface_first_order (xMap, 'real', 0, XResolution, 0, 0, 0, Width, Height)
gen_image_surface_first_order (yMap, 'real', YResolution, 0, 0, 0, 0, Width, Height)
xyz_to_object_model_3d (xMap, yMap, zMap, ObjectModel3D)
visualize_object_model_3d (WindowHandle, ObjectModel3D, [], [], \['lut','color_attrib','disp_pose'], \['color1','coord_z','true'], [], [], [], PoseOut)*裁切出需要检测平面度的区域,并利用点云计算平面度
reduce_object_model_3d_by_view (RegionDifference, ObjectModel3D, [], [], ObjectModel3DReduced)
*把3D图元拟合到3D点集中(plane类型是平面,算法least_squares_tukey是最小二乘法)
paraName:=['primitive_type','fitting_algorithm']
paraVal:=['plane','least_squares_tukey']
fit_primitives_object_model_3d (ObjectModel3DReduced, paraName, paraVal, planeFitted)
*计算平面度
distance_object_model_3d (ObjectModel3DReduced, planeFitted, [], 0, [], [])
get_object_model_3d_params (ObjectModel3DReduced, '&distance', pointsDis)
flatness:=abs(max(pointsDis)-min(pointsDis))