1//M*//////////////////////////////////////////////////////////////////////////////////////
2//
3//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4//
5//  By downloading, copying, installing or using the software you agree to this license.
6//  If you do not agree to this license, do not download, install,
7//  copy or use the software.
8//
9//
10//                           License Agreement
11//                For Open Source Computer Vision Library
12//
13// Copyright (C) 2000, Intel Corporation, all rights reserved.
14// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
15// Third party copyrights are property of their respective owners.
16//
17// Redistribution and use in source and binary forms, with or without modification,
18// are permitted provided that the following conditions are met:
19//
20//   * Redistribution's of source code must retain the above copyright notice,
21//     this list of conditions and the following disclaimer.
22//
23//   * Redistribution's in binary form must reproduce the above copyright notice,
24//     this list of conditions and the following disclaimer in the documentation
25//     and/or other materials provided with the distribution.
26//
27//   * The name of the copyright holders may not be used to endorse or promote products
28//     derived from this software without specific prior written permission.
29//
30// This software is provided by the copyright holders and contributors "as is" and
31// any express or implied warranties, including, but not limited to, the implied
32// warranties of merchantability and fitness for a particular purpose are disclaimed.
33// In no event shall the Intel Corporation or contributors be liable for any direct,
34// indirect, incidental, special, exemplary, or consequential damages
35// (including, but not limited to, procurement of substitute goods or services;
36// loss of use, data, or profits; or business interruption) however caused
37// and on any theory of liability, whether in contract, strict liability,
38// or tort (including negligence or otherwise) arising in any way out of
39// the use of this software, even if advised of the possibility of such damage.
40//
41//M*/
42
43#include "precomp.hpp"
44#include "opencv2/calib3d/calib3d_c.h"
45
46CvStereoBMState* cvCreateStereoBMState( int /*preset*/, int numberOfDisparities )
47{
48    CvStereoBMState* state = (CvStereoBMState*)cvAlloc( sizeof(*state) );
49    if( !state )
50        return 0;
51
52    state->preFilterType = CV_STEREO_BM_XSOBEL; //CV_STEREO_BM_NORMALIZED_RESPONSE;
53    state->preFilterSize = 9;
54    state->preFilterCap = 31;
55    state->SADWindowSize = 15;
56    state->minDisparity = 0;
57    state->numberOfDisparities = numberOfDisparities > 0 ? numberOfDisparities : 64;
58    state->textureThreshold = 10;
59    state->uniquenessRatio = 15;
60    state->speckleRange = state->speckleWindowSize = 0;
61    state->trySmallerWindows = 0;
62    state->roi1 = state->roi2 = cvRect(0,0,0,0);
63    state->disp12MaxDiff = -1;
64
65    state->preFilteredImg0 = state->preFilteredImg1 = state->slidingSumBuf =
66    state->disp = state->cost = 0;
67
68    return state;
69}
70
71void cvReleaseStereoBMState( CvStereoBMState** state )
72{
73    if( !state )
74        CV_Error( CV_StsNullPtr, "" );
75
76    if( !*state )
77        return;
78
79    cvReleaseMat( &(*state)->preFilteredImg0 );
80    cvReleaseMat( &(*state)->preFilteredImg1 );
81    cvReleaseMat( &(*state)->slidingSumBuf );
82    cvReleaseMat( &(*state)->disp );
83    cvReleaseMat( &(*state)->cost );
84    cvFree( state );
85}
86
87void cvFindStereoCorrespondenceBM( const CvArr* leftarr, const CvArr* rightarr,
88                                   CvArr* disparr, CvStereoBMState* state )
89{
90    cv::Mat left = cv::cvarrToMat(leftarr), right = cv::cvarrToMat(rightarr);
91    const cv::Mat disp = cv::cvarrToMat(disparr);
92
93    CV_Assert( state != 0 );
94
95    cv::Ptr<cv::StereoBM> sm = cv::StereoBM::create(state->numberOfDisparities,
96                                                       state->SADWindowSize);
97    sm->setPreFilterType(state->preFilterType);
98    sm->setPreFilterSize(state->preFilterSize);
99    sm->setPreFilterCap(state->preFilterCap);
100    sm->setBlockSize(state->SADWindowSize);
101    sm->setNumDisparities(state->numberOfDisparities > 0 ? state->numberOfDisparities : 64);
102    sm->setTextureThreshold(state->textureThreshold);
103    sm->setUniquenessRatio(state->uniquenessRatio);
104    sm->setSpeckleRange(state->speckleRange);
105    sm->setSpeckleWindowSize(state->speckleWindowSize);
106    sm->setDisp12MaxDiff(state->disp12MaxDiff);
107
108    sm->compute(left, right, disp);
109}
110
111CvRect cvGetValidDisparityROI( CvRect roi1, CvRect roi2, int minDisparity,
112                              int numberOfDisparities, int SADWindowSize )
113{
114    return (CvRect)cv::getValidDisparityROI( roi1, roi2, minDisparity,
115                                            numberOfDisparities, SADWindowSize );
116}
117
118void cvValidateDisparity( CvArr* _disp, const CvArr* _cost, int minDisparity,
119                         int numberOfDisparities, int disp12MaxDiff )
120{
121    cv::Mat disp = cv::cvarrToMat(_disp), cost = cv::cvarrToMat(_cost);
122    cv::validateDisparity( disp, cost, minDisparity, numberOfDisparities, disp12MaxDiff );
123}
124