1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//M*////////////////////////////////////////////////////////////////////////////////////// 2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. 4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// By downloading, copying, installing or using the software you agree to this license. 6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// If you do not agree to this license, do not download, install, 7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// copy or use the software. 8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// License Agreement 11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// For Open Source Computer Vision Library 12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright (C) 2000, Intel Corporation, all rights reserved. 14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright (C) 2013, OpenCV Foundation, all rights reserved. 15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Third party copyrights are property of their respective owners. 16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Redistribution and use in source and binary forms, with or without modification, 18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// are permitted provided that the following conditions are met: 19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// * Redistribution's of source code must retain the above copyright notice, 21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// this list of conditions and the following disclaimer. 22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// * Redistribution's in binary form must reproduce the above copyright notice, 24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// this list of conditions and the following disclaimer in the documentation 25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// and/or other materials provided with the distribution. 26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// * The name of the copyright holders may not be used to endorse or promote products 28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// derived from this software without specific prior written permission. 29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 30793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// This software is provided by the copyright holders and contributors "as is" and 31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// any express or implied warranties, including, but not limited to, the implied 32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// warranties of merchantability and fitness for a particular purpose are disclaimed. 33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// In no event shall the Intel Corporation or contributors be liable for any direct, 34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// indirect, incidental, special, exemplary, or consequential damages 35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// (including, but not limited to, procurement of substitute goods or services; 36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// loss of use, data, or profits; or business interruption) however caused 37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// and on any theory of liability, whether in contract, strict liability, 38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// or tort (including negligence or otherwise) arising in any way out of 39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// the use of this software, even if advised of the possibility of such damage. 40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// 41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//M*/ 42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "precomp.hpp" 44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "opencv2/calib3d/calib3d_c.h" 45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 46793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerCvStereoBMState* cvCreateStereoBMState( int /*preset*/, int numberOfDisparities ) 47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ 48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler CvStereoBMState* state = (CvStereoBMState*)cvAlloc( sizeof(*state) ); 49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler if( !state ) 50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler return 0; 51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->preFilterType = CV_STEREO_BM_XSOBEL; //CV_STEREO_BM_NORMALIZED_RESPONSE; 53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->preFilterSize = 9; 54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->preFilterCap = 31; 55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->SADWindowSize = 15; 56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->minDisparity = 0; 57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->numberOfDisparities = numberOfDisparities > 0 ? numberOfDisparities : 64; 58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->textureThreshold = 10; 59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->uniquenessRatio = 15; 60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->speckleRange = state->speckleWindowSize = 0; 61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->trySmallerWindows = 0; 62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->roi1 = state->roi2 = cvRect(0,0,0,0); 63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->disp12MaxDiff = -1; 64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->preFilteredImg0 = state->preFilteredImg1 = state->slidingSumBuf = 66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->disp = state->cost = 0; 67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 68793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler return state; 69793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} 70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 71793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid cvReleaseStereoBMState( CvStereoBMState** state ) 72793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ 73793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler if( !state ) 74793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler CV_Error( CV_StsNullPtr, "" ); 75793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 76793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler if( !*state ) 77793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler return; 78793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 79793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cvReleaseMat( &(*state)->preFilteredImg0 ); 80793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cvReleaseMat( &(*state)->preFilteredImg1 ); 81793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cvReleaseMat( &(*state)->slidingSumBuf ); 82793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cvReleaseMat( &(*state)->disp ); 83793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cvReleaseMat( &(*state)->cost ); 84793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cvFree( state ); 85793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} 86793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 87793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid cvFindStereoCorrespondenceBM( const CvArr* leftarr, const CvArr* rightarr, 88793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler CvArr* disparr, CvStereoBMState* state ) 89793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ 90793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cv::Mat left = cv::cvarrToMat(leftarr), right = cv::cvarrToMat(rightarr); 91793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler const cv::Mat disp = cv::cvarrToMat(disparr); 92793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 93793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler CV_Assert( state != 0 ); 94793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 95793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cv::Ptr<cv::StereoBM> sm = cv::StereoBM::create(state->numberOfDisparities, 96793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler state->SADWindowSize); 97793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setPreFilterType(state->preFilterType); 98793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setPreFilterSize(state->preFilterSize); 99793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setPreFilterCap(state->preFilterCap); 100793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setBlockSize(state->SADWindowSize); 101793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setNumDisparities(state->numberOfDisparities > 0 ? state->numberOfDisparities : 64); 102793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setTextureThreshold(state->textureThreshold); 103793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setUniquenessRatio(state->uniquenessRatio); 104793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setSpeckleRange(state->speckleRange); 105793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setSpeckleWindowSize(state->speckleWindowSize); 106793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->setDisp12MaxDiff(state->disp12MaxDiff); 107793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 108793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler sm->compute(left, right, disp); 109793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} 110793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 111793ee12c6df9cad3806238d32528c49a3ff9331dNoah PreslerCvRect cvGetValidDisparityROI( CvRect roi1, CvRect roi2, int minDisparity, 112793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler int numberOfDisparities, int SADWindowSize ) 113793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ 114793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler return (CvRect)cv::getValidDisparityROI( roi1, roi2, minDisparity, 115793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler numberOfDisparities, SADWindowSize ); 116793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} 117793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler 118793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid cvValidateDisparity( CvArr* _disp, const CvArr* _cost, int minDisparity, 119793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler int numberOfDisparities, int disp12MaxDiff ) 120793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler{ 121793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cv::Mat disp = cv::cvarrToMat(_disp), cost = cv::cvarrToMat(_cost); 122793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler cv::validateDisparity( disp, cost, minDisparity, numberOfDisparities, disp12MaxDiff ); 123793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler} 124