Path: blob/master/modules/core/test/ocl/test_channels.cpp
16339 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.13// Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.14// Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.15// Third party copyrights are property of their respective owners.16//17// @Authors18// Jia Haipeng, [email protected]19//20// Redistribution and use in source and binary forms, with or without modification,21// are permitted provided that the following conditions are met:22//23// * Redistribution's of source code must retain the above copyright notice,24// this list of conditions and the following disclaimer.25//26// * Redistribution's in binary form must reproduce the above copyright notice,27// this list of conditions and the following disclaimer in the documentation28// and/or other materials provided with the distribution.29//30// * The name of the copyright holders may not be used to endorse or promote products31// derived from this software without specific prior written permission.32//33// This software is provided by the copyright holders and contributors "as is" and34// any express or implied warranties, including, but not limited to, the implied35// warranties of merchantability and fitness for a particular purpose are disclaimed.36// In no event shall the Intel Corporation or contributors be liable for any direct,37// indirect, incidental, special, exemplary, or consequential damages38// (including, but not limited to, procurement of substitute goods or services;39// loss of use, data, or profits; or business interruption) however caused40// and on any theory of liability, whether in contract, strict liability,41// or tort (including negligence or otherwise) arising in any way out of42// the use of this software, even if advised of the possibility of such damage.43//44//M*/4546#include "../test_precomp.hpp"47#include "opencv2/ts/ocl_test.hpp"4849#ifdef HAVE_OPENCL5051namespace opencv_test {52namespace ocl {5354//////////////////////////////////////// Merge ///////////////////////////////////////////////5556PARAM_TEST_CASE(Merge, MatDepth, int, bool)57{58int depth, nsrc;59bool use_roi;6061TEST_DECLARE_INPUT_PARAMETER(src1);62TEST_DECLARE_INPUT_PARAMETER(src2);63TEST_DECLARE_INPUT_PARAMETER(src3);64TEST_DECLARE_INPUT_PARAMETER(src4);65TEST_DECLARE_OUTPUT_PARAMETER(dst);6667std::vector<Mat> src_roi;68std::vector<UMat> usrc_roi;6970virtual void SetUp()71{72depth = GET_PARAM(0);73nsrc = GET_PARAM(1);74use_roi = GET_PARAM(2);7576CV_Assert(nsrc >= 1 && nsrc <= 4);77}7879int type()80{81return CV_MAKE_TYPE(depth, randomInt(1, 3));82}8384void generateTestData()85{86Size roiSize = randomSize(1, MAX_VALUE);8788{89Border src1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);90randomSubMat(src1, src1_roi, roiSize, src1Border, type(), 2, 11);9192Border src2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);93randomSubMat(src2, src2_roi, roiSize, src2Border, type(), -1540, 1740);9495Border src3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);96randomSubMat(src3, src3_roi, roiSize, src3Border, type(), -1540, 1740);9798Border src4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);99randomSubMat(src4, src4_roi, roiSize, src4Border, type(), -1540, 1740);100}101102UMAT_UPLOAD_INPUT_PARAMETER(src1);103UMAT_UPLOAD_INPUT_PARAMETER(src2);104UMAT_UPLOAD_INPUT_PARAMETER(src3);105UMAT_UPLOAD_INPUT_PARAMETER(src4);106107src_roi.clear(); usrc_roi.clear(); // for test_loop_times > 1108src_roi.push_back(src1_roi), usrc_roi.push_back(usrc1_roi);109if (nsrc >= 2)110src_roi.push_back(src2_roi), usrc_roi.push_back(usrc2_roi);111if (nsrc >= 3)112src_roi.push_back(src3_roi), usrc_roi.push_back(usrc3_roi);113if (nsrc >= 4)114src_roi.push_back(src4_roi), usrc_roi.push_back(usrc4_roi);115116int dcn = 0;117for (int i = 0; i < nsrc; ++i)118dcn += src_roi[i].channels();119120Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);121randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, dcn), 5, 16);122123UMAT_UPLOAD_OUTPUT_PARAMETER(dst);124}125126void Near(double threshold = 0.)127{128OCL_EXPECT_MATS_NEAR(dst, threshold);129}130};131132OCL_TEST_P(Merge, Accuracy)133{134for(int j = 0; j < test_loop_times; j++)135{136generateTestData();137138OCL_OFF(cv::merge(src_roi, dst_roi));139OCL_ON(cv::merge(usrc_roi, udst_roi));140141Near();142}143}144145//////////////////////////////////////// Split ///////////////////////////////////////////////146147PARAM_TEST_CASE(Split, MatType, Channels, bool)148{149int depth, cn;150bool use_roi;151152TEST_DECLARE_INPUT_PARAMETER(src);153TEST_DECLARE_OUTPUT_PARAMETER(dst1);154TEST_DECLARE_OUTPUT_PARAMETER(dst2);155TEST_DECLARE_OUTPUT_PARAMETER(dst3);156TEST_DECLARE_OUTPUT_PARAMETER(dst4);157158std::vector<Mat> dst_roi, dst;159std::vector<UMat> udst_roi, udst;160161virtual void SetUp()162{163depth = GET_PARAM(0);164cn = GET_PARAM(1);165use_roi = GET_PARAM(2);166167CV_Assert(cn >= 1 && cn <= 4);168}169170void generateTestData()171{172Size roiSize = randomSize(1, MAX_VALUE);173Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);174randomSubMat(src, src_roi, roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 5, 16);175176{177Border dst1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);178randomSubMat(dst1, dst1_roi, roiSize, dst1Border, depth, 2, 11);179180Border dst2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);181randomSubMat(dst2, dst2_roi, roiSize, dst2Border, depth, -1540, 1740);182183Border dst3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);184randomSubMat(dst3, dst3_roi, roiSize, dst3Border, depth, -1540, 1740);185186Border dst4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);187randomSubMat(dst4, dst4_roi, roiSize, dst4Border, depth, -1540, 1740);188}189190UMAT_UPLOAD_INPUT_PARAMETER(src);191UMAT_UPLOAD_OUTPUT_PARAMETER(dst1);192UMAT_UPLOAD_OUTPUT_PARAMETER(dst2);193UMAT_UPLOAD_OUTPUT_PARAMETER(dst3);194UMAT_UPLOAD_OUTPUT_PARAMETER(dst4);195196dst_roi.push_back(dst1_roi), udst_roi.push_back(udst1_roi),197dst.push_back(dst1), udst.push_back(udst1);198if (cn >= 2)199dst_roi.push_back(dst2_roi), udst_roi.push_back(udst2_roi),200dst.push_back(dst2), udst.push_back(udst2);201if (cn >= 3)202dst_roi.push_back(dst3_roi), udst_roi.push_back(udst3_roi),203dst.push_back(dst3), udst.push_back(udst3);204if (cn >= 4)205dst_roi.push_back(dst4_roi), udst_roi.push_back(udst4_roi),206dst.push_back(dst4), udst.push_back(udst4);207}208};209210OCL_TEST_P(Split, Accuracy)211{212for (int j = 0; j < test_loop_times; j++)213{214generateTestData();215216OCL_OFF(cv::split(src_roi, dst_roi));217OCL_ON(cv::split(usrc_roi, udst_roi));218219for (int i = 0; i < cn; ++i)220{221EXPECT_MAT_NEAR(dst[i], udst[i], 0.0);222EXPECT_MAT_NEAR(dst_roi[i], udst_roi[i], 0.0);223}224}225}226227//////////////////////////////////////// MixChannels ///////////////////////////////////////////////228229PARAM_TEST_CASE(MixChannels, MatType, bool)230{231int depth;232bool use_roi;233234TEST_DECLARE_INPUT_PARAMETER(src1);235TEST_DECLARE_INPUT_PARAMETER(src2);236TEST_DECLARE_INPUT_PARAMETER(src3);237TEST_DECLARE_INPUT_PARAMETER(src4);238TEST_DECLARE_OUTPUT_PARAMETER(dst1);239TEST_DECLARE_OUTPUT_PARAMETER(dst2);240TEST_DECLARE_OUTPUT_PARAMETER(dst3);241TEST_DECLARE_OUTPUT_PARAMETER(dst4);242243std::vector<Mat> src_roi, dst_roi, dst;244std::vector<UMat> usrc_roi, udst_roi, udst;245std::vector<int> fromTo;246247virtual void SetUp()248{249depth = GET_PARAM(0);250use_roi = GET_PARAM(1);251}252253// generate number of channels and create type254int type()255{256int cn = randomInt(1, 5);257return CV_MAKE_TYPE(depth, cn);258}259260void generateTestData()261{262src_roi.clear();263dst_roi.clear();264dst.clear();265usrc_roi.clear();266udst_roi.clear();267udst.clear();268fromTo.clear();269270Size roiSize = randomSize(1, MAX_VALUE);271272{273Border src1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);274randomSubMat(src1, src1_roi, roiSize, src1Border, type(), 2, 11);275276Border src2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);277randomSubMat(src2, src2_roi, roiSize, src2Border, type(), -1540, 1740);278279Border src3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);280randomSubMat(src3, src3_roi, roiSize, src3Border, type(), -1540, 1740);281282Border src4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);283randomSubMat(src4, src4_roi, roiSize, src4Border, type(), -1540, 1740);284}285286{287Border dst1Border = randomBorder(0, use_roi ? MAX_VALUE : 0);288randomSubMat(dst1, dst1_roi, roiSize, dst1Border, type(), 2, 11);289290Border dst2Border = randomBorder(0, use_roi ? MAX_VALUE : 0);291randomSubMat(dst2, dst2_roi, roiSize, dst2Border, type(), -1540, 1740);292293Border dst3Border = randomBorder(0, use_roi ? MAX_VALUE : 0);294randomSubMat(dst3, dst3_roi, roiSize, dst3Border, type(), -1540, 1740);295296Border dst4Border = randomBorder(0, use_roi ? MAX_VALUE : 0);297randomSubMat(dst4, dst4_roi, roiSize, dst4Border, type(), -1540, 1740);298}299300UMAT_UPLOAD_INPUT_PARAMETER(src1);301UMAT_UPLOAD_INPUT_PARAMETER(src2);302UMAT_UPLOAD_INPUT_PARAMETER(src3);303UMAT_UPLOAD_INPUT_PARAMETER(src4);304305UMAT_UPLOAD_OUTPUT_PARAMETER(dst1);306UMAT_UPLOAD_OUTPUT_PARAMETER(dst2);307UMAT_UPLOAD_OUTPUT_PARAMETER(dst3);308UMAT_UPLOAD_OUTPUT_PARAMETER(dst4);309310int nsrc = randomInt(1, 5), ndst = randomInt(1, 5);311312src_roi.push_back(src1_roi), usrc_roi.push_back(usrc1_roi);313if (nsrc >= 2)314src_roi.push_back(src2_roi), usrc_roi.push_back(usrc2_roi);315if (nsrc >= 3)316src_roi.push_back(src3_roi), usrc_roi.push_back(usrc3_roi);317if (nsrc >= 4)318src_roi.push_back(src4_roi), usrc_roi.push_back(usrc4_roi);319320dst_roi.push_back(dst1_roi), udst_roi.push_back(udst1_roi),321dst.push_back(dst1), udst.push_back(udst1);322if (ndst >= 2)323dst_roi.push_back(dst2_roi), udst_roi.push_back(udst2_roi),324dst.push_back(dst2), udst.push_back(udst2);325if (ndst >= 3)326dst_roi.push_back(dst3_roi), udst_roi.push_back(udst3_roi),327dst.push_back(dst3), udst.push_back(udst3);328if (ndst >= 4)329dst_roi.push_back(dst4_roi), udst_roi.push_back(udst4_roi),330dst.push_back(dst4), udst.push_back(udst4);331332int scntotal = 0, dcntotal = 0;333for (int i = 0; i < nsrc; ++i)334scntotal += src_roi[i].channels();335for (int i = 0; i < ndst; ++i)336dcntotal += dst_roi[i].channels();337338int npairs = randomInt(1, std::min(scntotal, dcntotal) + 1);339fromTo.resize(npairs << 1);340341for (int i = 0; i < npairs; ++i)342{343fromTo[i<<1] = randomInt(0, scntotal);344fromTo[(i<<1)+1] = randomInt(0, dcntotal);345}346}347};348349OCL_TEST_P(MixChannels, Accuracy)350{351for (int j = 0; j < test_loop_times + 10; j++)352{353generateTestData();354355OCL_OFF(cv::mixChannels(src_roi, dst_roi, fromTo));356OCL_ON(cv::mixChannels(usrc_roi, udst_roi, fromTo));357358for (size_t i = 0, size = dst_roi.size(); i < size; ++i)359{360EXPECT_MAT_NEAR(dst[i], udst[i], 0.0);361EXPECT_MAT_NEAR(dst_roi[i], udst_roi[i], 0.0);362}363}364}365366//////////////////////////////////////// InsertChannel ///////////////////////////////////////////////367368PARAM_TEST_CASE(InsertChannel, MatDepth, Channels, bool)369{370int depth, cn, coi;371bool use_roi;372373TEST_DECLARE_INPUT_PARAMETER(src);374TEST_DECLARE_OUTPUT_PARAMETER(dst);375376virtual void SetUp()377{378depth = GET_PARAM(0);379cn = GET_PARAM(1);380use_roi = GET_PARAM(2);381}382383void generateTestData()384{385Size roiSize = randomSize(1, MAX_VALUE);386coi = randomInt(0, cn);387388Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);389randomSubMat(src, src_roi, roiSize, srcBorder, depth, 2, 11);390391Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);392randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, cn), 5, 16);393394UMAT_UPLOAD_INPUT_PARAMETER(src);395UMAT_UPLOAD_OUTPUT_PARAMETER(dst);396}397};398399OCL_TEST_P(InsertChannel, Accuracy)400{401for(int j = 0; j < test_loop_times; j++)402{403generateTestData();404405OCL_OFF(cv::insertChannel(src_roi, dst_roi, coi));406OCL_ON(cv::insertChannel(usrc_roi, udst_roi, coi));407408OCL_EXPECT_MATS_NEAR(dst, 0);409}410}411412//////////////////////////////////////// ExtractChannel ///////////////////////////////////////////////413414PARAM_TEST_CASE(ExtractChannel, MatDepth, Channels, bool)415{416int depth, cn, coi;417bool use_roi;418419TEST_DECLARE_INPUT_PARAMETER(src);420TEST_DECLARE_OUTPUT_PARAMETER(dst);421422virtual void SetUp()423{424depth = GET_PARAM(0);425cn = GET_PARAM(1);426use_roi = GET_PARAM(2);427}428429void generateTestData()430{431Size roiSize = randomSize(1, MAX_VALUE);432coi = randomInt(0, cn);433434Border srcBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);435randomSubMat(src, src_roi, roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 2, 11);436437Border dstBorder = randomBorder(0, use_roi ? MAX_VALUE : 0);438randomSubMat(dst, dst_roi, roiSize, dstBorder, depth, 5, 16);439440UMAT_UPLOAD_INPUT_PARAMETER(src);441UMAT_UPLOAD_OUTPUT_PARAMETER(dst);442}443};444445OCL_TEST_P(ExtractChannel, Accuracy)446{447for(int j = 0; j < test_loop_times; j++)448{449generateTestData();450451OCL_OFF(cv::extractChannel(src_roi, dst_roi, coi));452OCL_ON(cv::extractChannel(usrc_roi, udst_roi, coi));453454OCL_EXPECT_MATS_NEAR(dst, 0);455}456}457458//////////////////////////////////////// Instantiation ///////////////////////////////////////////////459460OCL_INSTANTIATE_TEST_CASE_P(Channels, Merge, Combine(OCL_ALL_DEPTHS, Values(1, 2, 3, 4), Bool()));461OCL_INSTANTIATE_TEST_CASE_P(Channels, Split, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));462OCL_INSTANTIATE_TEST_CASE_P(Channels, MixChannels, Combine(OCL_ALL_DEPTHS, Bool()));463OCL_INSTANTIATE_TEST_CASE_P(Channels, InsertChannel, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));464OCL_INSTANTIATE_TEST_CASE_P(Channels, ExtractChannel, Combine(OCL_ALL_DEPTHS, OCL_ALL_CHANNELS, Bool()));465466} } // namespace opencv_test::ocl467468#endif // HAVE_OPENCL469470471