获得深度图

5

我无法从视差中获取正常的深度图。以下是我的代码:

#include "opencv2/core/core.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include "opencv2/contrib/contrib.hpp"
#include <cstdio>
#include <iostream>
#include <fstream>

using namespace cv;
using namespace std;

ofstream out("points.txt");

int main()
{
    Mat img1, img2;
    img1 = imread("images/im7rect.bmp");
    img2 = imread("images/im8rect.bmp");

    //resize(img1, img1, Size(320, 280));
    //resize(img2, img2, Size(320, 280));

    Mat g1,g2, disp, disp8;
    cvtColor(img1, g1, CV_BGR2GRAY);
    cvtColor(img2, g2, CV_BGR2GRAY);

    int sadSize = 3;
    StereoSGBM sbm;
    sbm.SADWindowSize = sadSize;
    sbm.numberOfDisparities = 144;//144; 128
    sbm.preFilterCap = 10; //63
    sbm.minDisparity = 0; //-39; 0
    sbm.uniquenessRatio = 10;
    sbm.speckleWindowSize = 100;
    sbm.speckleRange = 32;
    sbm.disp12MaxDiff = 1;
    sbm.fullDP = true;
    sbm.P1 = sadSize*sadSize*4;
    sbm.P2 = sadSize*sadSize*32;
    sbm(g1, g2, disp);

    normalize(disp, disp8, 0, 255, CV_MINMAX, CV_8U);

    Mat dispSGBMscale; 
    disp.convertTo(dispSGBMscale,CV_32F, 1./16); 

    imshow("image", img1);

    imshow("disparity", disp8);

    Mat Q;
    FileStorage fs("Q.txt", FileStorage::READ);
    fs["Q"] >> Q;
    fs.release();

    Mat points, points1;
    //reprojectImageTo3D(disp, points, Q, true);
    reprojectImageTo3D(disp, points, Q, false, CV_32F);
    imshow("points", points);

    ofstream point_cloud_file;
    point_cloud_file.open ("point_cloud.xyz");
    for(int i = 0; i < points.rows; i++) {
        for(int j = 0; j < points.cols; j++) {
            Vec3f point = points.at<Vec3f>(i,j);
            if(point[2] < 10) {
                point_cloud_file << point[0] << " " << point[1] << " " << point[2]
                    << " " << static_cast<unsigned>(img1.at<uchar>(i,j)) << " " << static_cast<unsigned>(img1.at<uchar>(i,j)) << " " << static_cast<unsigned>(img1.at<uchar>(i,j)) << endl;
            }
        }
    }
    point_cloud_file.close(); 

    waitKey(0);

    return 0;
}

我的图片是:

enter image description here enter image description here

视差图:

enter image description here

我得到了类似于这样的点云: enter image description here

Q相等: [ 1., 0., 0., -3.2883545303344727e+02, 0., 1., 0., -2.3697290992736816e+02, 0., 0., 0., 5.4497170185417110e+02, 0., 0., -1.4446083962336606e-02, 0. ]

我尝试了很多其他方法。我尝试使用不同的图像,但没有一个能够得到正常的深度图。

我做错了什么?我应该使用reprojectImageTo3D还是使用其他方法?最好的可视化深度图的方法是什么?(我尝试过point_cloud库) 或者您能否提供带有数据集和校准信息的工作示例,以便我可以运行它并获得深度图。或者如何从middlebury立体数据库中获取深度图(http://vision.middlebury.edu/stereo/data/),我认为没有足够的校准信息。

编辑: 现在我得到了类似于: enter image description here

当然更好了,但仍然不正常。

编辑2: 我尝试了您所说的:

Mat disp;
disp = imread("disparity-image.pgm", CV_LOAD_IMAGE_GRAYSCALE);

Mat disp64; 
disp.convertTo(disp64,CV_64F, 1.0/16.0); 
imshow("disp", disp);

使用cv::minMaxIdx(...)函数,我得到了以下结果: enter image description here

当我注释掉这一行时,结果如下所示: enter image description here

附带说明:请问如何只知道基线和以像素为单位的焦距时计算深度?


这完全取决于您的校准。如果您的重新投影误差超过0.5,将得到一个糟糕的Q矩阵。 - berak
@berak: 我知道这个问题,因为我无法获得良好的校准,所以我尝试了来自互联网的数据集(https://code.google.com/p/tjpstereovision/source/browse/),我认为这是正常的Q矩阵。我如何从Middleburry数据集中获取深度图? - Aram Gevorgyan
1个回答

2

我已经对OpenCV的reprojectImageTo3D()和我的方法进行了简单比较(见下文),并且还测试了正确的视差和Q矩阵。

// Reproject image to 3D
void customReproject(const cv::Mat& disparity, const cv::Mat& Q, cv::Mat& out3D)
{
    CV_Assert(disparity.type() == CV_32F && !disparity.empty());
    CV_Assert(Q.type() == CV_32F && Q.cols == 4 && Q.rows == 4);

    // 3-channel matrix for containing the reprojected 3D world coordinates
    out3D = cv::Mat::zeros(disparity.size(), CV_32FC3);

    // Getting the interesting parameters from Q, everything else is zero or one
    float Q03 = Q.at<float>(0, 3);
    float Q13 = Q.at<float>(1, 3);
    float Q23 = Q.at<float>(2, 3);
    float Q32 = Q.at<float>(3, 2);
    float Q33 = Q.at<float>(3, 3);

    // Transforming a single-channel disparity map to a 3-channel image representing a 3D surface
    for (int i = 0; i < disparity.rows; i++)
    {
        const float* disp_ptr = disparity.ptr<float>(i);
        cv::Vec3f* out3D_ptr = out3D.ptr<cv::Vec3f>(i);

        for (int j = 0; j < disparity.cols; j++)
        {
            const float pw = 1.0f / (disp_ptr[j] * Q32 + Q33);

            cv::Vec3f& point = out3D_ptr[j];
            point[0] = (static_cast<float>(j)+Q03) * pw;
            point[1] = (static_cast<float>(i)+Q13) * pw;
            point[2] = Q23 * pw;
        }
    }
}

两种方法产生的结果几乎相同,它们都对我来说似乎是正确的。您能否在视差图和Q矩阵上尝试一下?您可以在我的GitHub上使用我的测试环境。
注意1:还要注意不要两次缩放视差(如果您的视差也被缩放了,请注释掉disparity.convertTo(disparity, CV_32F, 1.0 / 16.0);这一行)。
注意2:它是基于OpenCV 3.0构建的,您可能需要更改包含文件。

所以 XYZ 是一个与视差相同大小的三通道浮点图像。XYZ 的每个元素都包含从视差图计算出的点(x, y)的三维坐标。在类型方面,存在误用问题,我修改了代码以在各处使用 double - Kornel
谢谢,我修改了问题。我使用的是Opencv 2.4.9版本。 - Aram Gevorgyan
如果您注释掉cv::minMaxIdx(...)这一行,会发生什么? - Kornel
很难确定在这里中,你有一个带有正确视差和Q矩阵的样例代码,你能否加载并尝试在你的代码中运行? - Kornel
我进行了最终测试,结果看起来对我来说是正确的,请查看我的更新答案。 - Kornel
是的,它可行,非常感谢您的帮助 :) 我还没有用Opencv 3.0检查过,但我也会检查。 - Aram Gevorgyan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接