Blob分析之board _components.hdev

*用立体方法分割板子组件的示例程序
*Application program to illustrate the segmentation
* of board _components.hdev  with stereo methods
* 用有两个相机的立体装备
* We have a stereo setup of two cameras.
*两个相机都是经过30mm的标定板标定过的
* Both cameras will be calibrated by a couple of images
* of a 30mm calibration plate.
*以此来矫正和分割不同板子的图像
*The images of different  boards will be rectified and the components segmented.
*这种方法只对有一定纹理的物体有效
* Note that this approach only works for sufficiently textured objects.
*如果只有很少的或者重复的纹理图像,对应的两个图像中点会匹配失败,测量结果也是错的
* If little or repetitive texture is present, the matching
* of corresponding points in both images will fail and
* therefore the measurement will be false.
*只有很少的或者重复的纹理图像的时候,可以加一个动态照明,给现场加一个随机的照明,生成一种纹理
* In such cases, it is a good practice to use active
* illumination, e.g., projecting a random light pattern
* onto the scene, to add texture information.
* 在有些应用场合中,有限的场景深度会成为一个问题,特别是场景特别小或者用远心镜头的时候
* In some applications, a limited depth of field can be
* a problem, especially when using a small field of view
* or telecentric lenses.
*在这种情况下,可以用倾斜镜头,充分利用Scheimpflug原理
* In such cases, please consider using tilt lenses
* to take advantage of the Scheimpflug principle.
*
* -------------------------------------------------------
* (1)           STEREO CALIBRATION
* 关闭更新
dev_update_off ()
*设置图像路径
* Set the image path (make sure, that you have set HALCONIMAGES to the HALCON image directory)
ImgPath := 'stereo/board/'
*标定板描述文件
CalDescrFile := 'caltab_30mm.descr'
* Read the first images to get their size
*获取图像
read_image (ImageL, ImgPath + 'calib_l_01')
read_image (ImageR, ImgPath + 'calib_r_01')
* Reopen the windows with an appropriate size
*官拜窗口
dev_close_window ()
*获取图像大小
get_image_size (ImageL, WidthL, HeightL)
*打开固定尺寸的新窗体
dev_open_window (0, 0, WidthL, HeightL, 'black', WindowHandle1)
*设置显示填充形式,两种,margin表示边界,fill表示全部填充
dev_set_draw ('margin')
*设置显示颜色为绿色
dev_set_color ('green')
*设置显示字体
set_display_font (WindowHandle1, 14, 'mono', 'true', 'false')
*获取图像大小
get_image_size (ImageR, WidthR, HeightR)
*打开新窗体
dev_open_window (0, WidthL + 12, WidthL, HeightL, 'black', WindowHandle2)
*设置显示填充形式,两种,margin表示边界,fill表示全部填充
dev_set_draw ('margin')
*设置显示颜色为绿色
dev_set_color ('green')
*设置显示字体
set_display_font (WindowHandle2, 14, 'mono', 'true', 'false')
* Calibrate the stereo setup
* (determine the internal and external camera parameters).
*
* Set the initial values for the internal camera parameters
*生成面扫描相机初始参数
gen_cam_par_area_scan_division (0.0125, 0, 1.48e-5, 1.48e-5, WidthL / 2.0, HeightL / 2.0, WidthL, HeightL, StartCamPar)
*创建标定数据句柄
create_calib_data ('calibration_object', 2, 1, CalibDataID)
*设置标定相机出事参数
set_calib_data_cam_param (CalibDataID, 'all', [], StartCamPar)
*设置标定板文件
set_calib_data_calib_object (CalibDataID, 0, CalDescrFile)
*
* Read all used calibration images
*读取左右两幅图像
read_image (ImagesL, ImgPath + 'calib_l_' + [1:15]$'02d')
read_image (ImagesR, ImgPath + 'calib_r_' + [1:15]$'02d')
* Calibrate the setup
*矫正立体双目视觉
calibrate_stereo_setup (ImagesL, ImagesR, WindowHandle1, WindowHandle2, CalibDataID, StartCamPar, CalDescrFile, CamParamL, CamParamR, cLPcR, Errors)
* Generate the rectification maps
*生成双目矫正分布图
gen_binocular_rectification_map (MapL, MapR, CamParamL, CamParamR, cLPcR, 1, 'geometric', 'bilinear', RectCamParL, RectCamParR, CamPoseRectL, CamPoseRectR, RectLPosRectR)
*
* Check the calibration result
* by checking the epipolar constraint on the rectified images:
* The differences in row coordinates of all
* calibration point correspondences should be minimal
*检视矫正结果
map_image (ImagesL, MapL, ImagesRectL)
map_image (ImagesR, MapR, ImagesRectR)
*检测极线约束差异
check_epipolar_constraint (ImagesRectL, ImagesRectR, RectCamParL, RectCamParR, WindowHandle1, WindowHandle2, CalDescrFile, EpipolarError)
if (EpipolarError[1] > 0.3)
    * The calibration of the stereo rig was not performed thoroughly
    *右下角显示 'Press Run (F5) to continue' 这个信息
    disp_continue_message (WindowHandle2, 'black', 'true')
    stop ()
