halcon|halcon 畸变图像校正与长度测量

* This program measures the length of scratches in world * coordinates in a perspectively distorted image *************************************************************1.初始化****************************************************************************** * 关闭窗口 dev_close_window () * 更新程序计数器,更新变量,更新图形窗口 dev_update_off () * 设置区域填充模式 dev_set_draw ('margin') * 读取图像 read_image (Image, 'scratch/scratch_perspective') * 获取图像类型,宽度,高度,缓存数据 get_image_pointer1 (Image, Pointer, Type, Width, Height) * 打开窗口 dev_open_window (0, 0, Width, Height, 'black', WindowHandle1) * 设置字体信息 set_display_font (WindowHandle1, 14, 'mono', 'true', 'false') * 显示图像 dev_display (Image) * 右下角显示运行字样字符串 disp_continue_message (WindowHandle1, 'black', 'true') stop () * *************************************************************2.校正相机**************************************************************************** * 标定文件名 CaltabName := 'caltab_30mm.descr' * [Focus,Kappa,Sx,Sy,Cx,Cy,Whith,Height] StartCamPar := [0.0184898,-548.002,8.33409e-006,8.3e-006,275.291,255.374,640,480] *创建标定数据模型 create_calib_data ('calibration_object', 1, 1, CalibDataID) *设置相机类型并初始化标定数据模型里的摄像机内部参数 set_calib_data_cam_param (CalibDataID, 0, 'area_scan_division', StartCamPar) *为校正模型指定校正文件 set_calib_data_calib_object (CalibDataID, 0, CaltabName) NumImages := 12 for i := 1 to NumImages by 1 *读取图像 read_image (Image, 'scratch/scratch_calib_'+i$'02d') *显示图像 dev_display (Image) *获取校正板内边框以内的区域 find_caltab (Image, Caltab, CaltabName, 3, 112, 5) *设置输出对象显示颜色 dev_set_color ('green') *显示校正板内边框以内的区域 dev_display (Caltab) *提取出图像中MARK点的位置并计算出摄像机外部参数 find_marks_and_pose (Image, Caltab, CaltabName, StartCamPar, 128, 10, 18, 0.9, 15, 100, RCoord, CCoord, StartPose) *设置输出对象显示颜色 dev_set_color ('red') *显示MARK点的位置 disp_circle (WindowHandle1, RCoord, CCoord, gen_tuple_const(|RCoord|,2.5)) dev_set_part (0, 0, Height-1, Width-1) *收集观察数据 set_calib_data_observ_points (CalibDataID, 0, 0, i, RCoord, CCoord, 'all', StartPose) endfor dev_update_time ('on') disp_continue_message (WindowHandle1, 'black', 'true') stop () *开始校正摄像机 calibrate_cameras (CalibDataID, Error) *获取优化以后的摄像机内部参数 get_calib_data (CalibDataID, 'camera', 0, 'params', CamParam) *获取优化以后的校正对象姿势,相对于当前参考相机。 get_calib_data (CalibDataID, 'calib_obj_pose', [0,1], 'pose', PoseCalib)*************************************************************3.图像转换**************************************************************************** * dev_open_window (0, Width+5, Width, Height, 'black', WindowHandle2) set_display_font (WindowHandle2, 14, 'mono', 'true', 'false') *将校正对象姿势顺时针旋转90度 insert (PoseCalib, PoseCalib[5] -90, 5, PoseCalibRot) *将摄像机位姿进行X,Y,Z的平移(矩阵乘积) set_origin_pose (PoseCalibRot, -0.04, -0.03, 0.00075, Pose) * 像素的实际物理长度 PixelDist := 0.00013 *pose_to_hom_mat3d (Pose, HomMat3D) *根据摄像机内参和外参生成一个投影变换,这个投影变换代表图像坐标系(z=0)到世界坐标系的转换关系 gen_image_to_world_plane_map (Map, CamParam, Pose, Width, Height, Width, Height, PixelDist, 'bilinear') Imagefiles := ['scratch/scratch_calib_01', 'scratch/scratch_perspective'] for i := 1 to 2 by 1 *读取图像 read_image (Image, Imagefiles[i-1]) *设置当前窗口为窗口一 dev_set_window (WindowHandle1) *窗口一显示图像 dev_display (Image) *设置当前窗口为窗口二 dev_set_window (WindowHandle2) *将投影变换应用到当前图像上 map_image (Image, Map, ModelImageMapped) *映射以后的图像 dev_display (ModelImageMapped) if (i=1) gen_contour_polygon_xld (Polygon, [230, 230], [189, 189+0.03/PixelDist]) disp_message (WindowHandle2, '3cm', 'window', 205, 195, 'red', 'false') dev_display (Polygon) disp_continue_message (WindowHandle2, 'black', 'true') stop () endif endfor*************************************************************4.在世界坐标系下面对图像中的刮伤进行长度测量****************************************************设置区域填充模式 dev_set_draw ('fill') *快速阈值 fast_threshold (ModelImageMapped, Region, 0, 80, 20) *填充阈值后的区域 fill_up (Region, RegionFillUp) *腐蚀上面的区域 erosion_rectangle1 (RegionFillUp, RegionErosion, 5, 5) *将腐蚀后区域里的图像剪切出来 reduce_domain (ModelImageMapped, RegionErosion, ImageReduced) *再对剪切的图形进行阈值 fast_threshold (ImageReduced, Region1, 55, 100, 20) *对阈值区域膨胀运算 dilation_circle (Region1, RegionDilation1, 2.0) *对膨胀区域进行腐蚀运算 erosion_circle (RegionDilation1, RegionErosion1, 1.0) *连通性处理 connection (RegionErosion1, ConnectedRegions) *根据面积来过滤连通处理后的区域 select_shape (ConnectedRegions, SelectedRegions, ['area', 'ra'], 'and', [40, 15], [2000, 1000]) *统计对象数目 count_obj (SelectedRegions, NumScratches) *显示校正后的图像 dev_display (ModelImageMapped) for i := 1 to NumScratches by 1 *设置输出对象的颜色 dev_set_color ('yellow') *从SelectedRegions里根据索引获取子区域 select_obj (SelectedRegions, ObjectSelected, i) *获取ObjectSelected区域的骨架 skeleton (ObjectSelected, Skeleton) *获取骨架的轮廓XLD gen_contours_skeleton_xld (Skeleton, Contours, 1, 'filter') *显示缺陷的轮廓 dev_display (Contours) *计算缺陷轮廓的长度 length_xld (Contours, ContLength) *计算缺陷轮廓的面积和中心坐标 area_center_points_xld (Contours, Area, Row, Column) *显示缺陷的长度信息 disp_message (WindowHandle2, 'L= '+(ContLength*PixelDist*100)$'.4'+' cm', 'window', Row-10, Column+20, 'yellow', 'false') disp_continue_message (WindowHandle2, 'black', 'true') stop () endfor

halcon|halcon 畸变图像校正与长度测量
文章图片

【halcon|halcon 畸变图像校正与长度测量】

    推荐阅读