1 #include "perf_precomp.hpp"
2
3 using namespace std;
4 using namespace cv;
5 using namespace perf;
6 using namespace testing;
7 using std::tr1::make_tuple;
8 using std::tr1::get;
9
10 enum{HALF_SIZE=0, UPSIDE_DOWN, REFLECTION_X, REFLECTION_BOTH};
11
12 CV_ENUM(BorderMode, BORDER_CONSTANT, BORDER_REPLICATE)
13 CV_ENUM(InterType, INTER_NEAREST, INTER_LINEAR)
14 CV_ENUM(RemapMode, HALF_SIZE, UPSIDE_DOWN, REFLECTION_X, REFLECTION_BOTH)
15
16 typedef TestBaseWithParam< tr1::tuple<Size, InterType, BorderMode> > TestWarpAffine;
17 typedef TestBaseWithParam< tr1::tuple<Size, InterType, BorderMode> > TestWarpPerspective;
18 typedef TestBaseWithParam< tr1::tuple<Size, InterType, BorderMode, MatType> > TestWarpPerspectiveNear_t;
19 typedef TestBaseWithParam< tr1::tuple<MatType, Size, InterType, BorderMode, RemapMode> > TestRemap;
20
21 void update_map(const Mat& src, Mat& map_x, Mat& map_y, const int remapMode );
22
PERF_TEST_P(TestWarpAffine,WarpAffine,Combine (Values (szVGA,sz720p,sz1080p),InterType::all (),BorderMode::all ()))23 PERF_TEST_P( TestWarpAffine, WarpAffine,
24 Combine(
25 Values( szVGA, sz720p, sz1080p ),
26 InterType::all(),
27 BorderMode::all()
28 )
29 )
30 {
31 Size sz, szSrc(512, 512);
32 int borderMode, interType;
33 sz = get<0>(GetParam());
34 interType = get<1>(GetParam());
35 borderMode = get<2>(GetParam());
36 Scalar borderColor = Scalar::all(150);
37
38 Mat src(szSrc,CV_8UC4), dst(sz, CV_8UC4);
39 cvtest::fillGradient(src);
40 if(borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1);
41 Mat warpMat = getRotationMatrix2D(Point2f(src.cols/2.f, src.rows/2.f), 30., 2.2);
42 declare.in(src).out(dst);
43
44 TEST_CYCLE() warpAffine( src, dst, warpMat, sz, interType, borderMode, borderColor );
45
46 #ifdef ANDROID
47 SANITY_CHECK(dst, interType==INTER_LINEAR? 5 : 10);
48 #else
49 SANITY_CHECK(dst, 1);
50 #endif
51 }
52
PERF_TEST_P(TestWarpPerspective,WarpPerspective,Combine (Values (szVGA,sz720p,sz1080p),InterType::all (),BorderMode::all ()))53 PERF_TEST_P( TestWarpPerspective, WarpPerspective,
54 Combine(
55 Values( szVGA, sz720p, sz1080p ),
56 InterType::all(),
57 BorderMode::all()
58 )
59 )
60 {
61 Size sz, szSrc(512, 512);
62 int borderMode, interType;
63 sz = get<0>(GetParam());
64 interType = get<1>(GetParam());
65 borderMode = get<2>(GetParam());
66 Scalar borderColor = Scalar::all(150);
67
68 Mat src(szSrc,CV_8UC4), dst(sz, CV_8UC4);
69 cvtest::fillGradient(src);
70 if(borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1);
71 Mat rotMat = getRotationMatrix2D(Point2f(src.cols/2.f, src.rows/2.f), 30., 2.2);
72 Mat warpMat(3, 3, CV_64FC1);
73 for(int r=0; r<2; r++)
74 for(int c=0; c<3; c++)
75 warpMat.at<double>(r, c) = rotMat.at<double>(r, c);
76 warpMat.at<double>(2, 0) = .3/sz.width;
77 warpMat.at<double>(2, 1) = .3/sz.height;
78 warpMat.at<double>(2, 2) = 1;
79
80 declare.in(src).out(dst);
81
82 TEST_CYCLE() warpPerspective( src, dst, warpMat, sz, interType, borderMode, borderColor );
83
84 #ifdef ANDROID
85 SANITY_CHECK(dst, interType==INTER_LINEAR? 5 : 10);
86 #else
87 SANITY_CHECK(dst, 1);
88 #endif
89 }
90
91 PERF_TEST_P( TestWarpPerspectiveNear_t, WarpPerspectiveNear,
92 Combine(
93 Values( Size(640,480), Size(1920,1080), Size(2592,1944) ),
94 InterType::all(),
95 BorderMode::all(),
96 Values( CV_8UC1, CV_8UC4 )
97 )
98 )
99 {
100 Size size;
101 int borderMode, interType, type;
102 size = get<0>(GetParam());
103 interType = get<1>(GetParam());
104 borderMode = get<2>(GetParam());
105 type = get<3>(GetParam());
106 Scalar borderColor = Scalar::all(150);
107
108 Mat src(size, type), dst(size, type);
109 cvtest::fillGradient(src);
110 if(borderMode == BORDER_CONSTANT) cvtest::smoothBorder(src, borderColor, 1);
111 int shift = static_cast<int>(src.cols*0.04);
112 Mat srcVertices = (Mat_<Vec2f>(1, 4) << Vec2f(0, 0),
113 Vec2f(static_cast<float>(size.width-1), 0),
114 Vec2f(static_cast<float>(size.width-1), static_cast<float>(size.height-1)),
115 Vec2f(0, static_cast<float>(size.height-1)));
116 Mat dstVertices = (Mat_<Vec2f>(1, 4) << Vec2f(0, static_cast<float>(shift)),
117 Vec2f(static_cast<float>(size.width-shift/2), 0),
118 Vec2f(static_cast<float>(size.width-shift), static_cast<float>(size.height-shift)),
119 Vec2f(static_cast<float>(shift/2), static_cast<float>(size.height-1)));
120 Mat warpMat = getPerspectiveTransform(srcVertices, dstVertices);
121
122 declare.in(src).out(dst);
123 declare.time(100);
124
TEST_CYCLE()125 TEST_CYCLE()
126 {
127 warpPerspective( src, dst, warpMat, size, interType, borderMode, borderColor );
128 }
129
130 #ifdef ANDROID
131 SANITY_CHECK(dst, interType==INTER_LINEAR? 5 : 10);
132 #else
133 SANITY_CHECK(dst, 1);
134 #endif
135 }
136
PERF_TEST_P(TestRemap,remap,Combine (Values (TYPICAL_MAT_TYPES),Values (szVGA,sz720p,sz1080p),InterType::all (),BorderMode::all (),RemapMode::all ()))137 PERF_TEST_P( TestRemap, remap,
138 Combine(
139 Values( TYPICAL_MAT_TYPES ),
140 Values( szVGA, sz720p, sz1080p ),
141 InterType::all(),
142 BorderMode::all(),
143 RemapMode::all()
144 )
145 )
146 {
147 int type = get<0>(GetParam());
148 Size size = get<1>(GetParam());
149 int interpolationType = get<2>(GetParam());
150 int borderMode = get<3>(GetParam());
151 int remapMode = get<4>(GetParam());
152 unsigned int height = size.height;
153 unsigned int width = size.width;
154 Mat source(height, width, type);
155 Mat destination;
156 Mat map_x(height, width, CV_32F);
157 Mat map_y(height, width, CV_32F);
158
159 declare.in(source, WARMUP_RNG);
160
161 update_map(source, map_x, map_y, remapMode);
162
163 TEST_CYCLE()
164 {
165 remap(source, destination, map_x, map_y, interpolationType, borderMode);
166 }
167
168 SANITY_CHECK(destination, 1);
169 }
170
update_map(const Mat & src,Mat & map_x,Mat & map_y,const int remapMode)171 void update_map(const Mat& src, Mat& map_x, Mat& map_y, const int remapMode )
172 {
173 for( int j = 0; j < src.rows; j++ )
174 {
175 for( int i = 0; i < src.cols; i++ )
176 {
177 switch( remapMode )
178 {
179 case HALF_SIZE:
180 if( i > src.cols*0.25 && i < src.cols*0.75 && j > src.rows*0.25 && j < src.rows*0.75 )
181 {
182 map_x.at<float>(j,i) = 2*( i - src.cols*0.25f ) + 0.5f ;
183 map_y.at<float>(j,i) = 2*( j - src.rows*0.25f ) + 0.5f ;
184 }
185 else
186 {
187 map_x.at<float>(j,i) = 0 ;
188 map_y.at<float>(j,i) = 0 ;
189 }
190 break;
191 case UPSIDE_DOWN:
192 map_x.at<float>(j,i) = static_cast<float>(i) ;
193 map_y.at<float>(j,i) = static_cast<float>(src.rows - j) ;
194 break;
195 case REFLECTION_X:
196 map_x.at<float>(j,i) = static_cast<float>(src.cols - i) ;
197 map_y.at<float>(j,i) = static_cast<float>(j) ;
198 break;
199 case REFLECTION_BOTH:
200 map_x.at<float>(j,i) = static_cast<float>(src.cols - i) ;
201 map_y.at<float>(j,i) = static_cast<float>(src.rows - j) ;
202 break;
203 } // end of switch
204 }
205 }
206 }
207
PERF_TEST(Transform,getPerspectiveTransform)208 PERF_TEST(Transform, getPerspectiveTransform)
209 {
210 unsigned int size = 8;
211 Mat source(1, size/2, CV_32FC2);
212 Mat destination(1, size/2, CV_32FC2);
213 Mat transformCoefficient;
214
215 declare.in(source, destination, WARMUP_RNG);
216
217 TEST_CYCLE()
218 {
219 transformCoefficient = getPerspectiveTransform(source, destination);
220 }
221
222 SANITY_CHECK(transformCoefficient, 1e-5);
223 }
224