test_split_merge.cpp 10.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*M///////////////////////////////////////////////////////////////////////////////////////
//
//  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
//  By downloading, copying, installing or using the software you agree to this license.
//  If you do not agree to this license, do not download, install,
//  copy or use the software.
//
//
//                           License Agreement
//                For Open Source Computer Vision Library
//
// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
Y
yao 已提交
15
// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
// Third party copyrights are property of their respective owners.
//
// @Authors
//    Jia Haipeng, jiahaipeng95@gmail.com
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
//   * Redistribution's of source code must retain the above copyright notice,
//     this list of conditions and the following disclaimer.
//
//   * Redistribution's in binary form must reproduce the above copyright notice,
//     this list of conditions and the following disclaimer in the documentation
//     and/or other oclMaterials provided with the distribution.
//
//   * The name of the copyright holders may not be used to endorse or promote products
//     derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/

#include "precomp.hpp"

#ifdef HAVE_OPENCL

using namespace cvtest;
using namespace testing;
using namespace std;

PARAM_TEST_CASE(MergeTestBase, MatType, int)
{
    int type;
    int channels;

    //src mat
    cv::Mat mat1;
    cv::Mat mat2;
    cv::Mat mat3;
    cv::Mat mat4;

    //dst mat
    cv::Mat dst;

    // set up roi
    int roicols;
    int roirows;
    int src1x;
    int src1y;
    int src2x;
    int src2y;
    int src3x;
    int src3y;
    int src4x;
    int src4y;
    int dstx;
    int dsty;

    //src mat with roi
    cv::Mat mat1_roi;
    cv::Mat mat2_roi;
    cv::Mat mat3_roi;
    cv::Mat mat4_roi;

    //dst mat with roi
    cv::Mat dst_roi;
Y
yao 已提交
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    //ocl dst mat for testing
    cv::ocl::oclMat gdst_whole;

    //ocl mat with roi
    cv::ocl::oclMat gmat1;
    cv::ocl::oclMat gmat2;
    cv::ocl::oclMat gmat3;
    cv::ocl::oclMat gmat4;
    cv::ocl::oclMat gdst;

    virtual void SetUp()
    {
        type = GET_PARAM(0);
        channels = GET_PARAM(1);

        cv::RNG &rng = TS::ptr()->get_rng();
        cv::Size size(MWIDTH, MHEIGHT);

        mat1 = randomMat(rng, size, CV_MAKETYPE(type, 1), 5, 16, false);
        mat2 = randomMat(rng, size, CV_MAKETYPE(type, 1), 5, 16, false);
        mat3 = randomMat(rng, size, CV_MAKETYPE(type, 1), 5, 16, false);
        mat4 = randomMat(rng, size, CV_MAKETYPE(type, 1), 5, 16, false);
        dst  = randomMat(rng, size, CV_MAKETYPE(type, channels), 5, 16, false);

    }

    void random_roi()
119
    {
120 121
#ifdef RANDOMROI
        //randomize ROI
122
        cv::RNG &rng = TS::ptr()->get_rng();
123 124 125 126 127 128 129
        roicols = rng.uniform(1, mat1.cols);
        roirows = rng.uniform(1, mat1.rows);
        src1x   = rng.uniform(0, mat1.cols - roicols);
        src1y   = rng.uniform(0, mat1.rows - roirows);
        src2x   = rng.uniform(0, mat2.cols - roicols);
        src2y   = rng.uniform(0, mat2.rows - roirows);
        src3x   = rng.uniform(0, mat3.cols - roicols);
130 131
        src3y   = rng.uniform(0, mat3.rows - roirows);
        src4x   = rng.uniform(0, mat4.cols - roicols);
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
        src4y   = rng.uniform(0, mat4.rows - roirows);
        dstx    = rng.uniform(0, dst.cols  - roicols);
        dsty    = rng.uniform(0, dst.rows  - roirows);
#else
        roicols = mat1.cols;
        roirows = mat1.rows;
        src1x   = 0;
        src1y   = 0;
        src2x   = 0;
        src2y   = 0;
        src3x   = 0;
        src3y   = 0;
        src4x   = 0;
        src4y   = 0;
        dstx    = 0;
        dsty    = 0;
#endif


        mat1_roi = mat1(Rect(src1x, src1y, roicols, roirows));
        mat2_roi = mat2(Rect(src2x, src2y, roicols, roirows));
        mat3_roi = mat3(Rect(src3x, src3y, roicols, roirows));
        mat4_roi = mat4(Rect(src4x, src4y, roicols, roirows));


        dst_roi = dst(Rect(dstx, dsty, roicols, roirows));

        gdst_whole = dst;
        gdst = gdst_whole(Rect(dstx, dsty, roicols, roirows));

        gmat1 = mat1_roi;
        gmat2 = mat2_roi;
        gmat3 = mat3_roi;
        gmat4 = mat4_roi;
    }

};

