1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
10 // Intel License Agreement
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000, Intel Corporation, all rights reserved.
14 // Third party copyrights are property of their respective owners.
16 // Redistribution and use in source and binary forms, with or without modification,
17 // are permitted provided that the following conditions are met:
19 // * Redistribution's of source code must retain the above copyright notice,
20 // this list of conditions and the following disclaimer.
22 // * Redistribution's in binary form must reproduce the above copyright notice,
23 // this list of conditions and the following disclaimer in the documentation
24 // and/or other materials provided with the distribution.
26 // * The name of Intel Corporation may not be used to endorse or promote products
27 // derived from this software without specific prior written permission.
29 // This software is provided by the copyright holders and contributors "as is" and
30 // any express or implied warranties, including, but not limited to, the implied
31 // warranties of merchantability and fitness for a particular purpose are disclaimed.
32 // In no event shall the Intel Corporation or contributors be liable for any direct,
33 // indirect, incidental, special, exemplary, or consequential damages
34 // (including, but not limited to, procurement of substitute goods or services;
35 // loss of use, data, or profits; or business interruption) however caused
36 // and on any theory of liability, whether in contract, strict liability,
37 // or tort (including negligence or otherwise) arising in any way out of
38 // the use of this software, even if advised of the possibility of such damage.
43 This is a regression test for stereo matching algorithms. This test gets some quality metrics
44 discribed in "A Taxonomy and Evaluation of Dense Two-Frame Stereo Correspondence Algorithms".
45 Daniel Scharstein, Richard Szeliski
48 #include "test_precomp.hpp"
55 const float EVAL_BAD_THRESH = 1.f;
56 const int EVAL_TEXTURELESS_WIDTH = 3;
57 const float EVAL_TEXTURELESS_THRESH = 4.f;
58 const float EVAL_DISP_THRESH = 1.f;
59 const float EVAL_DISP_GAP = 2.f;
60 const int EVAL_DISCONT_WIDTH = 9;
61 const int EVAL_IGNORE_BORDER = 10;
63 const int ERROR_KINDS_COUNT = 6;
65 //============================== quality measuring functions =================================================
68 Calculate textureless regions of image (regions where the squared horizontal intensity gradient averaged over
69 a square window of size=evalTexturelessWidth is below a threshold=evalTexturelessThresh) and textured regions.
71 void computeTextureBasedMasks( const Mat& _img, Mat* texturelessMask, Mat* texturedMask,
72 int texturelessWidth = EVAL_TEXTURELESS_WIDTH, float texturelessThresh = EVAL_TEXTURELESS_THRESH )
74 if( !texturelessMask && !texturedMask )
77 CV_Error( CV_StsBadArg, "img is empty" );
80 if( _img.channels() > 1)
82 Mat tmp; cvtColor( _img, tmp, CV_BGR2GRAY ); img = tmp;
84 Mat dxI; Sobel( img, dxI, CV_32FC1, 1, 0, 3 );
85 Mat dxI2; pow( dxI / 8.f/*normalize*/, 2, dxI2 );
86 Mat avgDxI2; boxFilter( dxI2, avgDxI2, CV_32FC1, Size(texturelessWidth,texturelessWidth) );
89 *texturelessMask = avgDxI2 < texturelessThresh;
91 *texturedMask = avgDxI2 >= texturelessThresh;
94 void checkTypeAndSizeOfDisp( const Mat& dispMap, const Size* sz )
97 CV_Error( CV_StsBadArg, "dispMap is empty" );
98 if( dispMap.type() != CV_32FC1 )
99 CV_Error( CV_StsBadArg, "dispMap must have CV_32FC1 type" );
100 if( sz && (dispMap.rows != sz->height || dispMap.cols != sz->width) )
101 CV_Error( CV_StsBadArg, "dispMap has incorrect size" );
104 void checkTypeAndSizeOfMask( const Mat& mask, Size sz )
107 CV_Error( CV_StsBadArg, "mask is empty" );
108 if( mask.type() != CV_8UC1 )
109 CV_Error( CV_StsBadArg, "mask must have CV_8UC1 type" );
110 if( mask.rows != sz.height || mask.cols != sz.width )
111 CV_Error( CV_StsBadArg, "mask has incorrect size" );
114 void checkDispMapsAndUnknDispMasks( const Mat& leftDispMap, const Mat& rightDispMap,
115 const Mat& leftUnknDispMask, const Mat& rightUnknDispMask )
117 // check type and size of disparity maps
118 checkTypeAndSizeOfDisp( leftDispMap, 0 );
119 if( !rightDispMap.empty() )
121 Size sz = leftDispMap.size();
122 checkTypeAndSizeOfDisp( rightDispMap, &sz );
125 // check size and type of unknown disparity maps
126 if( !leftUnknDispMask.empty() )
127 checkTypeAndSizeOfMask( leftUnknDispMask, leftDispMap.size() );
128 if( !rightUnknDispMask.empty() )
129 checkTypeAndSizeOfMask( rightUnknDispMask, rightDispMap.size() );
131 // check values of disparity maps (known disparity values musy be positive)
132 double leftMinVal = 0, rightMinVal = 0;
133 if( leftUnknDispMask.empty() )
134 minMaxLoc( leftDispMap, &leftMinVal );
136 minMaxLoc( leftDispMap, &leftMinVal, 0, 0, 0, ~leftUnknDispMask );
137 if( !rightDispMap.empty() )
139 if( rightUnknDispMask.empty() )
140 minMaxLoc( rightDispMap, &rightMinVal );
142 minMaxLoc( rightDispMap, &rightMinVal, 0, 0, 0, ~rightUnknDispMask );
144 if( leftMinVal < 0 || rightMinVal < 0)
145 CV_Error( CV_StsBadArg, "known disparity values must be positive" );
149 Calculate occluded regions of reference image (left image) (regions that are occluded in the matching image (right image),
150 i.e., where the forward-mapped disparity lands at a location with a larger (nearer) disparity) and non occluded regions.
152 void computeOcclusionBasedMasks( const Mat& leftDisp, const Mat& _rightDisp,
153 Mat* occludedMask, Mat* nonOccludedMask,
154 const Mat& leftUnknDispMask = Mat(), const Mat& rightUnknDispMask = Mat(),
155 float dispThresh = EVAL_DISP_THRESH )
157 if( !occludedMask && !nonOccludedMask )
159 checkDispMapsAndUnknDispMasks( leftDisp, _rightDisp, leftUnknDispMask, rightUnknDispMask );
162 if( _rightDisp.empty() )
164 if( !rightUnknDispMask.empty() )
165 CV_Error( CV_StsBadArg, "rightUnknDispMask must be empty if _rightDisp is empty" );
166 rightDisp.create(leftDisp.size(), CV_32FC1);
167 rightDisp.setTo(Scalar::all(0) );
168 for( int leftY = 0; leftY < leftDisp.rows; leftY++ )
170 for( int leftX = 0; leftX < leftDisp.cols; leftX++ )
172 if( !leftUnknDispMask.empty() && leftUnknDispMask.at<uchar>(leftY,leftX) )
174 float leftDispVal = leftDisp.at<float>(leftY, leftX);
175 int rightX = leftX - cvRound(leftDispVal), rightY = leftY;
177 rightDisp.at<float>(rightY,rightX) = max(rightDisp.at<float>(rightY,rightX), leftDispVal);
182 _rightDisp.copyTo(rightDisp);
186 occludedMask->create(leftDisp.size(), CV_8UC1);
187 occludedMask->setTo(Scalar::all(0) );
189 if( nonOccludedMask )
191 nonOccludedMask->create(leftDisp.size(), CV_8UC1);
192 nonOccludedMask->setTo(Scalar::all(0) );
194 for( int leftY = 0; leftY < leftDisp.rows; leftY++ )
196 for( int leftX = 0; leftX < leftDisp.cols; leftX++ )
198 if( !leftUnknDispMask.empty() && leftUnknDispMask.at<uchar>(leftY,leftX) )
200 float leftDispVal = leftDisp.at<float>(leftY, leftX);
201 int rightX = leftX - cvRound(leftDispVal), rightY = leftY;
202 if( rightX < 0 && occludedMask )
203 occludedMask->at<uchar>(leftY, leftX) = 255;
206 if( !rightUnknDispMask.empty() && rightUnknDispMask.at<uchar>(rightY,rightX) )
208 float rightDispVal = rightDisp.at<float>(rightY, rightX);
209 if( rightDispVal > leftDispVal + dispThresh )
212 occludedMask->at<uchar>(leftY, leftX) = 255;
216 if( nonOccludedMask )
217 nonOccludedMask->at<uchar>(leftY, leftX) = 255;
225 Calculate depth discontinuty regions: pixels whose neiboring disparities differ by more than
226 dispGap, dilated by window of width discontWidth.
228 void computeDepthDiscontMask( const Mat& disp, Mat& depthDiscontMask, const Mat& unknDispMask = Mat(),
229 float dispGap = EVAL_DISP_GAP, int discontWidth = EVAL_DISCONT_WIDTH )
232 CV_Error( CV_StsBadArg, "disp is empty" );
233 if( disp.type() != CV_32FC1 )
234 CV_Error( CV_StsBadArg, "disp must have CV_32FC1 type" );
235 if( !unknDispMask.empty() )
236 checkTypeAndSizeOfMask( unknDispMask, disp.size() );
238 Mat curDisp; disp.copyTo( curDisp );
239 if( !unknDispMask.empty() )
240 curDisp.setTo( Scalar(numeric_limits<float>::min()), unknDispMask );
241 Mat maxNeighbDisp; dilate( curDisp, maxNeighbDisp, Mat(3, 3, CV_8UC1, Scalar(1)) );
242 if( !unknDispMask.empty() )
243 curDisp.setTo( Scalar(numeric_limits<float>::max()), unknDispMask );
244 Mat minNeighbDisp; erode( curDisp, minNeighbDisp, Mat(3, 3, CV_8UC1, Scalar(1)) );
245 depthDiscontMask = max( (Mat)(maxNeighbDisp-disp), (Mat)(disp-minNeighbDisp) ) > dispGap;
246 if( !unknDispMask.empty() )
247 depthDiscontMask &= ~unknDispMask;
248 dilate( depthDiscontMask, depthDiscontMask, Mat(discontWidth, discontWidth, CV_8UC1, Scalar(1)) );
252 Get evaluation masks excluding a border.
254 Mat getBorderedMask( Size maskSize, int border = EVAL_IGNORE_BORDER )
256 CV_Assert( border >= 0 );
257 Mat mask(maskSize, CV_8UC1, Scalar(0));
258 int w = maskSize.width - 2*border, h = maskSize.height - 2*border;
260 mask.setTo(Scalar(0));
262 mask( Rect(Point(border,border),Size(w,h)) ).setTo(Scalar(255));
267 Calculate root-mean-squared error between the computed disparity map (computedDisp) and ground truth map (groundTruthDisp).
269 float dispRMS( const Mat& computedDisp, const Mat& groundTruthDisp, const Mat& mask )
271 checkTypeAndSizeOfDisp( groundTruthDisp, 0 );
272 Size sz = groundTruthDisp.size();
273 checkTypeAndSizeOfDisp( computedDisp, &sz );
275 int pointsCount = sz.height*sz.width;
278 checkTypeAndSizeOfMask( mask, sz );
279 pointsCount = countNonZero(mask);
281 return 1.f/sqrt((float)pointsCount) * (float)cvtest::norm(computedDisp, groundTruthDisp, NORM_L2, mask);
285 Calculate fraction of bad matching pixels.
287 float badMatchPxlsFraction( const Mat& computedDisp, const Mat& groundTruthDisp, const Mat& mask,
288 float _badThresh = EVAL_BAD_THRESH )
290 int badThresh = cvRound(_badThresh);
291 checkTypeAndSizeOfDisp( groundTruthDisp, 0 );
292 Size sz = groundTruthDisp.size();
293 checkTypeAndSizeOfDisp( computedDisp, &sz );
296 absdiff( computedDisp, groundTruthDisp, badPxlsMap );
297 badPxlsMap = badPxlsMap > badThresh;
298 int pointsCount = sz.height*sz.width;
301 checkTypeAndSizeOfMask( mask, sz );
302 badPxlsMap = badPxlsMap & mask;
303 pointsCount = countNonZero(mask);
305 return 1.f/pointsCount * countNonZero(badPxlsMap);
308 //===================== regression test for stereo matching algorithms ==============================
310 const string ALGORITHMS_DIR = "stereomatching/algorithms/";
311 const string DATASETS_DIR = "stereomatching/datasets/";
312 const string DATASETS_FILE = "datasets.xml";
314 const string RUN_PARAMS_FILE = "_params.xml";
315 const string RESULT_FILE = "_res.xml";
317 const string LEFT_IMG_NAME = "im2.png";
318 const string RIGHT_IMG_NAME = "im6.png";
319 const string TRUE_LEFT_DISP_NAME = "disp2.png";
320 const string TRUE_RIGHT_DISP_NAME = "disp6.png";
322 string ERROR_PREFIXES[] = { "borderedAll",
326 "borderedTextureless",
327 "borderedDepthDiscont" }; // size of ERROR_KINDS_COUNT
330 const string RMS_STR = "RMS";
331 const string BAD_PXLS_FRACTION_STR = "BadPxlsFraction";
333 class QualityEvalParams
336 QualityEvalParams() { setDefaults(); }
337 QualityEvalParams( int _ignoreBorder )
340 ignoreBorder = _ignoreBorder;
344 badThresh = EVAL_BAD_THRESH;
345 texturelessWidth = EVAL_TEXTURELESS_WIDTH;
346 texturelessThresh = EVAL_TEXTURELESS_THRESH;
347 dispThresh = EVAL_DISP_THRESH;
348 dispGap = EVAL_DISP_GAP;
349 discontWidth = EVAL_DISCONT_WIDTH;
350 ignoreBorder = EVAL_IGNORE_BORDER;
353 int texturelessWidth;
354 float texturelessThresh;
361 class CV_StereoMatchingTest : public cvtest::BaseTest
364 CV_StereoMatchingTest()
365 { rmsEps.resize( ERROR_KINDS_COUNT, 0.01f ); fracEps.resize( ERROR_KINDS_COUNT, 1.e-6f ); }
367 // assumed that left image is a reference image
368 virtual int runStereoMatchingAlgorithm( const Mat& leftImg, const Mat& rightImg,
369 Mat& leftDisp, Mat& rightDisp, int caseIdx ) = 0; // return ignored border width
371 int readDatasetsParams( FileStorage& fs );
372 virtual int readRunParams( FileStorage& fs );
373 void writeErrors( const string& errName, const vector<float>& errors, FileStorage* fs = 0 );
374 void readErrors( FileNode& fn, const string& errName, vector<float>& errors );
375 int compareErrors( const vector<float>& calcErrors, const vector<float>& validErrors,
376 const vector<float>& eps, const string& errName );
377 int processStereoMatchingResults( FileStorage& fs, int caseIdx, bool isWrite,
378 const Mat& leftImg, const Mat& rightImg,
379 const Mat& trueLeftDisp, const Mat& trueRightDisp,
380 const Mat& leftDisp, const Mat& rightDisp,
381 const QualityEvalParams& qualityEvalParams );
384 vector<float> rmsEps;
385 vector<float> fracEps;
392 map<string, DatasetParams> datasetsParams;
394 vector<string> caseNames;
395 vector<string> caseDatasets;
398 void CV_StereoMatchingTest::run(int)
400 string dataPath = ts->get_data_path();
401 string algorithmName = name;
402 assert( !algorithmName.empty() );
403 if( dataPath.empty() )
405 ts->printf( cvtest::TS::LOG, "dataPath is empty" );
406 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ARG_CHECK );
410 FileStorage datasetsFS( dataPath + DATASETS_DIR + DATASETS_FILE, FileStorage::READ );
411 int code = readDatasetsParams( datasetsFS );
412 if( code != cvtest::TS::OK )
414 ts->set_failed_test_info( code );
417 FileStorage runParamsFS( dataPath + ALGORITHMS_DIR + algorithmName + RUN_PARAMS_FILE, FileStorage::READ );
418 code = readRunParams( runParamsFS );
419 if( code != cvtest::TS::OK )
421 ts->set_failed_test_info( code );
425 string fullResultFilename = dataPath + ALGORITHMS_DIR + algorithmName + RESULT_FILE;
426 FileStorage resFS( fullResultFilename, FileStorage::READ );
427 bool isWrite = true; // write or compare results
428 if( resFS.isOpened() )
432 resFS.open( fullResultFilename, FileStorage::WRITE );
433 if( !resFS.isOpened() )
435 ts->printf( cvtest::TS::LOG, "file %s can not be read or written\n", fullResultFilename.c_str() );
436 ts->set_failed_test_info( cvtest::TS::FAIL_BAD_ARG_CHECK );
439 resFS << "stereo_matching" << "{";
442 int progress = 0, caseCount = (int)caseNames.size();
443 for( int ci = 0; ci < caseCount; ci++)
445 progress = update_progress( progress, ci, caseCount, 0 );
446 printf("progress: %d%%\n", progress);
448 string datasetName = caseDatasets[ci];
449 string datasetFullDirName = dataPath + DATASETS_DIR + datasetName + "/";
450 Mat leftImg = imread(datasetFullDirName + LEFT_IMG_NAME);
451 Mat rightImg = imread(datasetFullDirName + RIGHT_IMG_NAME);
452 Mat trueLeftDisp = imread(datasetFullDirName + TRUE_LEFT_DISP_NAME, 0);
453 Mat trueRightDisp = imread(datasetFullDirName + TRUE_RIGHT_DISP_NAME, 0);
455 if( leftImg.empty() || rightImg.empty() || trueLeftDisp.empty() )
457 ts->printf( cvtest::TS::LOG, "images or left ground-truth disparities of dataset %s can not be read", datasetName.c_str() );
458 code = cvtest::TS::FAIL_INVALID_TEST_DATA;
461 int dispScaleFactor = datasetsParams[datasetName].dispScaleFactor;
462 Mat tmp; trueLeftDisp.convertTo( tmp, CV_32FC1, 1.f/dispScaleFactor ); trueLeftDisp = tmp; tmp.release();
463 if( !trueRightDisp.empty() )
464 trueRightDisp.convertTo( tmp, CV_32FC1, 1.f/dispScaleFactor ); trueRightDisp = tmp; tmp.release();
466 Mat leftDisp, rightDisp;
467 int ignBorder = max(runStereoMatchingAlgorithm(leftImg, rightImg, leftDisp, rightDisp, ci), EVAL_IGNORE_BORDER);
468 leftDisp.convertTo( tmp, CV_32FC1 ); leftDisp = tmp; tmp.release();
469 rightDisp.convertTo( tmp, CV_32FC1 ); rightDisp = tmp; tmp.release();
471 int tempCode = processStereoMatchingResults( resFS, ci, isWrite,
472 leftImg, rightImg, trueLeftDisp, trueRightDisp, leftDisp, rightDisp, QualityEvalParams(ignBorder));
473 code = tempCode==cvtest::TS::OK ? code : tempCode;
477 resFS << "}"; // "stereo_matching"
479 ts->set_failed_test_info( code );
482 void calcErrors( const Mat& leftImg, const Mat& /*rightImg*/,
483 const Mat& trueLeftDisp, const Mat& trueRightDisp,
484 const Mat& trueLeftUnknDispMask, const Mat& trueRightUnknDispMask,
485 const Mat& calcLeftDisp, const Mat& /*calcRightDisp*/,
486 vector<float>& rms, vector<float>& badPxlsFractions,
487 const QualityEvalParams& qualityEvalParams )
489 Mat texturelessMask, texturedMask;
490 computeTextureBasedMasks( leftImg, &texturelessMask, &texturedMask,
491 qualityEvalParams.texturelessWidth, qualityEvalParams.texturelessThresh );
492 Mat occludedMask, nonOccludedMask;
493 computeOcclusionBasedMasks( trueLeftDisp, trueRightDisp, &occludedMask, &nonOccludedMask,
494 trueLeftUnknDispMask, trueRightUnknDispMask, qualityEvalParams.dispThresh);
495 Mat depthDiscontMask;
496 computeDepthDiscontMask( trueLeftDisp, depthDiscontMask, trueLeftUnknDispMask,
497 qualityEvalParams.dispGap, qualityEvalParams.discontWidth);
499 Mat borderedKnownMask = getBorderedMask( leftImg.size(), qualityEvalParams.ignoreBorder ) & ~trueLeftUnknDispMask;
501 nonOccludedMask &= borderedKnownMask;
502 occludedMask &= borderedKnownMask;
503 texturedMask &= nonOccludedMask; // & borderedKnownMask
504 texturelessMask &= nonOccludedMask; // & borderedKnownMask
505 depthDiscontMask &= nonOccludedMask; // & borderedKnownMask
507 rms.resize(ERROR_KINDS_COUNT);
508 rms[0] = dispRMS( calcLeftDisp, trueLeftDisp, borderedKnownMask );
509 rms[1] = dispRMS( calcLeftDisp, trueLeftDisp, nonOccludedMask );
510 rms[2] = dispRMS( calcLeftDisp, trueLeftDisp, occludedMask );
511 rms[3] = dispRMS( calcLeftDisp, trueLeftDisp, texturedMask );
512 rms[4] = dispRMS( calcLeftDisp, trueLeftDisp, texturelessMask );
513 rms[5] = dispRMS( calcLeftDisp, trueLeftDisp, depthDiscontMask );
515 badPxlsFractions.resize(ERROR_KINDS_COUNT);
516 badPxlsFractions[0] = badMatchPxlsFraction( calcLeftDisp, trueLeftDisp, borderedKnownMask, qualityEvalParams.badThresh );
517 badPxlsFractions[1] = badMatchPxlsFraction( calcLeftDisp, trueLeftDisp, nonOccludedMask, qualityEvalParams.badThresh );
518 badPxlsFractions[2] = badMatchPxlsFraction( calcLeftDisp, trueLeftDisp, occludedMask, qualityEvalParams.badThresh );
519 badPxlsFractions[3] = badMatchPxlsFraction( calcLeftDisp, trueLeftDisp, texturedMask, qualityEvalParams.badThresh );
520 badPxlsFractions[4] = badMatchPxlsFraction( calcLeftDisp, trueLeftDisp, texturelessMask, qualityEvalParams.badThresh );
521 badPxlsFractions[5] = badMatchPxlsFraction( calcLeftDisp, trueLeftDisp, depthDiscontMask, qualityEvalParams.badThresh );
524 int CV_StereoMatchingTest::processStereoMatchingResults( FileStorage& fs, int caseIdx, bool isWrite,
525 const Mat& leftImg, const Mat& rightImg,
526 const Mat& trueLeftDisp, const Mat& trueRightDisp,
527 const Mat& leftDisp, const Mat& rightDisp,
528 const QualityEvalParams& qualityEvalParams )
530 // rightDisp is not used in current test virsion
531 int code = cvtest::TS::OK;
532 assert( fs.isOpened() );
533 assert( trueLeftDisp.type() == CV_32FC1 && trueRightDisp.type() == CV_32FC1 );
534 assert( leftDisp.type() == CV_32FC1 && rightDisp.type() == CV_32FC1 );
536 // get masks for unknown ground truth disparity values
537 Mat leftUnknMask, rightUnknMask;
538 DatasetParams params = datasetsParams[caseDatasets[caseIdx]];
539 absdiff( trueLeftDisp, Scalar(params.dispUnknVal), leftUnknMask );
540 leftUnknMask = leftUnknMask < numeric_limits<float>::epsilon();
541 assert(leftUnknMask.type() == CV_8UC1);
542 if( !trueRightDisp.empty() )
544 absdiff( trueRightDisp, Scalar(params.dispUnknVal), rightUnknMask );
545 rightUnknMask = rightUnknMask < numeric_limits<float>::epsilon();
546 assert(leftUnknMask.type() == CV_8UC1);
550 vector<float> rmss, badPxlsFractions;
551 calcErrors( leftImg, rightImg, trueLeftDisp, trueRightDisp, leftUnknMask, rightUnknMask,
552 leftDisp, rightDisp, rmss, badPxlsFractions, qualityEvalParams );
556 fs << caseNames[caseIdx] << "{";
557 cvWriteComment( fs.fs, RMS_STR.c_str(), 0 );
558 writeErrors( RMS_STR, rmss, &fs );
559 cvWriteComment( fs.fs, BAD_PXLS_FRACTION_STR.c_str(), 0 );
560 writeErrors( BAD_PXLS_FRACTION_STR, badPxlsFractions, &fs );
561 fs << "}"; // datasetName
565 ts->printf( cvtest::TS::LOG, "\nquality of case named %s\n", caseNames[caseIdx].c_str() );
566 ts->printf( cvtest::TS::LOG, "%s\n", RMS_STR.c_str() );
567 writeErrors( RMS_STR, rmss );
568 ts->printf( cvtest::TS::LOG, "%s\n", BAD_PXLS_FRACTION_STR.c_str() );
569 writeErrors( BAD_PXLS_FRACTION_STR, badPxlsFractions );
571 FileNode fn = fs.getFirstTopLevelNode()[caseNames[caseIdx]];
572 vector<float> validRmss, validBadPxlsFractions;
574 readErrors( fn, RMS_STR, validRmss );
575 readErrors( fn, BAD_PXLS_FRACTION_STR, validBadPxlsFractions );
576 int tempCode = compareErrors( rmss, validRmss, rmsEps, RMS_STR );
577 code = tempCode==cvtest::TS::OK ? code : tempCode;
578 tempCode = compareErrors( badPxlsFractions, validBadPxlsFractions, fracEps, BAD_PXLS_FRACTION_STR );
579 code = tempCode==cvtest::TS::OK ? code : tempCode;
584 int CV_StereoMatchingTest::readDatasetsParams( FileStorage& fs )
588 ts->printf( cvtest::TS::LOG, "datasetsParams can not be read " );
589 return cvtest::TS::FAIL_INVALID_TEST_DATA;
591 datasetsParams.clear();
592 FileNode fn = fs.getFirstTopLevelNode();
594 for( int i = 0; i < (int)fn.size(); i+=3 )
597 DatasetParams params;
598 String sf = fn[i+1]; params.dispScaleFactor = atoi(sf.c_str());
599 String uv = fn[i+2]; params.dispUnknVal = atoi(uv.c_str());
600 datasetsParams[nm] = params;
602 return cvtest::TS::OK;
605 int CV_StereoMatchingTest::readRunParams( FileStorage& fs )
609 ts->printf( cvtest::TS::LOG, "runParams can not be read " );
610 return cvtest::TS::FAIL_INVALID_TEST_DATA;
613 caseDatasets.clear();
614 return cvtest::TS::OK;
617 void CV_StereoMatchingTest::writeErrors( const string& errName, const vector<float>& errors, FileStorage* fs )
619 assert( (int)errors.size() == ERROR_KINDS_COUNT );
620 vector<float>::const_iterator it = errors.begin();
622 for( int i = 0; i < ERROR_KINDS_COUNT; i++, ++it )
623 *fs << ERROR_PREFIXES[i] + errName << *it;
625 for( int i = 0; i < ERROR_KINDS_COUNT; i++, ++it )
626 ts->printf( cvtest::TS::LOG, "%s = %f\n", string(ERROR_PREFIXES[i]+errName).c_str(), *it );
629 void CV_StereoMatchingTest::readErrors( FileNode& fn, const string& errName, vector<float>& errors )
631 errors.resize( ERROR_KINDS_COUNT );
632 vector<float>::iterator it = errors.begin();
633 for( int i = 0; i < ERROR_KINDS_COUNT; i++, ++it )
634 fn[ERROR_PREFIXES[i]+errName] >> *it;
637 int CV_StereoMatchingTest::compareErrors( const vector<float>& calcErrors, const vector<float>& validErrors,
638 const vector<float>& eps, const string& errName )
640 assert( (int)calcErrors.size() == ERROR_KINDS_COUNT );
641 assert( (int)validErrors.size() == ERROR_KINDS_COUNT );
642 assert( (int)eps.size() == ERROR_KINDS_COUNT );
643 vector<float>::const_iterator calcIt = calcErrors.begin(),
644 validIt = validErrors.begin(),
647 for( int i = 0; i < ERROR_KINDS_COUNT; i++, ++calcIt, ++validIt, ++epsIt )
648 if( *calcIt - *validIt > *epsIt )
650 ts->printf( cvtest::TS::LOG, "bad accuracy of %s (valid=%f; calc=%f)\n", string(ERROR_PREFIXES[i]+errName).c_str(), *validIt, *calcIt );
653 return ok ? cvtest::TS::OK : cvtest::TS::FAIL_BAD_ACCURACY;
656 //----------------------------------- StereoGC test -----------------------------------------------------
658 class CV_StereoGCTest : public CV_StereoMatchingTest
664 fill(rmsEps.begin(), rmsEps.end(), 3.f);
665 fracEps[0] = 0.05f; // all
666 fracEps[1] = 0.05f; // noOccl
667 fracEps[2] = 0.25f; // occl
668 fracEps[3] = 0.05f; // textured
669 fracEps[4] = 0.10f; // textureless
670 fracEps[5] = 0.10f; // borderedDepthDiscont
678 vector<RunParams> caseRunParams;
680 virtual int readRunParams( FileStorage& fs )
682 int code = CV_StereoMatchingTest::readRunParams(fs);
683 FileNode fn = fs.getFirstTopLevelNode();
685 for( int i = 0; i < (int)fn.size(); i+=4 )
687 String caseName = fn[i], datasetName = fn[i+1];
689 String ndisp = fn[i+2]; params.ndisp = atoi(ndisp.c_str());
690 String iterCount = fn[i+3]; params.iterCount = atoi(iterCount.c_str());
691 caseNames.push_back( caseName );
692 caseDatasets.push_back( datasetName );
693 caseRunParams.push_back( params );
698 virtual int runStereoMatchingAlgorithm( const Mat& _leftImg, const Mat& _rightImg,
699 Mat& leftDisp, Mat& rightDisp, int caseIdx )
701 RunParams params = caseRunParams[caseIdx];
702 assert( _leftImg.type() == CV_8UC3 && _rightImg.type() == CV_8UC3 );
703 Mat leftImg, rightImg, tmp;
704 cvtColor( _leftImg, leftImg, CV_BGR2GRAY );
705 cvtColor( _rightImg, rightImg, CV_BGR2GRAY );
707 leftDisp.create( leftImg.size(), CV_16SC1 );
708 rightDisp.create( rightImg.size(), CV_16SC1 );
710 CvMat _limg = leftImg, _rimg = rightImg, _ldisp = leftDisp, _rdisp = rightDisp;
711 CvStereoGCState *state = cvCreateStereoGCState( params.ndisp, params.iterCount );
712 cvFindStereoCorrespondenceGC( &_limg, &_rimg, &_ldisp, &_rdisp, state );
713 cvReleaseStereoGCState( &state );
715 leftDisp = - leftDisp;
722 TEST(Legacy_StereoGC, regression) { CV_StereoGCTest test; test.safe_run(); }