问题:在SLAM双目立体中,使用OpenCV的StereoSGBM来计算左右图像的视差,代码时slam14讲的ch5/stereo/stereoVision.cpp
调试报错如下
终端运行报错
代码
#include <opencv2/opencv.hpp>
#include <vector>
#include <string>
#include <Eigen/Core>
#include <pangolin/pangolin.h>
#include <unistd.h>
using namespace std;
using namespace Eigen;
using namespace cv;
// 文件路径
string left_file = "./left.png";
string right_file = "./right.png";
// Pangolin绘画
void showPointCloud(
const vector<Vector4d, aligned_allocator<Vector4d>> &pointCloud);
int main(int argc, char **argv) {
// 内参
double fx = 718.856;
double fy = 718.856;
double cx = 607.1928;
double cy = 185.2157;
// 基线
double b = 0.573;
// 读取图像
Mat left = imread(left_file, 0);
Mat right = imread(right_file, 0);
// imshow("left", left);
// imshow("right", right);
// 使用StereoSGBM算法计算视差图
Ptr<StereoSGBM> sgbm = StereoSGBM::create(
0, 96, 9, 8*9*9, 32*9*9, 1, 63, 10, 100, 32
);
Mat disparity_sgbm, disparity;
sgbm -> compute(left, right, disparity_sgbm);
disparity_sgbm.convertTo(disparity, CV_32F, 1.0/16.0f);
// // 生成点云
// vector<Vector4d, aligned_allocator<Vector4d>> pointcloud;
// for (int v = 0; v < left.rows; v++) {
// for (int u = 0; u < left.cols; u++) {
// if (disparity.at<float>(v, u) <=0.0 || disparity.at<float>(v, u) >= 96.0) continue;
// Vector4d point(0, 0, 0, left.at<uchar>(v, u) / 255.0); // 前三维是xyz,第四维是颜色
// // 根据双目模型计算point的位置
// double x = (u - cx) / fx;
// double y = (u - cy) / fy;
// double depth = fx * b / (disparity.at<float>(v, u));
// point[0] = x * depth;
// point[1] = y * depth;
// point[2] = depth;
// pointcloud.push_back(point);
// }
// }
// 可视化
imshow("disparity", disparity / 96.0);
waitKey(0);
// showPointCloud(pointcloud);
return 0;
}
void showPointCloud(const vector<Vector4d, Eigen::aligned_allocator<Vector4d>> &pointcloud) {
if (pointcloud.empty()) {
cerr << "Point cloud is empty!" << endl;
return;
}
pangolin::CreateWindowAndBind("Point Cloud Viewer", 1024, 768);
glEnable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
pangolin::OpenGlRenderState s_cam(
pangolin::ProjectionMatrix(1024, 768, 500, 500, 512, 389, 0.1, 1000),
pangolin::ModelViewLookAt(0, -0.1, -1.8, 0, 0, 0, 0.0, -1.0, 0.0)
);
pangolin::View &d_cam = pangolin::CreateDisplay()
.SetBounds(0.0, 1.0, pangolin::Attach::Pix(175), 1.0, -1024.0f / 768.0f)
.SetHandler(new pangolin::Handler3D(s_cam));
while (pangolin::ShouldQuit() == false) {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
d_cam.Activate(s_cam);
glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
glPointSize(2);
glBegin(GL_POINTS);
for (auto &p: pointcloud) {
glColor3f(p[3], p[3], p[3]);
glVertex3d(p[0], p[1], p[2]);
}
glEnd();
pangolin::FinishFrame();
usleep(5000); // sleep 5 ms
}
return;
}
CMakeLists.txt如下
cmake_minimum_required( VERSION 3.10 )
project(stereoVision)
set(CMAKE_BUILD_TYPE "Debug")
set( CMAKE_CXX_FLAGS "-std=c++11")
find_package(OpenCV)
include_directories(${OpenCV})
find_package(Pangolin REQUIRED)
add_executable(stereoVision stereoVision.cpp)
target_link_libraries(stereoVision ${OpenCV_LIBS} ${Pangolin_LIBRARIES})
add_executable(stereoVisionTest stereoVisionTest.cpp)
target_link_libraries(stereoVisionTest ${OpenCV_LIBS} ${Pangolin_LIBRARIES})
文件目录结构
恳请各位好兄弟好姐妹指导