我无法从视差中获得正常的深度图。这是我的代码:
#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;
}
我的图像是:
视差图:
我得到这样的点云:
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/ )获取 depth_map ,我认为没有足够的校准信息。
编辑:现在我得到像这样的东西:
当然更好,但仍然不正常。
Edited2:我试过你说的:
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(...) 行得到这个结果:
当我评论这一行时:
Ps:另外请你告诉我如何仅知道基线和焦距以像素为单位计算深度。