Home | History | Annotate | Download | only in ocl
      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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
     14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
     15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
     16 // Third party copyrights are property of their respective owners.
     17 //
     18 // @Authors
     19 //    Jia Haipeng, jiahaipeng95 (at) gmail.com
     20 //
     21 // Redistribution and use in source and binary forms, with or without modification,
     22 // are permitted provided that the following conditions are met:
     23 //
     24 //   * Redistribution's of source code must retain the above copyright notice,
     25 //     this list of conditions and the following disclaimer.
     26 //
     27 //   * Redistribution's in binary form must reproduce the above copyright notice,
     28 //     this list of conditions and the following disclaimer in the documentation
     29 //     and/or other materials provided with the distribution.
     30 //
     31 //   * The name of the copyright holders may not be used to endorse or promote products
     32 //     derived from this software without specific prior written permission.
     33 //
     34 // This software is provided by the copyright holders and contributors "as is" and
     35 // any express or implied warranties, including, but not limited to, the implied
     36 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     37 // In no event shall the Intel Corporation or contributors be liable for any direct,
     38 // indirect, incidental, special, exemplary, or consequential damages
     39 // (including, but not limited to, procurement of substitute goods or services;
     40 // loss of use, data, or profits; or business interruption) however caused
     41 // and on any theory of liability, whether in contract, strict liability,
     42 // or tort (including negligence or otherwise) arising in any way out of
     43 // the use of this software, even if advised of the possibility of such damage.
     44 //
     45 //M*/
     46 
     47 #include "../test_precomp.hpp"
     48 #include "opencv2/ts/ocl_test.hpp"
     49 
     50 #ifdef HAVE_OPENCL
     51 
     52 namespace cvtest {
     53 namespace ocl {
     54 
     55 //////////////////////////////////////// Merge ///////////////////////////////////////////////
     56 
     57 PARAM_TEST_CASE(Merge, MatDepth, int, bool)
     58 {
     59     int depth, nsrc;
     60     bool use_roi;
     61 
     62     TEST_DECLARE_INPUT_PARAMETER(src1);
     63     TEST_DECLARE_INPUT_PARAMETER(src2);
     64     TEST_DECLARE_INPUT_PARAMETER(src3);
     65     TEST_DECLARE_INPUT_PARAMETER(src4);
     66     TEST_DECLARE_OUTPUT_PARAMETER(dst);
     67 
     68     std::vector<Mat> src_roi;
     69     std::vector<UMat> usrc_roi;
     70 
     71     virtual void SetUp()
     72     {
     73         depth = GET_PARAM(0);
     74         nsrc = GET_PARAM(1);
     75         use_roi = GET_PARAM(2);
     76 
     77         CV_Assert(nsrc >= 1 && nsrc <= 4);
     78     }
     79 
     80     int type()
     81     {
     82         return CV_MAKE_TYPE(depth, randomInt(1, 3));
     83     }
     84 
     85     void generateTestData()
     86     {
     87         Size roiSize = randomSize(1, MAX_VALUE);
     88 
     89         {
     90             Border src1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
     91             randomSubMat(src1, src1_roi, roiSize, src1Border, type(), 2, 11);
     92 
     93             Border src2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
     94             randomSubMat(src2, src2_roi, roiSize, src2Border, type(), -1540, 1740);
     95 
     96             Border src3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
     97             randomSubMat(src3, src3_roi, roiSize, src3Border, type(), -1540, 1740);
     98 
     99             Border src4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    100             randomSubMat(src4, src4_roi, roiSize, src4Border, type(), -1540, 1740);
    101         }
    102 
    103         UMAT_UPLOAD_INPUT_PARAMETER(src1);
    104         UMAT_UPLOAD_INPUT_PARAMETER(src2);
    105         UMAT_UPLOAD_INPUT_PARAMETER(src3);
    106         UMAT_UPLOAD_INPUT_PARAMETER(src4);
    107 
    108         src_roi.clear(); usrc_roi.clear(); // for test_loop_times > 1
    109         src_roi.push_back(src1_roi), usrc_roi.push_back(usrc1_roi);
    110         if (nsrc >= 2)
    111             src_roi.push_back(src2_roi), usrc_roi.push_back(usrc2_roi);
    112         if (nsrc >= 3)
    113             src_roi.push_back(src3_roi), usrc_roi.push_back(usrc3_roi);
    114         if (nsrc >= 4)
    115             src_roi.push_back(src4_roi), usrc_roi.push_back(usrc4_roi);
    116 
    117         int dcn = 0;
    118         for (int i = 0; i < nsrc; ++i)
    119             dcn += src_roi[i].channels();
    120 
    121         Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
    122         randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, dcn), 5, 16);
    123 
    124         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
    125     }
    126 
    127     void Near(double threshold = 0.)
    128     {
    129         OCL_EXPECT_MATS_NEAR(dst, threshold);
    130     }
    131 };
    132 
    133 OCL_TEST_P(Merge, Accuracy)
    134 {
    135     for(int j = 0; j < test_loop_times; j++)
    136     {
    137         generateTestData();
    138 
    139         OCL_OFF(cv::merge(src_roi, dst_roi));
    140         OCL_ON(cv::merge(usrc_roi, udst_roi));
    141 
    142         Near();
    143     }
    144 }
    145 
    146 //////////////////////////////////////// Split ///////////////////////////////////////////////
    147 
    148 PARAM_TEST_CASE(Split, MatType, Channels, bool)
    149 {
    150     int depth, cn;
    151     bool use_roi;
    152 
    153     TEST_DECLARE_INPUT_PARAMETER(src);
    154     TEST_DECLARE_OUTPUT_PARAMETER(dst1);
    155     TEST_DECLARE_OUTPUT_PARAMETER(dst2);
    156     TEST_DECLARE_OUTPUT_PARAMETER(dst3);
    157     TEST_DECLARE_OUTPUT_PARAMETER(dst4);
    158 
    159     std::vector<Mat> dst_roi, dst;
    160     std::vector<UMat> udst_roi, udst;
    161 
    162     virtual void SetUp()
    163     {
    164         depth = GET_PARAM(0);
    165         cn = GET_PARAM(1);
    166         use_roi = GET_PARAM(2);
    167 
    168         CV_Assert(cn >= 1 && cn <= 4);
    169     }
    170 
    171     void generateTestData()
    172     {
    173         Size roiSize = randomSize(1, MAX_VALUE);
    174         Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
    175         randomSubMat(src, src_roi, roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 5, 16);
    176 
    177         {
    178             Border dst1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    179             randomSubMat(dst1, dst1_roi, roiSize, dst1Border, depth, 2, 11);
    180 
    181             Border dst2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    182             randomSubMat(dst2, dst2_roi, roiSize, dst2Border, depth, -1540, 1740);
    183 
    184             Border dst3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    185             randomSubMat(dst3, dst3_roi, roiSize, dst3Border, depth, -1540, 1740);
    186 
    187             Border dst4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    188             randomSubMat(dst4, dst4_roi, roiSize, dst4Border, depth, -1540, 1740);
    189         }
    190 
    191         UMAT_UPLOAD_INPUT_PARAMETER(src);
    192         UMAT_UPLOAD_OUTPUT_PARAMETER(dst1);
    193         UMAT_UPLOAD_OUTPUT_PARAMETER(dst2);
    194         UMAT_UPLOAD_OUTPUT_PARAMETER(dst3);
    195         UMAT_UPLOAD_OUTPUT_PARAMETER(dst4);
    196 
    197         dst_roi.push_back(dst1_roi), udst_roi.push_back(udst1_roi),
    198                 dst.push_back(dst1), udst.push_back(udst1);
    199         if (cn >= 2)
    200             dst_roi.push_back(dst2_roi), udst_roi.push_back(udst2_roi),
    201                     dst.push_back(dst2), udst.push_back(udst2);
    202         if (cn >= 3)
    203             dst_roi.push_back(dst3_roi), udst_roi.push_back(udst3_roi),
    204                     dst.push_back(dst3), udst.push_back(udst3);
    205         if (cn >= 4)
    206             dst_roi.push_back(dst4_roi), udst_roi.push_back(udst4_roi),
    207                     dst.push_back(dst4), udst.push_back(udst4);
    208     }
    209 };
    210 
    211 OCL_TEST_P(Split, Accuracy)
    212 {
    213     for (int j = 0; j < test_loop_times; j++)
    214     {
    215         generateTestData();
    216 
    217         OCL_OFF(cv::split(src_roi, dst_roi));
    218         OCL_ON(cv::split(usrc_roi, udst_roi));
    219 
    220         for (int i = 0; i < cn; ++i)
    221         {
    222             EXPECT_MAT_NEAR(dst[i], udst[i], 0.0);
    223             EXPECT_MAT_NEAR(dst_roi[i], udst_roi[i], 0.0);
    224         }
    225     }
    226 }
    227 
    228 //////////////////////////////////////// MixChannels ///////////////////////////////////////////////
    229 
    230 PARAM_TEST_CASE(MixChannels, MatType, bool)
    231 {
    232     int depth;
    233     bool use_roi;
    234 
    235     TEST_DECLARE_INPUT_PARAMETER(src1);
    236     TEST_DECLARE_INPUT_PARAMETER(src2);
    237     TEST_DECLARE_INPUT_PARAMETER(src3);
    238     TEST_DECLARE_INPUT_PARAMETER(src4);
    239     TEST_DECLARE_OUTPUT_PARAMETER(dst1);
    240     TEST_DECLARE_OUTPUT_PARAMETER(dst2);
    241     TEST_DECLARE_OUTPUT_PARAMETER(dst3);
    242     TEST_DECLARE_OUTPUT_PARAMETER(dst4);
    243 
    244     std::vector<Mat> src_roi, dst_roi, dst;
    245     std::vector<UMat> usrc_roi, udst_roi, udst;
    246     std::vector<int> fromTo;
    247 
    248     virtual void SetUp()
    249     {
    250         depth = GET_PARAM(0);
    251         use_roi = GET_PARAM(1);
    252     }
    253 
    254     // generate number of channels and create type
    255     int type()
    256     {
    257         int cn = randomInt(1, 5);
    258         return CV_MAKE_TYPE(depth, cn);
    259     }
    260 
    261     void generateTestData()
    262     {
    263         src_roi.clear();
    264         dst_roi.clear();
    265         dst.clear();
    266         usrc_roi.clear();
    267         udst_roi.clear();
    268         udst.clear();
    269         fromTo.clear();
    270 
    271         Size roiSize = randomSize(1, MAX_VALUE);
    272 
    273         {
    274             Border src1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    275             randomSubMat(src1, src1_roi, roiSize, src1Border, type(), 2, 11);
    276 
    277             Border src2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    278             randomSubMat(src2, src2_roi, roiSize, src2Border, type(), -1540, 1740);
    279 
    280             Border src3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    281             randomSubMat(src3, src3_roi, roiSize, src3Border, type(), -1540, 1740);
    282 
    283             Border src4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    284             randomSubMat(src4, src4_roi, roiSize, src4Border, type(), -1540, 1740);
    285         }
    286 
    287         {
    288             Border dst1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    289             randomSubMat(dst1, dst1_roi, roiSize, dst1Border, type(), 2, 11);
    290 
    291             Border dst2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    292             randomSubMat(dst2, dst2_roi, roiSize, dst2Border, type(), -1540, 1740);
    293 
    294             Border dst3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    295             randomSubMat(dst3, dst3_roi, roiSize, dst3Border, type(), -1540, 1740);
    296 
    297             Border dst4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);
    298             randomSubMat(dst4, dst4_roi, roiSize, dst4Border, type(), -1540, 1740);
    299         }
    300 
    301         UMAT_UPLOAD_INPUT_PARAMETER(src1);
    302         UMAT_UPLOAD_INPUT_PARAMETER(src2);
    303         UMAT_UPLOAD_INPUT_PARAMETER(src3);
    304         UMAT_UPLOAD_INPUT_PARAMETER(src4);
    305 
    306         UMAT_UPLOAD_OUTPUT_PARAMETER(dst1);
    307         UMAT_UPLOAD_OUTPUT_PARAMETER(dst2);
    308         UMAT_UPLOAD_OUTPUT_PARAMETER(dst3);
    309         UMAT_UPLOAD_OUTPUT_PARAMETER(dst4);
    310 
    311         int nsrc = randomInt(1, 5), ndst = randomInt(1, 5);
    312 
    313         src_roi.push_back(src1_roi), usrc_roi.push_back(usrc1_roi);
    314         if (nsrc >= 2)
    315             src_roi.push_back(src2_roi), usrc_roi.push_back(usrc2_roi);
    316         if (nsrc >= 3)
    317             src_roi.push_back(src3_roi), usrc_roi.push_back(usrc3_roi);
    318         if (nsrc >= 4)
    319             src_roi.push_back(src4_roi), usrc_roi.push_back(usrc4_roi);
    320 
    321         dst_roi.push_back(dst1_roi), udst_roi.push_back(udst1_roi),
    322                 dst.push_back(dst1), udst.push_back(udst1);
    323         if (ndst >= 2)
    324             dst_roi.push_back(dst2_roi), udst_roi.push_back(udst2_roi),
    325                     dst.push_back(dst2), udst.push_back(udst2);
    326         if (ndst >= 3)
    327             dst_roi.push_back(dst3_roi), udst_roi.push_back(udst3_roi),
    328                     dst.push_back(dst3), udst.push_back(udst3);
    329         if (ndst >= 4)
    330             dst_roi.push_back(dst4_roi), udst_roi.push_back(udst4_roi),
    331                     dst.push_back(dst4), udst.push_back(udst4);
    332 
    333         int scntotal = 0, dcntotal = 0;
    334         for (int i = 0; i < nsrc; ++i)
    335             scntotal += src_roi[i].channels();
    336         for (int i = 0; i < ndst; ++i)
    337             dcntotal += dst_roi[i].channels();
    338 
    339         int npairs = randomInt(1, std::min(scntotal, dcntotal) + 1);
    340         fromTo.resize(npairs << 1);
    341 
    342         for (int i = 0; i < npairs; ++i)
    343         {
    344             fromTo[i<<1] = randomInt(0, scntotal);
    345             fromTo[(i<<1)+1] = randomInt(0, dcntotal);
    346         }
    347     }
    348 };
    349 
    350 OCL_TEST_P(MixChannels, Accuracy)
    351 {
    352     for (int j = 0; j < test_loop_times + 10; j++)
    353     {
    354         generateTestData();
    355 
    356         OCL_OFF(cv::mixChannels(src_roi, dst_roi, fromTo));
    357         OCL_ON(cv::mixChannels(usrc_roi, udst_roi, fromTo));
    358 
    359         for (size_t i = 0, size = dst_roi.size(); i < size; ++i)
    360         {
    361             EXPECT_MAT_NEAR(dst[i], udst[i], 0.0);
    362             EXPECT_MAT_NEAR(dst_roi[i], udst_roi[i], 0.0);
    363         }
    364     }
    365 }
    366 
    367 //////////////////////////////////////// InsertChannel ///////////////////////////////////////////////
    368 
    369 PARAM_TEST_CASE(InsertChannel, MatDepth, Channels, bool)
    370 {
    371     int depth, cn, coi;
    372     bool use_roi;
    373 
    374     TEST_DECLARE_INPUT_PARAMETER(src);
    375     TEST_DECLARE_OUTPUT_PARAMETER(dst);
    376 
    377     virtual void SetUp()
    378     {
    379         depth = GET_PARAM(0);
    380         cn = GET_PARAM(1);
    381         use_roi = GET_PARAM(2);
    382     }
    383 
    384     void generateTestData()
    385     {
    386         Size roiSize = randomSize(1, MAX_VALUE);
    387         coi = randomInt(0, cn);
    388 
    389         Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
    390         randomSubMat(src, src_roi, roiSize, srcBorder, depth, 2, 11);
    391 
    392         Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
    393         randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, cn), 5, 16);
    394 
    395         UMAT_UPLOAD_INPUT_PARAMETER(src);
    396         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
    397     }
    398 };
    399 
    400 OCL_TEST_P(InsertChannel, Accuracy)
    401 {
    402     for(int j = 0; j < test_loop_times; j++)
    403     {
    404         generateTestData();
    405 
    406         OCL_OFF(cv::insertChannel(src_roi, dst_roi, coi));
    407         OCL_ON(cv::insertChannel(usrc_roi, udst_roi, coi));
    408 
    409         OCL_EXPECT_MATS_NEAR(dst, 0);
    410     }
    411 }
    412 
    413 //////////////////////////////////////// ExtractChannel ///////////////////////////////////////////////
    414 
    415 PARAM_TEST_CASE(ExtractChannel, MatDepth, Channels, bool)
    416 {
    417     int depth, cn, coi;
    418     bool use_roi;
    419 
    420     TEST_DECLARE_INPUT_PARAMETER(src);
    421     TEST_DECLARE_OUTPUT_PARAMETER(dst);
    422 
    423     virtual void SetUp()
    424     {
    425         depth = GET_PARAM(0);
    426         cn = GET_PARAM(1);
    427         use_roi = GET_PARAM(2);
    428     }
    429 
    430     void generateTestData()
    431     {
    432         Size roiSize = randomSize(1, MAX_VALUE);
    433         coi = randomInt(0, cn);
    434 
    435         Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
    436         randomSubMat(src, src_roi, roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 2, 11);
    437 
    438         Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);
    439         randomSubMat(dst, dst_roi, roiSize, dstBorder, depth, 5, 16);
    440 
    441         UMAT_UPLOAD_INPUT_PARAMETER(src);
    442         UMAT_UPLOAD_OUTPUT_PARAMETER(dst);
    443     }
    444 };
    445 
    446 OCL_TEST_P(ExtractChannel, Accuracy)
    447 {
    448     for(int j = 0; j < test_loop_times; j++)
    449     {
    450         generateTestData();
    451 
    452         OCL_OFF(cv::extractChannel(src_roi, dst_roi, coi));
    453         OCL_ON(cv::extractChannel(usrc_roi, udst_roi, coi));
    454 
    455         OCL_EXPECT_MATS_NEAR(dst, 0);
    456     }
    457 }
    458 
    459 //////////////////////////////////////// Instantiation ///////////////////////////////////////////////
    460 
    461 OCL_INSTANTIATE_TEST_CASE_P(Channels, Merge, Combine(OCL_ALL_DEPTHS, Values(1, 2, 3, 4), Bool()));
    462 OCL_INSTANTIATE_TEST_CASE_P(Channels, Split, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));
    463 OCL_INSTANTIATE_TEST_CASE_P(Channels, MixChannels, Combine(OCL_ALL_DEPTHS, Bool()));
    464 OCL_INSTANTIATE_TEST_CASE_P(Channels, InsertChannel, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));
    465 OCL_INSTANTIATE_TEST_CASE_P(Channels, ExtractChannel, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));
    466 
    467 } } // namespace cvtest::ocl
    468 
    469 #endif // HAVE_OPENCL
    470