gpt4 book ai didi

opencv - 获取深度图

转载 作者:太空宇宙 更新时间:2023-11-03 20:59:21 31 4
gpt4 key购买 nike

我无法从差异中获得正常的深度图。这是我的代码:

#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/ ) 获取 depth_map,我认为没有足够的校准信息。

编辑:现在我得到像: 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

Ps:另外请你告诉我如何只知道以像素为单位的基线和焦距来计算深度。

最佳答案

我对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); 如果您disparity 也被缩放。)

注意 2:它是用 OpenCV 3.0 构建的,您可能需要更改包含。

关于opencv - 获取深度图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30071343/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com