OpenCV stereo matching 例子

OpenCV stereo matching 例子,给个例子学习一下啊!

0

2个回答

1
 /*
 *  stereo_match.cpp
 *  calibration
 *
 *  Created by Victor  Eruhimov on 1/18/10.
 *  Copyright 2010 Argus Corp. All rights reserved.
 *
 */

#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/contrib/contrib.hpp"

#include <stdio.h>

using namespace cv;

static void print_help()
{
    printf("\nDemo stereo matching converting L and R images into disparity and point clouds\n");
    printf("\nUsage: stereo_match <left_image> <right_image> [--algorithm=bm|sgbm|hh|var] [--blocksize=<block_size>]\n"
           "[--max-disparity=<max_disparity>] [--scale=scale_factor>] [-i <intrinsic_filename>] [-e <extrinsic_filename>]\n"
           "[--no-display] [-o <disparity_image>] [-p <point_cloud_file>]\n");
}

static void saveXYZ(const char* filename, const Mat& mat)
{
    const double max_z = 1.0e4;
    FILE* fp = fopen(filename, "wt");
    for(int y = 0; y < mat.rows; y++)
    {
        for(int x = 0; x < mat.cols; x++)
        {
            Vec3f point = mat.at<Vec3f>(y, x);
            if(fabs(point[2] - max_z) < FLT_EPSILON || fabs(point[2]) > max_z) continue;
            fprintf(fp, "%f %f %f\n", point[0], point[1], point[2]);
        }
    }
    fclose(fp);
}

