Home | History | Annotate | Download | only in test
      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-2008, Intel Corporation, all rights reserved.
     14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
     15 // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
     16 // Third party copyrights are property of their respective owners.
     17 //
     18 // Redistribution and use in source and binary forms, with or without modification,
     19 // are permitted provided that the following conditions are met:
     20 //
     21 //   * Redistribution's of source code must retain the above copyright notice,
     22 //     this list of conditions and the following disclaimer.
     23 //
     24 //   * Redistribution's in binary form must reproduce the above copyright notice,
     25 //     this list of conditions and the following disclaimer in the documentation
     26 //     and/or other materials provided with the distribution.
     27 //
     28 //   * The name of the copyright holders may not be used to endorse or promote products
     29 //     derived from this software without specific prior written permission.
     30 //
     31 // This software is provided by the copyright holders and contributors "as is" and
     32 // any express or implied warranties, including, but not limited to, the implied
     33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     34 // In no event shall the Intel Corporation or contributors be liable for any direct,
     35 // indirect, incidental, special, exemplary, or consequential damages
     36 // (including, but not limited to, procurement of substitute goods or services;
     37 // loss of use, data, or profits; or business interruption) however caused
     38 // and on any theory of liability, whether in contract, strict liability,
     39 // or tort (including negligence or otherwise) arising in any way out of
     40 // the use of this software, even if advised of the possibility of such damage.
     41 //
     42 //M*/
     43 
     44 #include "test_precomp.hpp"
     45 
     46 using namespace cv;
     47 using namespace cv::cuda;
     48 using namespace cv::cudev;
     49 using namespace cvtest;
     50 
     51 TEST(Sum, GpuMat)
     52 {
     53     const Size size = randomSize(100, 400);
     54 
     55     Mat src = randomMat(size, CV_8UC1);
     56 
     57     GpuMat_<uchar> d_src(src);
     58 
     59     GpuMat_<float> dst = sum_(d_src);
     60     float res;
     61     dst.download(_OutputArray(&res, 1));
     62 
     63     Scalar dst_gold = cv::sum(src);
     64 
     65     ASSERT_FLOAT_EQ(static_cast<float>(dst_gold[0]), res);
     66 }
     67 
     68 TEST(Sum, Expr)
     69 {
     70     const Size size = randomSize(100, 400);
     71 
     72     Mat src1 = randomMat(size, CV_32FC1, 0, 1);
     73     Mat src2 = randomMat(size, CV_32FC1, 0, 1);
     74 
     75     GpuMat_<float> d_src1(src1), d_src2(src2);
     76 
     77     GpuMat_<float> dst = sum_(abs_(d_src1 - d_src2));
     78     float res;
     79     dst.download(_OutputArray(&res, 1));
     80 
     81     Scalar dst_gold = cv::norm(src1, src2, NORM_L1);
     82 
     83     ASSERT_FLOAT_EQ(static_cast<float>(dst_gold[0]), res);
     84 }
     85 
     86 TEST(MinVal, GpuMat)
     87 {
     88     const Size size = randomSize(100, 400);
     89 
     90     Mat src = randomMat(size, CV_8UC1);
     91 
     92     GpuMat_<uchar> d_src(src);
     93 
     94     GpuMat_<float> dst = minVal_(d_src);
     95     float res;
     96     dst.download(_OutputArray(&res, 1));
     97 
     98     double res_gold;
     99     cv::minMaxLoc(src, &res_gold, 0);
    100 
    101     ASSERT_FLOAT_EQ(static_cast<float>(res_gold), res);
    102 }
    103 
    104 TEST(MaxVal, Expr)
    105 {
    106     const Size size = randomSize(100, 400);
    107 
    108     Mat src1 = randomMat(size, CV_32SC1);
    109     Mat src2 = randomMat(size, CV_32SC1);
    110 
    111     GpuMat_<int> d_src1(src1), d_src2(src2);
    112 
    113     GpuMat_<float> dst = maxVal_(abs_(d_src1 - d_src2));
    114     float res;
    115     dst.download(_OutputArray(&res, 1));
    116 
    117     double res_gold = cv::norm(src1, src2, NORM_INF);
    118 
    119     ASSERT_FLOAT_EQ(static_cast<float>(res_gold), res);
    120 }
    121 
    122 TEST(MinMaxVal, GpuMat)
    123 {
    124     const Size size = randomSize(100, 400);
    125 
    126     Mat src = randomMat(size, CV_8UC1);
    127 
    128     GpuMat_<uchar> d_src(src);
    129 
    130     GpuMat_<float> dst = minMaxVal_(d_src);
    131     float res[2];
    132     dst.download(Mat(1, 2, CV_32FC1, res));
    133 
    134     double res_gold[2];
    135     cv::minMaxLoc(src, &res_gold[0], &res_gold[1]);
    136 
    137     ASSERT_FLOAT_EQ(static_cast<float>(res_gold[0]), res[0]);
    138     ASSERT_FLOAT_EQ(static_cast<float>(res_gold[1]), res[1]);
    139 }
    140 
    141 TEST(NonZeroCount, Accuracy)
    142 {
    143     const Size size = randomSize(100, 400);
    144 
    145     Mat src = randomMat(size, CV_8UC1, 0, 5);
    146 
    147     GpuMat_<uchar> d_src(src);
    148 
    149     GpuMat_<int> dst1 = countNonZero_(d_src);
    150     GpuMat_<int> dst2 = sum_(cvt_<int>(d_src) != 0);
    151 
    152     EXPECT_MAT_NEAR(dst1, dst2, 0.0);
    153 }
    154 
    155 TEST(ReduceToRow, Sum)
    156 {
    157     const Size size = randomSize(100, 400);
    158 
    159     Mat src = randomMat(size, CV_8UC1);
    160 
    161     GpuMat_<uchar> d_src(src);
    162 
    163     GpuMat_<int> dst = reduceToRow_<Sum<int> >(d_src);
    164 
    165     Mat dst_gold;
    166     cv::reduce(src, dst_gold, 0, REDUCE_SUM, CV_32S);
    167 
    168     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
    169 }
    170 
    171 TEST(ReduceToRow, Avg)
    172 {
    173     const Size size = randomSize(100, 400);
    174 
    175     Mat src = randomMat(size, CV_8UC1);
    176 
    177     GpuMat_<uchar> d_src(src);
    178 
    179     GpuMat_<float> dst = reduceToRow_<Avg<float> >(d_src);
    180 
    181     Mat dst_gold;
    182     cv::reduce(src, dst_gold, 0, REDUCE_AVG, CV_32F);
    183 
    184     EXPECT_MAT_NEAR(dst_gold, dst, 1e-4);
    185 }
    186 
    187 TEST(ReduceToRow, Min)
    188 {
    189     const Size size = randomSize(100, 400);
    190 
    191     Mat src = randomMat(size, CV_8UC1);
    192 
    193     GpuMat_<uchar> d_src(src);
    194 
    195     GpuMat_<uchar> dst = reduceToRow_<Min<uchar> >(d_src);
    196 
    197     Mat dst_gold;
    198     cv::reduce(src, dst_gold, 0, REDUCE_MIN);
    199 
    200     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
    201 }
    202 
    203 TEST(ReduceToRow, Max)
    204 {
    205     const Size size = randomSize(100, 400);
    206 
    207     Mat src = randomMat(size, CV_8UC1);
    208 
    209     GpuMat_<uchar> d_src(src);
    210 
    211     GpuMat_<uchar> dst = reduceToRow_<Max<uchar> >(d_src);
    212 
    213     Mat dst_gold;
    214     cv::reduce(src, dst_gold, 0, REDUCE_MAX);
    215 
    216     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
    217 }
    218 
    219 TEST(ReduceToColumn, Sum)
    220 {
    221     const Size size = randomSize(100, 400);
    222 
    223     Mat src = randomMat(size, CV_8UC1);
    224 
    225     GpuMat_<uchar> d_src(src);
    226 
    227     GpuMat_<int> dst = reduceToColumn_<Sum<int> >(d_src);
    228 
    229     Mat dst_gold;
    230     cv::reduce(src, dst_gold, 1, REDUCE_SUM, CV_32S);
    231     dst_gold.cols = dst_gold.rows;
    232     dst_gold.rows = 1;
    233     dst_gold.step = dst_gold.cols * dst_gold.elemSize();
    234 
    235     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
    236 }
    237 
    238 TEST(ReduceToColumn, Avg)
    239 {
    240     const Size size = randomSize(100, 400);
    241 
    242     Mat src = randomMat(size, CV_8UC1);
    243 
    244     GpuMat_<uchar> d_src(src);
    245 
    246     GpuMat_<float> dst = reduceToColumn_<Avg<float> >(d_src);
    247 
    248     Mat dst_gold;
    249     cv::reduce(src, dst_gold, 1, REDUCE_AVG, CV_32F);
    250     dst_gold.cols = dst_gold.rows;
    251     dst_gold.rows = 1;
    252     dst_gold.step = dst_gold.cols * dst_gold.elemSize();
    253 
    254     EXPECT_MAT_NEAR(dst_gold, dst, 1e-4);
    255 }
    256 
    257 TEST(ReduceToColumn, Min)
    258 {
    259     const Size size = randomSize(100, 400);
    260 
    261     Mat src = randomMat(size, CV_8UC1);
    262 
    263     GpuMat_<uchar> d_src(src);
    264 
    265     GpuMat_<uchar> dst = reduceToColumn_<Min<uchar> >(d_src);
    266 
    267     Mat dst_gold;
    268     cv::reduce(src, dst_gold, 1, REDUCE_MIN);
    269     dst_gold.cols = dst_gold.rows;
    270     dst_gold.rows = 1;
    271     dst_gold.step = dst_gold.cols * dst_gold.elemSize();
    272 
    273     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
    274 }
    275 
    276 TEST(ReduceToColumn, Max)
    277 {
    278     const Size size = randomSize(100, 400);
    279 
    280     Mat src = randomMat(size, CV_8UC1);
    281 
    282     GpuMat_<uchar> d_src(src);
    283 
    284     GpuMat_<uchar> dst = reduceToColumn_<Max<uchar> >(d_src);
    285 
    286     Mat dst_gold;
    287     cv::reduce(src, dst_gold, 1, REDUCE_MAX);
    288     dst_gold.cols = dst_gold.rows;
    289     dst_gold.rows = 1;
    290     dst_gold.step = dst_gold.cols * dst_gold.elemSize();
    291 
    292     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
    293 }
    294 
    295 static void calcHistGold(const cv::Mat& src, cv::Mat& hist)
    296 {
    297     hist.create(1, 256, CV_32SC1);
    298     hist.setTo(cv::Scalar::all(0));
    299 
    300     int* hist_row = hist.ptr<int>();
    301     for (int y = 0; y < src.rows; ++y)
    302     {
    303         const uchar* src_row = src.ptr(y);
    304 
    305         for (int x = 0; x < src.cols; ++x)
    306             ++hist_row[src_row[x]];
    307     }
    308 }
    309 
    310 TEST(Histogram, GpuMat)
    311 {
    312     const Size size = randomSize(100, 400);
    313 
    314     Mat src = randomMat(size, CV_8UC1);
    315 
    316     GpuMat_<uchar> d_src(src);
    317 
    318     GpuMat_<int> dst = histogram_<256>(d_src);
    319 
    320     Mat dst_gold;
    321     calcHistGold(src, dst_gold);
    322 
    323     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
    324 }
    325