当前位置: 首页 > article >正文

OpenCV双目立体视觉重建

         本篇文章主要给出使用opencv sgbm重建三维点云的代码,鉴于自身水平所限,如有错误,欢迎批评指正。

        环境:vs2015 ,opencv3.4.6,pcl1.8.0

        原始数据使用D455采集,图像已做完立体校正,如下图所示(欢迎进Q群交流:874653199):

        左图:

        右图:

         视差结果图:

        彩色视差结果图:

        点云结果:

#include <iostream>
#include <fstream>

#include <opencv2/opencv.hpp> 
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include<pcl/io/ply_io.h>
#include <pcl/point_types.h>
#include <pcl/visualization/pcl_visualizer.h>

#define isStereoRectify 


void visualize(pcl::PointCloud<pcl::PointXYZ>::Ptr cloud)
{
  pcl::visualization::PCLVisualizer viewer("3D Viewer");

  pcl::visualization::PointCloudColorHandlerCustom<pcl::PointXYZ> src_h(cloud, 255, 255, 255);

  viewer.setBackgroundColor(0, 0, 0);
  viewer.addPointCloud(cloud, src_h, "cloud");

  while (!viewer.wasStopped())
  {
    viewer.spinOnce(100);
    boost::this_thread::sleep(boost::posix_time::microseconds(100000));
  }

}


void recon3d(cv::Mat disparty, double f, double cx, double cy, double baseline) {

  pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>());

  pcl::PointXYZ singlePoint;

  for (int i = 0; i < disparty.rows; i++) {
    for (int j = 0; j < disparty.cols; j++) {
      const double disp = disparty.at<float>(i, j);
      if (disp == 0) {
        continue;
      }
      else {
        singlePoint.z = f*baseline / disp;

        singlePoint.x = (i - cx) / f *singlePoint.z;

        singlePoint.y = (j - cy) / f *singlePoint.z;

        if (singlePoint.z >= -0.65 && singlePoint.z <= 0.3) {
          cloud->points.emplace_back(singlePoint);
        }
      }
    }
  }

  visualize(cloud);
  pcl::io::savePLYFileBinary("cloud.ply", *cloud);

}