int main(int argc, char** argv)
{
    const char* algorithm_opt = "--algorithm=";
    const char* maxdisp_opt = "--max-disparity=";
    const char* blocksize_opt = "--blocksize=";
    const char* nodisplay_opt = "--no-display=";
    const char* scale_opt = "--scale=";

    if(argc < 3)
    {
        print_help();
        return 0;
    }
    const char* img1_filename = 0;
    const char* img2_filename = 0;
    const char* intrinsic_filename = 0;
    const char* extrinsic_filename = 0;
    const char* disparity_filename = 0;
    const char* point_cloud_filename = 0;

    enum { STEREO_BM=0, STEREO_SGBM=1, STEREO_HH=2, STEREO_VAR=3 };
    int alg = STEREO_SGBM;
    int SADWindowSize = 0, numberOfDisparities = 0;
    bool no_display = false;
    float scale = 1.f;

    StereoBM bm;
    StereoSGBM sgbm;
    StereoVar var;

    for( int i = 1; i < argc; i++ )
    {
        if( argv[i][0] != '-' )
        {
            if( !img1_filename )
                img1_filename = argv[i];
            else
                img2_filename = argv[i];
        }
        else if( strncmp(argv[i], algorithm_opt, strlen(algorithm_opt)) == 0 )
        {
            char* _alg = argv[i] + strlen(algorithm_opt);
            alg = strcmp(_alg, "bm") == 0 ? STEREO_BM :
                  strcmp(_alg, "sgbm") == 0 ? STEREO_SGBM :
                  strcmp(_alg, "hh") == 0 ? STEREO_HH :
                  strcmp(_alg, "var") == 0 ? STEREO_VAR : -1;
            if( alg < 0 )
            {
                printf("Command-line parameter error: Unknown stereo algorithm\n\n");
                print_help();
                return -1;
            }
        }
        else if( strncmp(argv[i], maxdisp_opt, strlen(maxdisp_opt)) == 0 )
        {
            if( sscanf( argv[i] + strlen(maxdisp_opt), "%d", &numberOfDisparities ) != 1 ||
                numberOfDisparities < 1 || numberOfDisparities % 16 != 0 )
            {
                printf("Command-line parameter error: The max disparity (--maxdisparity=<...>) must be a positive integer divisible by 16\n");
                print_help();
                return -1;
            }
        }
        else if( strncmp(argv[i], blocksize_opt, strlen(blocksize_opt)) == 0 )
        {
            if( sscanf( argv[i] + strlen(blocksize_opt), "%d", &SADWindowSize ) != 1 ||
                SADWindowSize < 1 || SADWindowSize % 2 != 1 )
            {
                printf("Command-line parameter error: The block size (--blocksize=<...>) must be a positive odd number\n");
                return -1;
            }
        }
        else if( strncmp(argv[i], scale_opt, strlen(scale_opt)) == 0 )
        {
            if( sscanf( argv[i] + strlen(scale_opt), "%f", &scale ) != 1 || scale < 0 )
            {
                printf("Command-line parameter error: The scale factor (--scale=<...>) must be a positive floating-point number\n");
                return -1;
            }
        }
        else if( strcmp(argv[i], nodisplay_opt) == 0 )
            no_display = true;
        else if( strcmp(argv[i], "-i" ) == 0 )
            intrinsic_filename = argv[++i];
        else if( strcmp(argv[i], "-e" ) == 0 )
            extrinsic_filename = argv[++i];
        else if( strcmp(argv[i], "-o" ) == 0 )
            disparity_filename = argv[++i];
        else if( strcmp(argv[i], "-p" ) == 0 )
            point_cloud_filename = argv[++i];
        else
        {
            printf("Command-line parameter error: unknown option %s\n", argv[i]);
            return -1;
        }
    }

    if( !img1_filename || !img2_filename )
    {
        printf("Command-line parameter error: both left and right images must be specified\n");
        return -1;
    }

    if( (intrinsic_filename != 0) ^ (extrinsic_filename != 0) )
    {
        printf("Command-line parameter error: either both intrinsic and extrinsic parameters must be specified, or none of them (when the stereo pair is already rectified)\n");
        return -1;
    }

    if( extrinsic_filename == 0 && point_cloud_filename )
    {
        printf("Command-line parameter error: extrinsic and intrinsic parameters must be specified to compute the point cloud\n");
        return -1;
    }

    int color_mode = alg == STEREO_BM ? 0 : -1;
    Mat img1 = imread(img1_filename, color_mode);
    Mat img2 = imread(img2_filename, color_mode);

    if( scale != 1.f )
    {
        Mat temp1, temp2;
        int method = scale < 1 ? INTER_AREA : INTER_CUBIC;
        resize(img1, temp1, Size(), scale, scale, method);
        img1 = temp1;
        resize(img2, temp2, Size(), scale, scale, method);
        img2 = temp2;
    }

    Size img_size = img1.size();

    Rect roi1, roi2;
    Mat Q;

    if( intrinsic_filename )
    {
        // reading intrinsic parameters
        FileStorage fs(intrinsic_filename, CV_STORAGE_READ);
        if(!fs.isOpened())
        {
            printf("Failed to open file %s\n", intrinsic_filename);
            return -1;
        }

        Mat M1, D1, M2, D2;
        fs["M1"] >> M1;
        fs["D1"] >> D1;
        fs["M2"] >> M2;
        fs["D2"] >> D2;

        M1 *= scale;
        M2 *= scale;

        fs.open(extrinsic_filename, CV_STORAGE_READ);
        if(!fs.isOpened())
        {
            printf("Failed to open file %s\n", extrinsic_filename);
            return -1;
        }

        Mat R, T, R1, P1, R2, P2;
        fs["R"] >> R;
        fs["T"] >> T;

        stereoRectify( M1, D1, M2, D2, img_size, R, T, R1, R2, P1, P2, Q, CALIB_ZERO_DISPARITY, -1, img_size, &roi1, &roi2 );

        Mat map11, map12, map21, map22;
        initUndistortRectifyMap(M1, D1, R1, P1, img_size, CV_16SC2, map11, map12);
        initUndistortRectifyMap(M2, D2, R2, P2, img_size, CV_16SC2, map21, map22);

        Mat img1r, img2r;
        remap(img1, img1r, map11, map12, INTER_LINEAR);
        remap(img2, img2r, map21, map22, INTER_LINEAR);

        img1 = img1r;
        img2 = img2r;
    }

    numberOfDisparities = numberOfDisparities > 0 ? numberOfDisparities : ((img_size.width/8) + 15) & -16;

    bm.state->roi1 = roi1;
    bm.state->roi2 = roi2;
    bm.state->preFilterCap = 31;
    bm.state->SADWindowSize = SADWindowSize > 0 ? SADWindowSize : 9;
    bm.state->minDisparity = 0;
    bm.state->numberOfDisparities = numberOfDisparities;
    bm.state->textureThreshold = 10;
    bm.state->uniquenessRatio = 15;
    bm.state->speckleWindowSize = 100;
    bm.state->speckleRange = 32;
    bm.state->disp12MaxDiff = 1;

    sgbm.preFilterCap = 63;
    sgbm.SADWindowSize = SADWindowSize > 0 ? SADWindowSize : 3;

    int cn = img1.channels();

    sgbm.P1 = 8*cn*sgbm.SADWindowSize*sgbm.SADWindowSize;
    sgbm.P2 = 32*cn*sgbm.SADWindowSize*sgbm.SADWindowSize;
    sgbm.minDisparity = 0;
    sgbm.numberOfDisparities = numberOfDisparities;
    sgbm.uniquenessRatio = 10;
    sgbm.speckleWindowSize = bm.state->speckleWindowSize;
    sgbm.speckleRange = bm.state->speckleRange;
    sgbm.disp12MaxDiff = 1;
    sgbm.fullDP = alg == STEREO_HH;

    var.levels = 3;                                 // ignored with USE_AUTO_PARAMS
    var.pyrScale = 0.5;                             // ignored with USE_AUTO_PARAMS
    var.nIt = 25;
    var.minDisp = -numberOfDisparities;
    var.maxDisp = 0;
    var.poly_n = 3;
    var.poly_sigma = 0.0;
    var.fi = 15.0f;
    var.lambda = 0.03f;
    var.penalization = var.PENALIZATION_TICHONOV;   // ignored with USE_AUTO_PARAMS
    var.cycle = var.CYCLE_V;                        // ignored with USE_AUTO_PARAMS
    var.flags = var.USE_SMART_ID | var.USE_AUTO_PARAMS | var.USE_INITIAL_DISPARITY | var.USE_MEDIAN_FILTERING ;

    Mat disp, disp8;
    //Mat img1p, img2p, dispp;
    //copyMakeBorder(img1, img1p, 0, 0, numberOfDisparities, 0, IPL_BORDER_REPLICATE);
    //copyMakeBorder(img2, img2p, 0, 0, numberOfDisparities, 0, IPL_BORDER_REPLICATE);

    int64 t = getTickCount();
    if( alg == STEREO_BM )
        bm(img1, img2, disp);
    else if( alg == STEREO_VAR ) {
        var(img1, img2, disp);
    }
    else if( alg == STEREO_SGBM || alg == STEREO_HH )
        sgbm(img1, img2, disp);
    t = getTickCount() - t;
    printf("Time elapsed: %fms\n", t*1000/getTickFrequency());

    //disp = dispp.colRange(numberOfDisparities, img1p.cols);
    if( alg != STEREO_VAR )
        disp.convertTo(disp8, CV_8U, 255/(numberOfDisparities*16.));
    else
        disp.convertTo(disp8, CV_8U);
    if( !no_display )
    {
        namedWindow("left", 1);
        imshow("left", img1);
        namedWindow("right", 1);
        imshow("right", img2);
        namedWindow("disparity", 0);
        imshow("disparity", disp8);
        printf("press any key to continue...");
        fflush(stdout);
        waitKey();
        printf("\n");
    }

    if(disparity_filename)
        imwrite(disparity_filename, disp8);

    if(point_cloud_filename)
    {
        printf("storing the point cloud...");
        fflush(stdout);
        Mat xyz;
        reprojectImageTo3D(disp, xyz, Q, true);
        saveXYZ(point_cloud_filename, xyz);
        printf("\n");
    }

    return 0;
}
2
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
其他相关推荐
立体匹配算法(Stereo Matching)及其在OpenCV中的应用
模拟人的两只眼睛的Stereo相机最近变得很受欢迎。通过对stereo相机拍摄的左右两张图进行匹配找出视差图,可以还原物体的3D信息。 立体匹配(Stereo matching)的步骤如下: 1: 预处理:亮度归一化,去噪,图像增强,滤波等等 2: 匹配Cost计算Cost aggregation       每个像素点的matching cost可用下图所示的
Stereo Matching文献笔记之(九):经典算法Semi-Global Matching(SGM)之神奇的HMI代价计算~
SGM算法源于《Stereo Processing by Semi-Global Matching and Mutual Information》一文,我认为这篇文章是立体匹配算法中最给力的,放眼KITTI,可以发现目前排名前五十的算法几乎一半都是对SGM的改进,具有最强的实用价值。SGM中文名称“半全局匹配”,顾名思义,其介于局部算法和全局算法之间,所谓半全局指的是算法既没有只考虑像素的局部区域,也没有考虑所有的像素点。例如,BM计算某一点视差的时候,往往根据目标像素周围的矩形区域进行代价聚合计算;Dou
Stereo Matching文献笔记之(五):经典算法DoubleBP读后感~
DoubleBP是一个立体匹配全局算法,来自于论文《Stereo Matching with Color-Weighted Correlation, Hierarchical Belief Propagation, and Occlusion Handling》,PAMI2009年提出,这是我第一篇读的,关于立体匹配方向的论文,当时感觉立体匹配太难了,很多概念都不知道是啥意思,比如说Color-We
Stereo Matching文献笔记之(十):经典算法Semi-Global Matching(SGM)之碉堡的动态规划~
上一篇博客中提到了SGM的第一部分,基于分层互信息(HMI)的代价计算,时隔两个月,本文继续说说自己对SGM代价聚合部分的理解。
推荐一些有效的stereo matching代码,对于3D图片的深度计算十分有效
最近在求深度图的时候总是很苦恼,一直找不到效果好的求
PatchMatch算法(vs+opencv)
基于Visual Studio 2015和OpenCV实现的PatchMatch算法。
Semi-Global Matching(SGM)算法原文理解
参考:@迷雾forest http://blog.csdn.net/wsj998689aa/article/details/49464017,原博主对SGM算法的精髓理解的很透,我是在参考他文章的基础上,才能看懂SGM算法几处关键的地方。本文的不同在于加入了一些我自己的理解,并且调整了一下整个算法阐述的思路,当是自己的一个阅读笔记。后边打算再做一下SGM原始算法与OpenCV的SGBM算法实现的对
双目Stereo重建算法SGM(1) - 互信息(Mutual Information)
1 概述对于机器视觉,我目前还是刚起步,可能步还没起来。近日开始接触双目Stereo的相关算法,主要是对Heiko Hirschmüller在2008年的文献(Hirschmüller 2008)中描述的算法进行研究。该文献,也就是利用互信息(Mutual Information)作为计算matching cost的基础进行stereo重建的工作,在相关领域内的影响还是比较大的。他所使用的cost...
图像的视差匹配(Stereo Matching)
这里要求用我们自己计算得到的视差图和给的视差图作比较来比较我们得到的视差图的好坏程度,我视差图返回的值是计算得到的视差乘以3之后的图,所以在计算时我不是两个值相差大于1,而是大于3,因为两个图像都乘3了,所以要大于3,我传入的参数是两个图像的矩阵,因为我是写了一个脚本咯跑所有测例的,在脚本里边已经把图像读出来了 function [percentnumberbadpixels] =
Patchmatch算法简单实现
OpenCV实现patchmatch算法笔记,环境Visual Studio 2015 x64 + OpenCV 3.0。
立体匹配算法_自适应窗口_Cross Based Support Region
1 自适应窗口 对于立体匹配局部算法而言,匹配代价计算 和 支持窗口选择是很重要的两个问题:匹配代价计算通过前面的文章已经介绍了关于Adaptive Support-Weight和Rank Transform两种方法,对于支持窗口来说的,通常我们选取一个矩形窗,既然要自适应通常有一下几个思路: 1)自适应大小:通过参数自适应调整矩形的长和宽来改变窗口,例如对于重复纹理区域我们可以采用较大的支持
Stereo Matching文献笔记之(六):浅谈置信度传播算法(Belief-Propagation)在立体匹配中的应用~
这是我一个纠结过的问题,曾经反反复复的看相关的知识,Belief-Propagation是一个伴随着“马尔科夫随机场”提出的优化算法,我对优化算法情有独钟,一直觉得搞定了各种优化,机器学习剩下的也就是知识扩展而已,嘿嘿,我也不知道这么想是对是错,最近脑袋糊涂的厉害,请各位见谅。。。。
Stereo Processing by Semiglobal Matching and Mutual Information
Stereo Processing by Semiglobal Matching and Mutual Information (SGBM)论文原文
Stereo Matching: an Overview
Computer stereo vision is the extraction of 3D information from digital images, such as those obtained by a CCD camera. By comparing information about a scene from two vantage points, 3D information can be extracted by examining the relative positions of objects in the two panels. This is similar to the biological process Stereopsis. Stereoscopic images are often stored as MPO (multi picture object) files. Recently, researchers pushed to develop methods to reduce the storage needed for these files in order to maintain the high quality of the stereo image
立体匹配算法_RankTransform
1.基本介绍 立体匹配算法,关键是计算左右图对应点的匹配代价。 传统的基于窗口的局部匹配算法都是通过计算亮度值的相似程度来获取匹配代价,上一篇文章讲到得Adaptive Support-weight只不过是在计算直至窗口匹配代价时,为每一个像素赋予不同的权值进而使得匹配代价更准确一些。 但是基于像素的匹配方法有一个问题就是对于重复纹理区域,支持窗口选取太小则不利于获取准确的匹配代价,窗口选取
stereo matching using tree filtering
stereo matching using tree filtering 是杨庆雄老师的一个论文代码,关于立体匹配,发表在PAMI,同时是non-local的扩展
立体匹配文献阅读笔记(三)《Stereo Matching Using Tree Filtering》15年
这篇论文基本算是对NLCA的补充(大部分内容为NLCA原论文)。针对NLCA在强纹理区域的不足作出了改进,需要计算两次MST,增加了时间复杂度。 序言部分对现有立体匹配方法总结得很好,值得一读。 等NLCA中的MST最小生成树如何构建掌握以后再来看这篇文章中对MST的改进。
立体匹配(ADCensus)
VS2012下调试好的代码,需要配置PCL,BOOST,OpenMP
目标检测、跟踪、语意分割、stereo matching常用网址
1 单目标跟踪单目标跟踪的benchmarkhttps://github.com/foolwood/benchmark_results总结性文章传统方法 Understanding and Diagnosing Visual Tracking Systems深度学习 Object Visual Tracking with Deep Learning Methods2 多目标跟踪https://m
TCSVT论文结构整理
本文记录了在准备TCSVT论文的过程中发现的论文撰写规律,经验主要基于五篇论文,分别是: 文献一:《Accurate Image-Guided Stereo Matching With Efficient Matching Cost and Disparity Refinement》(2016) 文献二:《PMSC:PatchMatch-Based Superpixel Cut for accu...
Stereo Match 立体匹配算法,包括SSD SAD SSD NCC NSSD Census 等基本算法
Stereo Match 立体匹配算法,包括SSD SAD SSD NCC NSSD Census 等基本算法,。用C/C++语言编写,适合初学者学习!
AdaptingBP算法
the best perform stereo matching algorithm
patchmatch 算法细节
本文讲解了patchmatch算法和图像补全算法, 并附以结果展示, 希望能对大家有所帮助
block matching
Block Matching Algorithms for Motion Estimation
四种Block Match算法
块匹配Block Match算法常用于双目立体匹配和帧间距离匹配上,特点是实现步骤简单,这里介绍四种基本的Block Match算法: 1.Sum of Absolute Differencse(sad)公式如下: C++实现代码: Mat funcSADR2L(Mat leftImage, Mat rightImage, int windowSize, int dispMi
转载:密集立体匹配20年论文整理
原文地址: http://blog.csdn.net/xuyuhua1985/article/details/26283389   1994 Kanade T, Okutomi M. A stereo matching algorithm with an adaptive window: Theory and experiment[J]. TPAMI, 1994, 16(9): 920-9...
基于分段的用于立体匹配的遮挡处理的差异细化
Segment-based Disparity Refinement with Occlusion Handling for Stereo Matching
ELAS开源程序(高效大规模立体匹配)
论文Efficient Large-Scale Stereo Matching对应源代码
立体匹配经典文献
Research on improved SIFT stereo matching algorithm;Binary Stereo Matching;Stereo Matching with Color-Weighted Correlation,Hierarchical Belief Propagation and OcclusionHandling;基于立体视觉分析的显著性区域检测算法_汪婷 等共计三十多篇中英文文献
Constant Time Weighted Median Filtering for Stereo Matching and Beyond.
Constant Time Weighted Median Filtering for Stereo Matching and Beyond.
A Non-Local Cost Aggregation method for stereo Matching配套ppt讲解
A Non-Local Cost Aggregation method for stereo Matching配套ppt讲解
动态规划 立体匹配 半全局立体匹配
根据论文Accurate and Efficient Stereo Processing by Semi-Global Matching and Mutual Information写的双目立体匹配代码,matlab编写方便阅读,带测试图片,注意算法只实现了4个方向,即左右,右左,上下,下上四个方向。然后,互信息没有用到。测试结果可见效果不错,对于学习动态规划,立体匹配的同学有帮助
Pyramid Stereo Matching Network
Recent work has shown that depth estimation from a stereo pair of images can be formulated as a supervised learning task to be resolved with convolutional neural networks (CNNs). However, current architectures rely on patch-based Siamese networks, lacking the means to exploit context information for finding correspondence in illposed regions. To tackle this problem, we propose PSMNet, a pyramid stereo matching network consisting of two main modules: spatial pyramid pooling and 3D CNN. The spatial pyramid pooling module takes advantage of the capacity of global context information by aggregating context in different scales and locations to form a cost volume. The 3D CNN learns to regularize cost volume using stacked multiple hourglass networks in conjunction with intermediate supervision. The proposed approach was evaluated on several benchmark datasets. Our method ranked first in the KITTI 2012 and 2015 leaderboards before March 18, 2018. The codes of PSMNet are available at: https: //github.com/JiaRenChang/PSMNet.
opencv 双目相机标定 自带例子程序的使用
有了上篇单目标定示例程序的经验,双目标定就是小菜一碟哈。 本人目前菜鸟,但还是愿意厚着脸皮分享我一下午的成果。不要拍我... 1.找到目录   ...\opencv\sources\samples\cpp下的 stereo_calib.cpp、stereo_calib.xml 和 stereo_match.cpp以及26张图片了(left01-14   right01-14)。 其中
patchmatch stereo matching
一种快速实现亚像素的立体匹配方法,其主要思想是补图的思想,引入进立体匹配中
stereo matching overview
立体匹配技术简介 立体匹配主要是通过找出每对图像间的对应关系,根据三角测量原理,得到视差图;在获得了视差信息后,根据投影模型很容易地可以得到原始图像的深度信息和三维信息。
立体标定中的StereoCalibration函数
立体标定就是双目视觉的标定,用来计算空间上两台摄像机在相机坐标系下的旋转矩阵R和平移矩阵T。cvStereoCalibration和cvCalibrationCamera2相似,不同的是cvStereoCalibration寻求单个旋转矩阵和平移向量来联系左右摄像机。        bool cvStereoCalibration( const CvMat* objectPoints,   /
stereo matching 2006 datasets.zip
Evaluation of Cost Functions for Stereo Matching.pdf及Learning Conditional Random Fields for Stereo,及论文中的实验图片素材
密集立体匹配20年论文整理
文章地址:http://blog.csdn.net/xuyuhua1985/article/details/26283389 1994 Kanade T, Okutomi M. A stereo matching algorithm with an adaptive window: Theory and experiment[J]. TPAMI, 1994, 16(9): 920-932. ...
A Census-Based Stereo Vision Algorithm
A Census-Based Stereo Vision Algorithm Using Modified Semi-Global Matching and Plane Fitting to Improve Matching Quality_ECVW2010
文章热词 统计学稳健估计opencv函数 机器学习教程 Objective-C培训 交互设计视频教程 颜色模型
相关热词 mysql关联查询两次本表 native底部 react extjs glyph 图标 python和opencv教程 python机器学习例子