• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  By downloading, copying, installing or using the software you agree to this license.
3  *  If you do not agree to this license, do not download, install,
4  *  copy or use the software.
5  *
6  *
7  *  License Agreement
8  *  For Open Source Computer Vision Library
9  *  (3 - clause BSD License)
10  *
11  *  Redistribution and use in source and binary forms, with or without modification,
12  *  are permitted provided that the following conditions are met :
13  *
14  *  * Redistributions of source code must retain the above copyright notice,
15  *  this list of conditions and the following disclaimer.
16  *
17  *  * Redistributions in binary form must reproduce the above copyright notice,
18  *  this list of conditions and the following disclaimer in the documentation
19  *  and / or other materials provided with the distribution.
20  *
21  *  * Neither the names of the copyright holders nor the names of the contributors
22  *  may be used to endorse or promote products derived from this software
23  *  without specific prior written permission.
24  *
25  *  This software is provided by the copyright holders and contributors "as is" and
26  *  any express or implied warranties, including, but not limited to, the implied
27  *  warranties of merchantability and fitness for a particular purpose are disclaimed.
28  *  In no event shall copyright holders or contributors be liable for any direct,
29  *  indirect, incidental, special, exemplary, or consequential damages
30  *  (including, but not limited to, procurement of substitute goods or services;
31  *  loss of use, data, or profits; or business interruption) however caused
32  *  and on any theory of liability, whether in contract, strict liability,
33  *  or tort(including negligence or otherwise) arising in any way out of
34  *  the use of this software, even if advised of the possibility of such damage.
35  */
36 
37 #include "perf_precomp.hpp"
38 
39 namespace opencv_test
40 {
41 using namespace perf;
42 using namespace testing;
43 
44 static void MakeArtificialExample(Mat& dst_left_view, Mat& dst_view);
45 
46 CV_ENUM(SGBMModes, StereoSGBM::MODE_SGBM, StereoSGBM::MODE_SGBM_3WAY, StereoSGBM::MODE_HH4);
47 typedef tuple<Size, int, SGBMModes> SGBMParams;
48 typedef TestBaseWithParam<SGBMParams> TestStereoCorrespSGBM;
49 
50 #ifndef _DEBUG
51 PERF_TEST_P( TestStereoCorrespSGBM, SGBM, Combine(Values(Size(1280,720),Size(640,480)), Values(256,128), SGBMModes::all()) )
52 #else
53 PERF_TEST_P( TestStereoCorrespSGBM, DISABLED_TooLongInDebug_SGBM, Combine(Values(Size(1280,720),Size(640,480)), Values(256,128), SGBMModes::all()) )
54 #endif
55 {
56     SGBMParams params = GetParam();
57 
58     Size sz              = get<0>(params);
59     int num_disparities  = get<1>(params);
60     int mode             = get<2>(params);
61 
62     Mat src_left(sz, CV_8UC3);
63     Mat src_right(sz, CV_8UC3);
64     Mat dst(sz, CV_16S);
65 
66     MakeArtificialExample(src_left,src_right);
67 
68     int wsize = 3;
69     int P1 = 8*src_left.channels()*wsize*wsize;
TEST_CYCLE()70     TEST_CYCLE()
71     {
72         Ptr<StereoSGBM> sgbm = StereoSGBM::create(0,num_disparities,wsize,P1,4*P1,1,63,25,0,0,mode);
73         sgbm->compute(src_left,src_right,dst);
74     }
75 
76     SANITY_CHECK(dst, .01, ERROR_RELATIVE);
77 }
78 
79 typedef tuple<Size, int> BMParams;
80 typedef TestBaseWithParam<BMParams> TestStereoCorrespBM;
81 
82 PERF_TEST_P(TestStereoCorrespBM, BM, Combine(Values(Size(1280, 720), Size(640, 480)), Values(256, 128)))
83 {
84     BMParams params = GetParam();
85     Size sz = get<0>(params);
86     int num_disparities = get<1>(params);
87 
88     Mat src_left(sz, CV_8UC1);
89     Mat src_right(sz, CV_8UC1);
90     Mat dst(sz, CV_16S);
91 
92     MakeArtificialExample(src_left, src_right);
93 
94     int wsize = 21;
TEST_CYCLE()95     TEST_CYCLE()
96     {
97         Ptr<StereoBM> bm = StereoBM::create(num_disparities, wsize);
98         bm->compute(src_left, src_right, dst);
99     }
100 
101     SANITY_CHECK(dst, .01, ERROR_RELATIVE);
102 }
103 
MakeArtificialExample(Mat & dst_left_view,Mat & dst_right_view)104 void MakeArtificialExample(Mat& dst_left_view, Mat& dst_right_view)
105 {
106     RNG rng(0);
107     int w = dst_left_view.cols;
108     int h = dst_left_view.rows;
109 
110     //params:
111     unsigned char bg_level = (unsigned char)rng.uniform(0.0,255.0);
112     unsigned char fg_level = (unsigned char)rng.uniform(0.0,255.0);
113     int rect_width  = (int)rng.uniform(w/16,w/2);
114     int rect_height = (int)rng.uniform(h/16,h/2);
115     int rect_disparity = (int)(0.15*w);
116     double sigma = 3.0;
117 
118     int rect_x_offset = (w-rect_width) /2;
119     int rect_y_offset = (h-rect_height)/2;
120 
121     if(dst_left_view.channels()==3)
122     {
123         dst_left_view  = Scalar(Vec3b(bg_level,bg_level,bg_level));
124         dst_right_view = Scalar(Vec3b(bg_level,bg_level,bg_level));
125     }
126     else
127     {
128         dst_left_view  = Scalar(bg_level);
129         dst_right_view = Scalar(bg_level);
130     }
131 
132     Mat dst_left_view_rect = Mat(dst_left_view, Rect(rect_x_offset,rect_y_offset,rect_width,rect_height));
133     if(dst_left_view.channels()==3)
134         dst_left_view_rect = Scalar(Vec3b(fg_level,fg_level,fg_level));
135     else
136         dst_left_view_rect = Scalar(fg_level);
137 
138     rect_x_offset-=rect_disparity;
139 
140     Mat dst_right_view_rect = Mat(dst_right_view, Rect(rect_x_offset,rect_y_offset,rect_width,rect_height));
141     if(dst_right_view.channels()==3)
142         dst_right_view_rect = Scalar(Vec3b(fg_level,fg_level,fg_level));
143     else
144         dst_right_view_rect = Scalar(fg_level);
145 
146     //add some gaussian noise:
147     unsigned char *l, *r;
148     for(int i=0;i<h;i++)
149     {
150         l = dst_left_view.ptr(i);
151         r = dst_right_view.ptr(i);
152 
153         if(dst_left_view.channels()==3)
154         {
155             for(int j=0;j<w;j++)
156             {
157                 l[0] = saturate_cast<unsigned char>(l[0] + rng.gaussian(sigma));
158                 l[1] = saturate_cast<unsigned char>(l[1] + rng.gaussian(sigma));
159                 l[2] = saturate_cast<unsigned char>(l[2] + rng.gaussian(sigma));
160                 l+=3;
161 
162                 r[0] = saturate_cast<unsigned char>(r[0] + rng.gaussian(sigma));
163                 r[1] = saturate_cast<unsigned char>(r[1] + rng.gaussian(sigma));
164                 r[2] = saturate_cast<unsigned char>(r[2] + rng.gaussian(sigma));
165                 r+=3;
166             }
167         }
168         else
169         {
170             for(int j=0;j<w;j++)
171             {
172                 l[0] = saturate_cast<unsigned char>(l[0] + rng.gaussian(sigma));
173                 l++;
174 
175                 r[0] = saturate_cast<unsigned char>(r[0] + rng.gaussian(sigma));
176                 r++;
177             }
178         }
179     }
180 }
181 
182 }
183