endif
*右下角显示 'Press Run (F5) to continue' 这个信息
disp_continue_message (WindowHandle2, 'black', 'true')
stop ()
*清除矫正数据
clear_calib_data (CalibDataID)
* --------------------------------------------------------
* (2)   STEREO PROCESSING
*
* Parameters from above if calibration was not performed:
* gen_cam_par_area_scan_division (0.0131205, -665.85466, 1.4803422e-005, 1.48e-005, 155.90117, 126.703971, 320, 240, CamParamL)
* gen_cam_par_area_scan_division (0.0131712, -728.9579, 1.4799849e-005, 1.48e-005, 163.265701, 119.310684, 320, 240, CamParamR)
* cLPcR := [0.15350044,-0.003732778,0.04481715,0.1736607,319.8612,359.8945,0]
*定义双目视觉相对的差异参数和双目距离
* Define parameter values for binocular_disparity and binocular_distance, respectively.
*掩码宽
MaskWidth := 17
*掩码高
MaskHeight := 17
*纹理阈值
TextureThresh := 5
*最小差异
MinDisparity := 10
*最大差异
MaxDisparity := 40
*Level
NumLevels := 1
ScoreThresh := 0.1
* Get stereo image pair
**聚焦到窗体1
dev_set_window (WindowHandle1)
*读取图像
read_image (ImageL, ImgPath + 'board_l_01')
*增强对比度
emphasize (ImageL, ImageL, 7, 7, 1)
*映射图像
map_image (ImageL, MapL, ImageRectifiedL)
*显示图像
dev_display (ImageRectifiedL)
**聚焦到窗体2
dev_set_window (WindowHandle2)
*读取图像
read_image (ImageR, ImgPath + 'board_r_01')
*增强图像
emphasize (ImageR, ImageR, 7, 7, 1)
*映射图像
map_image (ImageR, MapR, ImageRectifiedR)
*显示图像
dev_display (ImageRectifiedR)
*显示信息
disp_message (WindowHandle1, 'Rectified left image', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle2, 'Rectified right image', 'window', 12, 12, 'black', 'true')
  *右下角显示 'Press Run (F5) to continue' 这个信息
disp_continue_message (WindowHandle2, 'black', 'true')
stop ()
*当以图像距离
* Determine the distance image.
binocular_distance (ImageRectifiedL, ImageRectifiedR, DistanceImage, ScoreImageDistance, RectCamParL, RectCamParR, RectLPosRectR, 'ncc', MaskWidth, MaskHeight, TextureThresh, MinDisparity, MaxDisparity, NumLevels, ScoreThresh, 'left_right_check', 'interpolation')
* Display the distance image
dev_set_window (WindowHandle1)
dev_clear_window ()
dev_display (DistanceImage)
* Correct the distance image for the tilt of the stereo camera system.
*   (Attention: the distance values are not measured
*    in the rectified coordinate system anymore!)
* Define a region, which indicates three areas on the reference plane
*生成一组圆
gen_circle (Circle, [15,208,95], [58,32,211], [8,8,8])
*联合区域
union1 (Circle, RegionDefiningReferencePlane)
*倾斜修正
tilt_correction (DistanceImage, RegionDefiningReferencePlane, DistanceImageCorrected)
**聚焦到窗体2
dev_set_window (WindowHandle2)
*清除窗体
dev_clear_window ()
*显示纠正图像
dev_display (DistanceImageCorrected)
*显示信息
disp_message (WindowHandle1, 'Distance image', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle2, 'After tilt correction', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle2, 'black', 'true')
stop ()
MinHeight := -0.0005
MaxHeight := 0.05
* Transform the distances into heights above the reference plane.
*计算离参考平面的距离范围
height_range_above_reference_plane (DistanceImageCorrected, HeightAboveReferencePlaneReduced, MinHeight, MaxHeight)
*显示高度范围(本地函数)
visualize_height_ranges (ImageRectifiedL, HeightAboveReferencePlaneReduced, WindowHandle2, 0.0004, 0.0015, 0.0015, 0.0025, 0.0025, 0.004)
**聚焦到窗体1
dev_set_window (WindowHandle1)
*清除窗体
dev_clear_window ()
*显示数据
dev_display (HeightAboveReferencePlaneReduced)
*显示信息
disp_message (WindowHandle2, 'Segmented components', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle1, 'Height above reference plane', 'window', 12, 12, 'black', 'true')

上一篇:Halcon例程解析——measure_caliper.hdev


下一篇:Halcon progress 18.05新功能介绍