struct Merge : MergeTestBase {};

TEST_P(Merge, Accuracy)
{
    for(int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        std::vector<cv::Mat> dev_src;
        dev_src.push_back(mat1_roi);
N
niko 已提交
180 181 182 183 184 185 186 187 188

        if(channels >= 2)
            dev_src.push_back(mat2_roi);

        if(channels >= 3)
            dev_src.push_back(mat3_roi);

        if(channels >= 4)
            dev_src.push_back(mat4_roi);
189 190 191

        std::vector<cv::ocl::oclMat> dev_gsrc;
        dev_gsrc.push_back(gmat1);
N
niko 已提交
192 193

        if(channels >= 2)
194
            dev_gsrc.push_back(gmat2);
N
niko 已提交
195 196

        if(channels >= 3)
197
            dev_gsrc.push_back(gmat3);
N
niko 已提交
198 199

        if(channels >= 4)
200
            dev_gsrc.push_back(gmat4);
201 202 203 204

        cv::merge(dev_src, dst_roi);
        cv::ocl::merge(dev_gsrc, gdst);

Y
yao 已提交
205
        EXPECT_MAT_NEAR(dst, Mat(gdst_whole), 0.0);
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    }
}



PARAM_TEST_CASE(SplitTestBase, MatType, int)
{
    int type;
    int channels;

    //src mat
    cv::Mat mat;

    //dstmat
    cv::Mat dst1;
    cv::Mat dst2;
    cv::Mat dst3;
    cv::Mat dst4;

    // set up roi
    int roicols;
    int roirows;
    int srcx;
    int srcy;
    int dst1x;
    int dst1y;
    int dst2x;
    int dst2y;
    int dst3x;
    int dst3y;
    int dst4x;
    int dst4y;

    //src mat with roi
    cv::Mat mat_roi;

    //dst mat with roi
    cv::Mat dst1_roi;
    cv::Mat dst2_roi;
    cv::Mat dst3_roi;
    cv::Mat dst4_roi;
Y
yao 已提交
247

248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    //ocl dst mat for testing
    cv::ocl::oclMat gdst1_whole;
    cv::ocl::oclMat gdst2_whole;
    cv::ocl::oclMat gdst3_whole;
    cv::ocl::oclMat gdst4_whole;

    //ocl mat with roi
    cv::ocl::oclMat gmat;
    cv::ocl::oclMat gdst1;
    cv::ocl::oclMat gdst2;
    cv::ocl::oclMat gdst3;
    cv::ocl::oclMat gdst4;

    virtual void SetUp()
    {
        type = GET_PARAM(0);
        channels = GET_PARAM(1);

        cv::RNG &rng = TS::ptr()->get_rng();
        cv::Size size(MWIDTH, MHEIGHT);

        mat  = randomMat(rng, size, CV_MAKETYPE(type, channels), 5, 16, false);
        dst1 = randomMat(rng, size, CV_MAKETYPE(type, 1), 5, 16, false);
        dst2 = randomMat(rng, size, CV_MAKETYPE(type, 1), 5, 16, false);
        dst3 = randomMat(rng, size, CV_MAKETYPE(type, 1), 5, 16, false);
        dst4 = randomMat(rng, size, CV_MAKETYPE(type, 1), 5, 16, false);

    }

    void random_roi()
278
    {
279 280
#ifdef RANDOMROI
        //randomize ROI
281
        cv::RNG &rng = TS::ptr()->get_rng();
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
        roicols = rng.uniform(1, mat.cols);
        roirows = rng.uniform(1, mat.rows);
        srcx    = rng.uniform(0, mat.cols - roicols);
        srcy    = rng.uniform(0, mat.rows - roirows);
        dst1x   = rng.uniform(0, dst1.cols  - roicols);
        dst1y   = rng.uniform(0, dst1.rows  - roirows);
        dst2x   = rng.uniform(0, dst2.cols  - roicols);
        dst2y   = rng.uniform(0, dst2.rows  - roirows);
        dst3x   = rng.uniform(0, dst3.cols  - roicols);
        dst3y   = rng.uniform(0, dst3.rows  - roirows);
        dst4x   = rng.uniform(0, dst4.cols  - roicols);
        dst4y   = rng.uniform(0, dst4.rows  - roirows);
#else
        roicols = mat.cols;
        roirows = mat.rows;
        srcx    = 0;
        srcy    = 0;
        dst1x   = 0;
        dst1y   = 0;
        dst2x   = 0;
        dst2y   = 0;
        dst3x   = 0;
        dst3y   = 0;
        dst4x   = 0;
        dst4y   = 0;
#endif

        mat_roi = mat(Rect(srcx, srcy, roicols, roirows));

        dst1_roi = dst1(Rect(dst1x, dst1y, roicols, roirows));
        dst2_roi = dst2(Rect(dst2x, dst2y, roicols, roirows));
        dst3_roi = dst3(Rect(dst3x, dst3y, roicols, roirows));
        dst4_roi = dst4(Rect(dst4x, dst4y, roicols, roirows));

        gdst1_whole = dst1;
        gdst1 = gdst1_whole(Rect(dst1x, dst1y, roicols, roirows));

        gdst2_whole = dst2;
        gdst2 = gdst2_whole(Rect(dst2x, dst2y, roicols, roirows));

        gdst3_whole = dst3;
        gdst3 = gdst3_whole(Rect(dst3x, dst3y, roicols, roirows));

        gdst4_whole = dst4;
        gdst4 = gdst4_whole(Rect(dst4x, dst4y, roicols, roirows));

        gmat = mat_roi;
    }

};

