gpt4 book ai didi

c++ - opencv 中的 StereoBM 类是否对输入图像或帧进行校正?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:30:02 25 4
gpt4 key购买 nike

作为我项目的一部分,我将 SteroBM 类用于立体视觉。我正在从 2 个网络摄像头获取输入帧,并在不进行校正的情况下对输入帧灰度帧运行立体 block 匹配计算。我得到的输出与基本事实相去甚远(非常不完整)。我想知道,是不是因为我没有对输入帧进行整改。此外,我选择的基线保持在 20 厘米。我使用的是 opencv-3.2.0 版本的 c++。

我正在运行的代码如下。

#include <opencv2/core.hpp>
#include <opencv2/opencv.hpp>
#include </home/eswar/softwares/opencv_contrib-3.2.0/modules/contrib_world/include/opencv2/contrib_world.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/calib3d.hpp>
#include <opencv2/imgproc.hpp>
#include <stdio.h>
#include <iostream>

using namespace std;
using namespace cv;

int main()
{
//initialize and allocate memory to load the video stream from camera
VideoCapture camera0(0);
VideoCapture camera1(1);

if( !camera0.isOpened() ) return 1;
if( !camera1.isOpened() ) return 1;
Mat frame0,frame1;
Mat frame0gray,frame1gray;
Mat dispbm,dispsgbm;
Mat dispnorm_bm,dispnorm_sgbm;
Mat falseColorsMap, sfalseColorsMap;
int ndisparities = 16*5; /**< Range of disparity */
int SADWindowSize = 21; /**< Size of the block window. Must be odd */
Ptr<StereoBM> sbm = StereoBM::create( ndisparities, SADWindowSize );
Ptr<StereoSGBM> sgbm = StereoSGBM::create(0, //int minDisparity
96, //int numDisparities
5, //int SADWindowSize
600, //int P1 = 0
2400, //int P2 = 0
10, //int disp12MaxDiff = 0
16, //int preFilterCap = 0
2, //int uniquenessRatio = 0
20, //int speckleWindowSize = 0
30, //int speckleRange = 0
true); //bool fullDP = false
//-- Check its extreme values
double minVal; double maxVal;
while(true)
{
//grab and retrieve each frames of the video sequentially
camera0 >> frame0;
camera1 >> frame1;

imshow("Video0", frame0);
imshow("Video1", frame1);
cvtColor(frame0,frame0gray,CV_BGR2GRAY);
cvtColor(frame1,frame1gray,CV_BGR2GRAY);

sbm->compute( frame0gray, frame1gray, dispbm );
minMaxLoc( dispbm, &minVal, &maxVal );
dispbm.convertTo( dispnorm_bm, CV_8UC1, 255/(maxVal - minVal));

sgbm->compute(frame0gray, frame1gray, dispsgbm);
minMaxLoc( dispsgbm, &minVal, &maxVal );
dispsgbm.convertTo( dispnorm_sgbm, CV_8UC1, 255/(maxVal - minVal));

imshow( "BM", dispnorm_bm);
imshow( "SGBM",dispnorm_sgbm);

//wait for 40 milliseconds
int c = cvWaitKey(40);
//exit the loop if user press "Esc" key (ASCII value of "Esc" is 27)
if(27 == char(c)) break;
}
return 0;
}

尽管在代码中您看到也使用了 block 匹配,但请忽略它,因为它给出的输出更差。我发现 SGBM 输出更接近真实情况,因此我决定对其进行改进。但是,如果对如何改进 block 匹配结果有任何帮助。太棒了,我当然会很感激。

SGBM 技术的输出图像深度图像看起来像。 Depth map using SGBM method

最佳答案

不,StereoBM不做校正,只是 block 匹配和一些前后处理,但是opencv提供了相机校准和校正检查的功能this link

此外,在 opencv 示例中还有针对此过程的现成示例,因此不必从头开始编写代码。

关于结果,StereoBM是基于SAD算法(local stereo-matching),鲁棒性不强,可以试试wls filter ,这可以显着改善您的结果。

StereoSGBM是基于SGM算法(实际上与原论文介绍的略有不同)的半全局算法,在视差图生成中考虑全局优化,产生更好的视差但速度较慢。

关于c++ - opencv 中的 StereoBM 类是否对输入图像或帧进行校正?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45855725/

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