int main(){

  cv::Mat imageL = cv::imread("E:/2_光学测量/6_数据/6_stereo/l0.jpg",0);
  cv::Mat imageR = cv::imread("E:/2_光学测量/6_数据/6_stereo/r0.jpg", 0);



  cv::Mat cameraMatrixL = (cv::Mat_<double>(3, 3) << 428.406, 0.000000, 420.335, 0.000000, 428.406, 238.037, 0.000000, 0.000000, 1.000000);
  cv::Mat distCoeffL = (cv::Mat_<double>(5, 1) << 0, 0, 0, 0, 0);


  cv::Mat cameraMatrixR = (cv::Mat_<double>(3, 3) << 428.406, 0.000000, 420.335, 0.000000, 428.406, 238.037, 0.000000, 0.000000, 1.000000);
  cv::Mat distCoeffR = (cv::Mat_<double>(5, 1) << 0, 0, 0, 0, 0);


  cv::Mat R = (cv::Mat_<double>(3, 3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);

  cv::Mat T = (cv::Mat_<double>(3, 1) << -0.0949472, 0, 0);
  

#ifdef isStereoRectify

  cv::Mat Rl, Rr, Pl, Pr, Q;
  cv::Rect validROIL, validROIR;

  cv::Size imageSize = imageL.size();
  cv::stereoRectify(cameraMatrixL, distCoeffL, cameraMatrixR, distCoeffR, imageSize, R, T, Rl, Rr, Pl, Pr, Q, cv::CALIB_ZERO_DISPARITY,
    0, imageSize, &validROIL, &validROIR);

  cv::Mat mapLx, mapLy, mapRx, mapRy;

  cv::initUndistortRectifyMap(cameraMatrixL, distCoeffL, Rl, Pl, imageSize, CV_32FC1, mapLx, mapLy);
  cv::initUndistortRectifyMap(cameraMatrixR, distCoeffR, Rr, Pr, imageSize, CV_32FC1, mapRx, mapRy);

  cv::Mat rectifyImageL, rectifyImageR;
  cv::remap(imageL, rectifyImageL, mapLx, mapLy, cv::INTER_LINEAR);
  cv::remap(imageR, rectifyImageR, mapRx, mapRy, cv::INTER_LINEAR);
  imageL = rectifyImageL;
  imageR = rectifyImageR;

#endif // stero

  cv::namedWindow("disparity", CV_WINDOW_NORMAL);

  int SADWindowSize =5, numberOfDisparities = 128;
  cv::Ptr<cv::StereoSGBM> sgbm = cv::StereoSGBM::create(0, numberOfDisparities, SADWindowSize);
  sgbm->setPreFilterCap(64);
  sgbm->setBlockSize(SADWindowSize);
  sgbm->setP1(8 * SADWindowSize* SADWindowSize);
  sgbm->setP2(64 * SADWindowSize* SADWindowSize);
  sgbm->setMinDisparity(0);
  sgbm->setNumDisparities(numberOfDisparities);
  sgbm->setUniquenessRatio(10);
  sgbm->setSpeckleWindowSize(200);
  sgbm->setSpeckleRange(64);
  sgbm->setDisp12MaxDiff(1);
  sgbm->setMode(cv::StereoSGBM::MODE_SGBM);

  cv::Mat disp, disp8, dispf;
  sgbm->compute(imageL, imageR, disp);

  disp.convertTo(disp, CV_32F, 1.0 / 16.0);//1.0/16.0 
  disp.convertTo(disp8, CV_8U, 1.0);
  imshow("disparity", disp8);
  cv::imwrite("disp_mono.png", disp8);
  cv::Mat disp8_color;
  cv::applyColorMap(disp8, disp8_color, cv::COLORMAP_JET);
  imshow("disparity_color", disp8_color);
  cv::imwrite("disp_color.png", disp8_color);

  recon3d(disp, cameraMatrixL.at<double>(0,0), cameraMatrixL.at<double>(0, 2), cameraMatrixL.at<double>(1, 2), T.at<double>(0));

  cv::waitKey(0);

  return 0;

}


http://www.kler.cn/a/397420.html

相关文章:

  • 网盘聚合搜索项目Aipan(爱盼)
  • Ubuntu从入门到精通(一)系统安装
  • pytest在conftest.py中实现用例执行失败进行截图并附到allure测试报告
  • JavaSE常用API-日期(计算两个日期时间差-高考倒计时)
  • HarmonyOS 开发环境搭建
  • 机器学习 决策树
  • 在openi平台 基于华为顶级深度计算平台 openmind 动手实践
  • OSS文件上传
  • 基于微信小程序的校园超市购物系统设计与实现,LW+源码+讲解
  • onlyoffice Command service(命令服务)使用示例
  • 【HarmonyOS】鸿蒙应用低功耗蓝牙BLE的使用心得 (三)
  • 要卸载 Grafana 或者从 TiDB 集群中删除 Grafana 服务节点,你需要按以下步骤操作
  • leetcode 35. 搜索插入位置 简单
  • python re模块 详解
  • 在k8s上部署Crunchy Postgres for Kubernetes
  • 流程图图解@RequestBody @RequestPart @RequestParam @ModelAttribute
  • Django的RBAC认证和权限
  • Python + Memcached:分布式应用程序中的高效缓存
  • pytest中的断言:深入解析与实践
  • Net.Core Mvc 添加 log 日志
  • 1、PyTorch介绍与张量的创建
  • 迅睿CMS如何实现文章自动推送百度的便捷方法?
  • 怎样遵守编程规范,减少和控制C++编程中出现的bug?
  • uniapp适配暗黑模式配置plus.nativeUI.setUIStyle适配DarkMode配置
  • phonemizer 获取英文文本句子单词音素 - python实现
  • 智能工厂的设计软件 为了监管控一体化的全能Supervisor 的监督学习 之 序2 架构for认知系统 :机器学习及其行动门上的机器人