struct Split : SplitTestBase {};

TEST_P(Split, Accuracy)
{
    for(int j = 0; j < LOOP_TIMES; j++)
    {
        random_roi();

        cv::Mat         dev_dst[4]  = {dst1_roi, dst2_roi, dst3_roi, dst4_roi};
        cv::ocl::oclMat dev_gdst[4] = {gdst1, gdst2, gdst3, gdst4};

        cv::split(mat_roi, dev_dst);
        cv::ocl::split(gmat, dev_gdst);

N
niko 已提交
347
        if(channels >= 1)
Y
yao 已提交
348
            EXPECT_MAT_NEAR(dst1, Mat(gdst1_whole), 0.0);
N
niko 已提交
349 350

        if(channels >= 2)
Y
yao 已提交
351
            EXPECT_MAT_NEAR(dst2, Mat(gdst2_whole), 0.0);
N
niko 已提交
352 353

        if(channels >= 3)
Y
yao 已提交
354
            EXPECT_MAT_NEAR(dst3, Mat(gdst3_whole), 0.0);
N
niko 已提交
355 356

        if(channels >= 4)
Y
yao 已提交
357
            EXPECT_MAT_NEAR(dst4, Mat(gdst4_whole), 0.0);
358 359 360 361 362
    }
}


INSTANTIATE_TEST_CASE_P(SplitMerge, Merge, Combine(
363
                            Values(CV_8U, CV_32S, CV_32F), Values(1, 3, 4)));
364 365 366


INSTANTIATE_TEST_CASE_P(SplitMerge, Split , Combine(
367 368
                            Values(CV_8U, CV_32S, CV_32F), Values(1, 3, 4)));

369 370

#endif // HAVE_OPENCL