Path: blob/master/modules/imgproc/test/ocl/test_histogram.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// Copyright (C) 2014, Itseez, Inc., all rights reserved.16// Third party copyrights are property of their respective owners.17//18// @Authors19// Niko Li, [email protected]20// Jia Haipeng, [email protected]21// Shengen Yan, [email protected]22// Jiang Liyuan, [email protected]23// Rock Li, [email protected]24// Wu Zailong, [email protected]25// Xu Pang, [email protected]26// Sen Liu, [email protected]27//28// Redistribution and use in source and binary forms, with or without modification,29// are permitted provided that the following conditions are met:30//31// * Redistribution's of source code must retain the above copyright notice,32// this list of conditions and the following disclaimer.33//34// * Redistribution's in binary form must reproduce the above copyright notice,35// this list of conditions and the following disclaimer in the documentation36// and/or other materials provided with the distribution.37//38// * The name of the copyright holders may not be used to endorse or promote products39// derived from this software without specific prior written permission.40//41// This software is provided by the copyright holders and contributors "as is" and42// any express or implied warranties, including, but not limited to, the implied43// warranties of merchantability and fitness for a particular purpose are disclaimed.44// In no event shall the Intel Corporation or contributors be liable for any direct,45// indirect, incidental, special, exemplary, or consequential damages46// (including, but not limited to, procurement of substitute goods or services;47// loss of use, data, or profits; or business interruption) however caused48// and on any theory of liability, whether in contract, strict liability,49// or tort (including negligence or otherwise) arising in any way out of50// the use of this software, even if advised of the possibility of such damage.51//52//M*/5354#include "../test_precomp.hpp"55#include "opencv2/ts/ocl_test.hpp"5657#ifdef HAVE_OPENCL5859namespace opencv_test {60namespace ocl {6162///////////////////////////////////////////////////////////////////////////////6364PARAM_TEST_CASE(CalcBackProject, MatDepth, int, bool)65{66int depth, N;67bool useRoi;6869std::vector<float> ranges;70std::vector<int> channels;71double scale;7273std::vector<Mat> images;74std::vector<Mat> images_roi;75std::vector<UMat> uimages;76std::vector<UMat> uimages_roi;7778TEST_DECLARE_INPUT_PARAMETER(hist);79TEST_DECLARE_OUTPUT_PARAMETER(dst);8081virtual void SetUp()82{83depth = GET_PARAM(0);84N = GET_PARAM(1);85useRoi = GET_PARAM(2);8687ASSERT_GE(2, N);8889images.resize(N);90images_roi.resize(N);91uimages.resize(N);92uimages_roi.resize(N);93}9495void random_roi()96{97Size roiSize = randomSize(1, MAX_VALUE);9899int totalChannels = 0;100101ranges.clear();102channels.clear();103104for (int i = 0; i < N; ++i)105{106Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);107int cn = randomInt(1, 5);108randomSubMat(images[i], images_roi[i], roiSize, srcBorder, CV_MAKE_TYPE(depth, cn), 0, 125);109110ranges.push_back(10);111ranges.push_back(100);112113channels.push_back(randomInt(0, cn) + totalChannels);114totalChannels += cn;115}116117Mat tmpHist;118{119std::vector<int> hist_size(N);120for (int i = 0 ; i < N; ++i)121hist_size[i] = randomInt(10, 50);122123cv::calcHist(images_roi, channels, noArray(), tmpHist, hist_size, ranges);124ASSERT_EQ(CV_32FC1, tmpHist.type());125}126127Border histBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);128randomSubMat(hist, hist_roi, tmpHist.size(), histBorder, tmpHist.type(), 0, MAX_VALUE);129tmpHist.copyTo(hist_roi);130131Border dstBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);132randomSubMat(dst, dst_roi, roiSize, dstBorder, CV_MAKE_TYPE(depth, 1), 5, 16);133134for (int i = 0; i < N; ++i)135{136images[i].copyTo(uimages[i]);137138Size _wholeSize;139Point ofs;140images_roi[i].locateROI(_wholeSize, ofs);141142uimages_roi[i] = uimages[i](Rect(ofs.x, ofs.y, images_roi[i].cols, images_roi[i].rows));143}144145UMAT_UPLOAD_INPUT_PARAMETER(hist);146UMAT_UPLOAD_OUTPUT_PARAMETER(dst);147148scale = randomDouble(0.1, 1);149}150151void test_by_pict()152{153Mat frame1 = readImage("optflow/RubberWhale1.png", IMREAD_GRAYSCALE);154155UMat usrc;156frame1.copyTo(usrc);157int histSize = randomInt(3, 29);158float hue_range[] = { 0, 180 };159const float* ranges1 = { hue_range };160Mat hist1;161162//compute histogram163calcHist(&frame1, 1, 0, Mat(), hist1, 1, &histSize, &ranges1, true, false);164normalize(hist1, hist1, 0, 255, NORM_MINMAX, -1, Mat());165166Mat dst1;167UMat udst1, src, uhist1;168hist1.copyTo(uhist1);169std::vector<UMat> uims;170uims.push_back(usrc);171std::vector<float> urngs;172urngs.push_back(0);173urngs.push_back(180);174std::vector<int> chs;175chs.push_back(0);176177OCL_OFF(calcBackProject(&frame1, 1, 0, hist1, dst1, &ranges1, 1, true));178OCL_ON(calcBackProject(uims, chs, uhist1, udst1, urngs, 1.0));179180if (cv::ocl::useOpenCL() && cv::ocl::Device::getDefault().isAMD())181{182Size dstSize = dst1.size();183int nDiffs = (int)(0.03f*dstSize.height*dstSize.width);184185//check if the dst mats are the same except 3% difference186EXPECT_MAT_N_DIFF(dst1, udst1, nDiffs);187}188else189{190EXPECT_MAT_NEAR(dst1, udst1, 0.0);191}192}193};194195//////////////////////////////// CalcBackProject //////////////////////////////////////////////196197OCL_TEST_P(CalcBackProject, Mat)198{199for (int j = 0; j < test_loop_times; j++)200{201random_roi();202203OCL_OFF(cv::calcBackProject(images_roi, channels, hist_roi, dst_roi, ranges, scale));204OCL_ON(cv::calcBackProject(uimages_roi, channels, uhist_roi, udst_roi, ranges, scale));205206Size dstSize = dst_roi.size();207int nDiffs = std::max((int)(0.07f*dstSize.area()), 1);208209//check if the dst mats are the same except 7% difference210EXPECT_MAT_N_DIFF(dst_roi, udst_roi, nDiffs);211}212}213214OCL_TEST_P(CalcBackProject, Mat_RealImage)215{216//check on given image217test_by_pict();218}219220//////////////////////////////// CalcHist //////////////////////////////////////////////221222PARAM_TEST_CASE(CalcHist, bool)223{224bool useRoi;225226TEST_DECLARE_INPUT_PARAMETER(src);227TEST_DECLARE_OUTPUT_PARAMETER(hist);228229virtual void SetUp()230{231useRoi = GET_PARAM(0);232}233234void random_roi()235{236Size roiSize = randomSize(1, MAX_VALUE);237238Border srcBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);239randomSubMat(src, src_roi, roiSize, srcBorder, CV_8UC1, 0, 256);240241Border histBorder = randomBorder(0, useRoi ? MAX_VALUE : 0);242randomSubMat(hist, hist_roi, Size(1, 256), histBorder, CV_32SC1, 0, MAX_VALUE);243244UMAT_UPLOAD_INPUT_PARAMETER(src);245UMAT_UPLOAD_OUTPUT_PARAMETER(hist);246}247};248249OCL_TEST_P(CalcHist, Mat)250{251const std::vector<int> channels(1, 0);252std::vector<float> ranges(2);253std::vector<int> histSize(1, 256);254ranges[0] = 0;255ranges[1] = 256;256257for (int j = 0; j < test_loop_times; j++)258{259random_roi();260261OCL_OFF(cv::calcHist(std::vector<Mat>(1, src_roi), channels, noArray(), hist_roi, histSize, ranges, false));262OCL_ON(cv::calcHist(std::vector<UMat>(1, usrc_roi), channels, noArray(), uhist_roi, histSize, ranges, false));263264OCL_EXPECT_MATS_NEAR(hist, 0.0);265}266}267268/////////////////////////////////////////////////////////////////////////////////////269270OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CalcBackProject, Combine(Values((MatDepth)CV_8U), Values(1, 2), Bool()));271OCL_INSTANTIATE_TEST_CASE_P(Imgproc, CalcHist, Values(true, false));272273} } // namespace opencv_test::ocl274275#endif // HAVE_OPENCL276277278