Path: blob/master/modules/imgproc/test/ocl/test_warp.cpp
16344 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// Niko Li, [email protected]19// Jia Haipeng, [email protected]20// Shengen Yan, [email protected]21// Jiang Liyuan, [email protected]22// Rock Li, [email protected]23// Wu Zailong, [email protected]24// Xu Pang, [email protected]25// Sen Liu, [email protected]26//27// Redistribution and use in source and binary forms, with or without modification,28// are permitted provided that the following conditions are met:29//30// * Redistribution's of source code must retain the above copyright notice,31// this list of conditions and the following disclaimer.32//33// * Redistribution's in binary form must reproduce the above copyright notice,34// this list of conditions and the following disclaimer in the documentation35// and/or other materials provided with the distribution.36//37// * The name of the copyright holders may not be used to endorse or promote products38// derived from this software without specific prior written permission.39//40// This software is provided by the copyright holders and contributors "as is" and41// any express or implied warranties, including, but not limited to, the implied42// warranties of merchantability and fitness for a particular purpose are disclaimed.43// In no event shall the Intel Corporation or contributors be liable for any direct,44// indirect, incidental, special, exemplary, or consequential damages45// (including, but not limited to, procurement of substitute goods or services;46// loss of use, data, or profits; or business interruption) however caused47// and on any theory of liability, whether in contract, strict liability,48// or tort (including negligence or otherwise) arising in any way out of49// the use of this software, even if advised of the possibility of such damage.50//51//M*/5253#include "../test_precomp.hpp"54#include "opencv2/ts/ocl_test.hpp"5556#ifdef HAVE_OPENCL5758namespace opencv_test {59namespace ocl {6061enum62{63noType = -164};6566/////////////////////////////////////////////////////////////////////////////////////////////////67// warpAffine & warpPerspective6869PARAM_TEST_CASE(WarpTestBase, MatType, Interpolation, bool, bool)70{71int type, interpolation;72Size dsize;73bool useRoi, mapInverse;74int depth;7576TEST_DECLARE_INPUT_PARAMETER(src);77TEST_DECLARE_OUTPUT_PARAMETER(dst);7879virtual void SetUp()80{81type = GET_PARAM(0);82interpolation = GET_PARAM(1);83mapInverse = GET_PARAM(2);84useRoi = GET_PARAM(3);85depth = CV_MAT_DEPTH(type);8687if (mapInverse)88interpolation |= WARP_INVERSE_MAP;89}9091void random_roi()92{93dsize = randomSize(1, MAX_VALUE);9495Size roiSize = randomSize(1, MAX_VALUE);96Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);97randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);9899Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);100randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE);101102UMAT_UPLOAD_INPUT_PARAMETER(src);103UMAT_UPLOAD_OUTPUT_PARAMETER(dst);104}105106void Near(double threshold = 0.0)107{108if (depth < CV_32F)109EXPECT_MAT_N_DIFF(dst_roi, udst_roi, cvRound(dst_roi.total()*threshold));110else111OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);112}113};114115PARAM_TEST_CASE(WarpTest_cols4_Base, MatType, Interpolation, bool, bool)116{117int type, interpolation;118Size dsize;119bool useRoi, mapInverse;120int depth;121122TEST_DECLARE_INPUT_PARAMETER(src);123TEST_DECLARE_OUTPUT_PARAMETER(dst);124125virtual void SetUp()126{127type = GET_PARAM(0);128interpolation = GET_PARAM(1);129mapInverse = GET_PARAM(2);130useRoi = GET_PARAM(3);131depth = CV_MAT_DEPTH(type);132133if (mapInverse)134interpolation |= WARP_INVERSE_MAP;135}136137void random_roi()138{139dsize = randomSize(1, MAX_VALUE);140dsize.width = ((dsize.width >> 2) + 1) * 4;141142Size roiSize = randomSize(1, MAX_VALUE);143Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);144randomSubMat(src, src_roi, roiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);145146Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);147randomSubMat(dst, dst_roi, dsize, dstBorder, type, -MAX_VALUE, MAX_VALUE);148149UMAT_UPLOAD_INPUT_PARAMETER(src);150UMAT_UPLOAD_OUTPUT_PARAMETER(dst);151}152153void Near(double threshold = 0.0)154{155if (depth < CV_32F)156EXPECT_MAT_N_DIFF(dst_roi, udst_roi, cvRound(dst_roi.total()*threshold));157else158OCL_EXPECT_MATS_NEAR_RELATIVE(dst, threshold);159}160};161162/////warpAffine163164typedef WarpTestBase WarpAffine;165166/////warpAffine167168typedef WarpTestBase WarpAffine;169170OCL_TEST_P(WarpAffine, Mat)171{172for (int j = 0; j < test_loop_times; j++)173{174double eps = depth < CV_32F ? 0.04 : 0.06;175random_roi();176177Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f),178rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));179180OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation));181OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation));182183Near(eps);184}185}186187typedef WarpTest_cols4_Base WarpAffine_cols4;188189OCL_TEST_P(WarpAffine_cols4, Mat)190{191for (int j = 0; j < test_loop_times; j++)192{193double eps = depth < CV_32F ? 0.04 : 0.06;194random_roi();195196Mat M = getRotationMatrix2D(Point2f(src_roi.cols / 2.0f, src_roi.rows / 2.0f),197rng.uniform(-180.f, 180.f), rng.uniform(0.4f, 2.0f));198199OCL_OFF(cv::warpAffine(src_roi, dst_roi, M, dsize, interpolation));200OCL_ON(cv::warpAffine(usrc_roi, udst_roi, M, dsize, interpolation));201202Near(eps);203}204}205206//// warpPerspective207208typedef WarpTestBase WarpPerspective;209210OCL_TEST_P(WarpPerspective, Mat)211{212for (int j = 0; j < test_loop_times; j++)213{214double eps = depth < CV_32F ? 0.03 : 0.06;215random_roi();216217float cols = static_cast<float>(src_roi.cols), rows = static_cast<float>(src_roi.rows);218float cols2 = cols / 2.0f, rows2 = rows / 2.0f;219Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };220Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)),221Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)),222Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)),223Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) };224Mat M = getPerspectiveTransform(sp, dp);225226OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation));227OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation));228229Near(eps);230}231}232233typedef WarpTest_cols4_Base WarpPerspective_cols4;234235OCL_TEST_P(WarpPerspective_cols4, Mat)236{237for (int j = 0; j < test_loop_times; j++)238{239double eps = depth < CV_32F ? 0.03 : 0.06;240random_roi();241242float cols = static_cast<float>(src_roi.cols), rows = static_cast<float>(src_roi.rows);243float cols2 = cols / 2.0f, rows2 = rows / 2.0f;244Point2f sp[] = { Point2f(0.0f, 0.0f), Point2f(cols, 0.0f), Point2f(0.0f, rows), Point2f(cols, rows) };245Point2f dp[] = { Point2f(rng.uniform(0.0f, cols2), rng.uniform(0.0f, rows2)),246Point2f(rng.uniform(cols2, cols), rng.uniform(0.0f, rows2)),247Point2f(rng.uniform(0.0f, cols2), rng.uniform(rows2, rows)),248Point2f(rng.uniform(cols2, cols), rng.uniform(rows2, rows)) };249Mat M = getPerspectiveTransform(sp, dp);250251OCL_OFF(cv::warpPerspective(src_roi, dst_roi, M, dsize, interpolation));252OCL_ON(cv::warpPerspective(usrc_roi, udst_roi, M, dsize, interpolation));253254Near(eps);255}256}257258/////////////////////////////////////////////////////////////////////////////////////////////////259//// resize260261PARAM_TEST_CASE(Resize, MatType, double, double, Interpolation, bool, int)262{263int type, interpolation;264int widthMultiple;265double fx, fy;266bool useRoi;267268TEST_DECLARE_INPUT_PARAMETER(src);269TEST_DECLARE_OUTPUT_PARAMETER(dst);270271virtual void SetUp()272{273type = GET_PARAM(0);274fx = GET_PARAM(1);275fy = GET_PARAM(2);276interpolation = GET_PARAM(3);277useRoi = GET_PARAM(4);278widthMultiple = GET_PARAM(5);279}280281void random_roi()282{283CV_Assert(fx > 0 && fy > 0);284285Size srcRoiSize = randomSize(10, MAX_VALUE), dstRoiSize;286// Make sure the width is a multiple of the requested value, and no more287srcRoiSize.width += widthMultiple - 1 - (srcRoiSize.width - 1) % widthMultiple;288dstRoiSize.width = cvRound(srcRoiSize.width * fx);289dstRoiSize.height = cvRound(srcRoiSize.height * fy);290291if (dstRoiSize.empty())292{293random_roi();294return;295}296297Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);298randomSubMat(src, src_roi, srcRoiSize, srcBorder, type, -MAX_VALUE, MAX_VALUE);299300Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);301randomSubMat(dst, dst_roi, dstRoiSize, dstBorder, type, -MAX_VALUE, MAX_VALUE);302303UMAT_UPLOAD_INPUT_PARAMETER(src);304UMAT_UPLOAD_OUTPUT_PARAMETER(dst);305}306};307308#if defined(__aarch64__) || defined(__arm__)309const int integerEps = 3;310#else311const int integerEps = 1;312#endif313OCL_TEST_P(Resize, Mat)314{315for (int j = 0; j < test_loop_times; j++)316{317int depth = CV_MAT_DEPTH(type);318double eps = depth <= CV_32S ? integerEps : 5e-2;319320random_roi();321322OCL_OFF(cv::resize(src_roi, dst_roi, Size(), fx, fy, interpolation));323OCL_ON(cv::resize(usrc_roi, udst_roi, Size(), fx, fy, interpolation));324325OCL_EXPECT_MAT_N_DIFF(dst, eps);326}327}328329/////////////////////////////////////////////////////////////////////////////////////////////////330// remap331332PARAM_TEST_CASE(Remap, MatDepth, Channels, std::pair<MatType, MatType>, BorderType, bool)333{334int srcType, map1Type, map2Type;335int borderType;336bool useRoi;337338Scalar val;339340TEST_DECLARE_INPUT_PARAMETER(src);341TEST_DECLARE_INPUT_PARAMETER(map1);342TEST_DECLARE_INPUT_PARAMETER(map2);343TEST_DECLARE_OUTPUT_PARAMETER(dst);344345virtual void SetUp()346{347srcType = CV_MAKE_TYPE(GET_PARAM(0), GET_PARAM(1));348map1Type = GET_PARAM(2).first;349map2Type = GET_PARAM(2).second;350borderType = GET_PARAM(3);351useRoi = GET_PARAM(4);352}353354void random_roi()355{356val = randomScalar(-MAX_VALUE, MAX_VALUE);357Size srcROISize = randomSize(1, MAX_VALUE);358Size dstROISize = randomSize(1, MAX_VALUE);359360Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);361randomSubMat(src, src_roi, srcROISize, srcBorder, srcType, 5, 256);362363Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);364randomSubMat(dst, dst_roi, dstROISize, dstBorder, srcType, -MAX_VALUE, MAX_VALUE);365366int mapMaxValue = MAX_VALUE << 2;367Border map1Border = randomBorder(0, useRoi ? MAX_VALUE : 0);368randomSubMat(map1, map1_roi, dstROISize, map1Border, map1Type, -mapMaxValue, mapMaxValue);369370Border map2Border = randomBorder(0, useRoi ? MAX_VALUE + 1 : 0);371if (map2Type != noType)372{373int mapMinValue = -mapMaxValue;374if (map2Type == CV_16UC1 || map2Type == CV_16SC1)375mapMinValue = 0, mapMaxValue = INTER_TAB_SIZE2;376randomSubMat(map2, map2_roi, dstROISize, map2Border, map2Type, mapMinValue, mapMaxValue);377}378379UMAT_UPLOAD_INPUT_PARAMETER(src);380UMAT_UPLOAD_INPUT_PARAMETER(map1);381UMAT_UPLOAD_OUTPUT_PARAMETER(dst);382if (noType != map2Type)383UMAT_UPLOAD_INPUT_PARAMETER(map2);384}385};386387typedef Remap Remap_INTER_NEAREST;388389OCL_TEST_P(Remap_INTER_NEAREST, Mat)390{391for (int j = 0; j < test_loop_times; j++)392{393random_roi();394395OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_NEAREST, borderType, val));396OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_NEAREST, borderType, val));397398OCL_EXPECT_MAT_N_DIFF(dst, 1.0);399}400}401402typedef Remap Remap_INTER_LINEAR;403404OCL_TEST_P(Remap_INTER_LINEAR, Mat)405{406for (int j = 0; j < test_loop_times; j++)407{408random_roi();409410double eps = 2.0;411#ifdef __ANDROID__412// TODO investigate accuracy413if (cv::ocl::Device::getDefault().isNVidia())414eps = 8.0;415#elif defined(__arm__)416eps = 8.0;417#endif418419OCL_OFF(cv::remap(src_roi, dst_roi, map1_roi, map2_roi, INTER_LINEAR, borderType, val));420OCL_ON(cv::remap(usrc_roi, udst_roi, umap1_roi, umap2_roi, INTER_LINEAR, borderType, val));421422OCL_EXPECT_MAT_N_DIFF(dst, eps);423}424}425426/////////////////////////////////////////////////////////////////////////////////////427428OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine, Combine(429Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),430Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),431Bool(),432Bool()));433434OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpAffine_cols4, Combine(435Values((MatType)CV_8UC1),436Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),437Bool(),438Bool()));439440OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective, Combine(441Values(CV_8UC1, CV_8UC3, CV_8UC4, CV_32FC1, CV_32FC3, CV_32FC4),442Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),443Bool(),444Bool()));445446OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, WarpPerspective_cols4, Combine(447Values((MatType)CV_8UC1),448Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR, (Interpolation)INTER_CUBIC),449Bool(),450Bool()));451452OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Resize, Combine(453Values(CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, CV_32FC4),454Values(0.5, 1.5, 2.0, 0.2),455Values(0.5, 1.5, 2.0, 0.2),456Values((Interpolation)INTER_NEAREST, (Interpolation)INTER_LINEAR),457Bool(),458Values(1, 16)));459460OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpLinearExact, Resize, Combine(461Values(CV_8UC1, CV_8UC4, CV_16UC2),462Values(0.5, 1.5, 2.0, 0.2),463Values(0.5, 1.5, 2.0, 0.2),464Values((Interpolation)INTER_LINEAR_EXACT),465Bool(),466Values(1, 16)));467468OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarpResizeArea, Resize, Combine(469Values((MatType)CV_8UC1, CV_8UC4, CV_32FC1, CV_32FC4),470Values(0.7, 0.4, 0.5),471Values(0.3, 0.6, 0.5),472Values((Interpolation)INTER_AREA),473Bool(),474Values(1, 16)));475476OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_LINEAR, Combine(477Values(CV_8U, CV_16U, CV_32F),478Values(1, 3, 4),479Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),480std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),481std::pair<MatType, MatType>((MatType)CV_32FC2, noType)),482Values((BorderType)BORDER_CONSTANT,483(BorderType)BORDER_REPLICATE,484(BorderType)BORDER_WRAP,485(BorderType)BORDER_REFLECT,486(BorderType)BORDER_REFLECT_101),487Bool()));488489OCL_INSTANTIATE_TEST_CASE_P(ImgprocWarp, Remap_INTER_NEAREST, Combine(490Values(CV_8U, CV_16U, CV_32F),491Values(1, 3, 4),492Values(std::pair<MatType, MatType>((MatType)CV_32FC1, (MatType)CV_32FC1),493std::pair<MatType, MatType>((MatType)CV_32FC2, noType),494std::pair<MatType, MatType>((MatType)CV_16SC2, (MatType)CV_16UC1),495std::pair<MatType, MatType>((MatType)CV_16SC2, noType)),496Values((BorderType)BORDER_CONSTANT,497(BorderType)BORDER_REPLICATE,498(BorderType)BORDER_WRAP,499(BorderType)BORDER_REFLECT,500(BorderType)BORDER_REFLECT_101),501Bool()));502503} } // namespace opencv_test::ocl504505#endif // HAVE_OPENCL506507508