双目立体视觉中最重要的一步
通过双目立体视觉行成disparityimage以后,如何或得点云。这个问题困扰了我很久,在形成视差图后,怎么把或得的结果,返回到左相机视图中,然后形成点云,这里最重要的就是这个算子disparity_image_to_xyz
Disparity : 视差图,可以通过binocular_disparity或得
X, Y, Z : 对应的x,y,z坐标,通过xyz_to_object_model_3d形成3d模型
CamParamRect1,左相机的内参(主相机)
CamParamRect2, 右相机的内参(辅相机)
RelPoseRect : 右相机相对于左相机的旋转矩阵,可以通过binocular_calibration或得,也就是双目相机的标定
这里有最重要的一步,就是disparityimage的区域填充
为什么要填充,在disparityimage形成的过程中,有些区域是没有值得,填充就是通过一定的运算,给这些空的区域进行赋值。halcon里面是这么做得,最后一步,有懂得朋友,欢迎交流学习!
*填补视差图中的空白,先获取有差值的区域
get_domain (DisparityImage, RegionInpainting)
****求没有视差值得区域的补集,这个区域如果system的设置clip_region为false时会比较大
complement (RegionInpainting, RegionInpainting)
*****扩展DisparityImage到它最小的外接矩形的全域
full_domain (DisparityImage, DisparityImage)
get_domain (DisparityImage, Domain)
*****对全域disparityimage的补集区域,进行谐波差值结算
harmonic_interpolation (DisparityImage, RegionInpainting, DisparityImage, 0.001)
*****补完以后可以看到,没有差值的部分,出现了差值,具体的插补过程不太懂,死记住先用吧
具体大家可以参考实例 disparity_image_to_xyz
它的代码如下
bash
dev_close_window ()
dev_update_on ()
ImagePath := 'stereo/board/'
Image1 := ImagePath + '/board_l_01'
Image2 := ImagePath + '/board_r_01'
*
* Define the camera parameters
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)
create_pose (0.15350044, -0.003732778, 0.04481715, 0.1736607, 319.8612, 359.8945, 'Rp+T', 'gba', 'point', RelPose)
*
* Generate a disparity image
* ******************************************
*
* Generate the rectification maps
gen_binocular_rectification_map (MapL, MapR, CamParamL, CamParamR, RelPose, 1, 'viewing_direction', 'bilinear', RectCamParL, RectCamParR, CamPoseRectL, CamPoseRectR, RectLPosRectR)
*
* Read and rectify the stereo image pair
read_image (ImageL, Image1)
read_image (ImageR, Image2)
map_image (ImageL, MapL, ImageRectifiedL)
map_image (ImageR, MapR, ImageRectifiedR)
*
* Display the left rectified image
get_image_size (ImageRectifiedL, WidthL, HeightL)
dev_open_window (0, 0, WidthL, HeightL, 'white', WindowHandle1)
set_display_font (WindowHandle1, 11, 'mono', 'true', 'false')
dev_display (ImageRectifiedL)
disp_message (WindowHandle1, 'Left rectified image', 'window', 10, 10, 'black', 'true')
*
* Display the right rectified image
dev_open_window (0, WidthL + 10, WidthL, HeightL, 'white', WindowHandle2)
set_display_font (WindowHandle2, 11, 'mono', 'true', 'false')
dev_display (ImageRectifiedL)
disp_message (WindowHandle2, 'Right rectified image', 'window', 10, 10, 'black', 'true')
disp_continue_message (WindowHandle2, 'black', 'true')
stop ()
*
* Determine the disparity image.
binocular_disparity (ImageRectifiedL, ImageRectifiedR, DisparityImage, Score, 'ncc', 17, 17, 5, 10, 40, 1, 0.1, 'left_right_check', 'none')
*
* Fill the gaps in the disparity image
*填补视差图中的空白
get_domain (DisparityImage, RegionInpainting)
****求没有视差值得区域的补集
complement (RegionInpainting, RegionInpainting)
*****扩展DisparityImage到它最小的外接矩形的全域
full_domain (DisparityImage, DisparityImage)
get_domain (DisparityImage, Domain)
*****对全域disparityimage的补集区域,进行谐波差值结算
harmonic_interpolation (DisparityImage, RegionInpainting, DisparityImage, 0.001)
*****补完以后可以看到,没有差值的部分,出现了差值,具体的插补过程不太懂,死记住先用吧
* Display the disparity image
dev_set_window (WindowHandle1)
dev_display (DisparityImage)
disp_message (WindowHandle1, 'Disparity image', 'window', 10, 10, 'black', 'true')
*
* Compute the 3D coordinates
* *******************************************
*
* Transform the disparity image into images X, Y and Z.
* The gray values in X, Y and Z represent the x, y, and z
* coordinates of the pixels (Row, Column).
*****一直在找的一个参数,将disparityimage值匹配回左相机的图像中去
disparity_image_to_xyz (DisparityImage, X, Y, Z, RectCamParL, RectCamParR, RectLPosRectR)
*
* Visualize the 3D points in the 3D space
* *******************************************
*
* Scale Z (for better visualization)
scale_image (Z, Z, 2, 0)
*
* Reduce impact of inaccuracies in visualization
threshold (Z, Regions, 0.415, 0.5)
reduce_domain (Z, Regions, ZThresholded)
* Add the gray values to the point cloud.
xyz_attrib_to_object_model_3d (X, Y, ZThresholded, ImageRectifiedL, '&gray', ObjectModel3D)
*
* Visualize the result
prepare_object_model_3d (ObjectModel3D, 'segmentation', 'true', 'max_area_holes', 100)
create_pose (-0.025, -0.37, 1.7, 300, 12, 180, 'Rp+T', 'gba', 'point', Pose)
visualize_object_model_3d (WindowHandle2, ObjectModel3D, [], Pose, 'color_attrib', '&gray', [], [], [], PoseOut)