1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "test_precomp.hpp"
47 using namespace cvtest;
49 ///////////////////////////////////////////////////////////////////////////////////////////////////////
52 PARAM_TEST_CASE(Integral, cv::gpu::DeviceInfo, cv::Size, UseRoi)
54 cv::gpu::DeviceInfo devInfo;
60 devInfo = GET_PARAM(0);
62 useRoi = GET_PARAM(2);
64 cv::gpu::setDevice(devInfo.deviceID());
68 GPU_TEST_P(Integral, Accuracy)
70 cv::Mat src = randomMat(size, CV_8UC1);
72 cv::gpu::GpuMat dst = createMat(cv::Size(src.cols + 1, src.rows + 1), CV_32SC1, useRoi);
73 cv::gpu::integral(loadMat(src, useRoi), dst);
76 cv::integral(src, dst_gold, CV_32S);
78 EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
81 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Integral, testing::Combine(
86 ///////////////////////////////////////////////////////////////////////////////////////////////////////
89 struct HistEven : testing::TestWithParam<cv::gpu::DeviceInfo>
91 cv::gpu::DeviceInfo devInfo;
97 cv::gpu::setDevice(devInfo.deviceID());
101 GPU_TEST_P(HistEven, Accuracy)
103 cv::Mat img = readImage("stereobm/aloe-L.png");
104 ASSERT_FALSE(img.empty());
107 cv::cvtColor(img, hsv, CV_BGR2HSV);
110 float hranges[] = {0.0f, 180.0f};
112 std::vector<cv::gpu::GpuMat> srcs;
113 cv::gpu::split(loadMat(hsv), srcs);
115 cv::gpu::GpuMat hist;
116 cv::gpu::histEven(srcs[0], hist, hbins, (int)hranges[0], (int)hranges[1]);
119 int histSize[] = {hbins};
120 const float* ranges[] = {hranges};
121 int channels[] = {0};
122 cv::calcHist(&hsv, 1, channels, cv::Mat(), histnd, 1, histSize, ranges);
124 cv::Mat hist_gold = histnd;
125 hist_gold = hist_gold.t();
126 hist_gold.convertTo(hist_gold, CV_32S);
128 EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
131 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, HistEven, ALL_DEVICES);
133 ///////////////////////////////////////////////////////////////////////////////////////////////////////
138 void calcHistGold(const cv::Mat& src, cv::Mat& hist)
140 hist.create(1, 256, CV_32SC1);
141 hist.setTo(cv::Scalar::all(0));
143 int* hist_row = hist.ptr<int>();
144 for (int y = 0; y < src.rows; ++y)
146 const uchar* src_row = src.ptr(y);
148 for (int x = 0; x < src.cols; ++x)
149 ++hist_row[src_row[x]];
154 PARAM_TEST_CASE(CalcHist, cv::gpu::DeviceInfo, cv::Size)
156 cv::gpu::DeviceInfo devInfo;
162 devInfo = GET_PARAM(0);
165 cv::gpu::setDevice(devInfo.deviceID());
169 GPU_TEST_P(CalcHist, Accuracy)
171 cv::Mat src = randomMat(size, CV_8UC1);
173 cv::gpu::GpuMat hist;
174 cv::gpu::calcHist(loadMat(src), hist);
177 calcHistGold(src, hist_gold);
179 EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
182 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CalcHist, testing::Combine(
186 ///////////////////////////////////////////////////////////////////////////////////////////////////////
189 PARAM_TEST_CASE(EqualizeHist, cv::gpu::DeviceInfo, cv::Size)
191 cv::gpu::DeviceInfo devInfo;
196 devInfo = GET_PARAM(0);
199 cv::gpu::setDevice(devInfo.deviceID());
203 GPU_TEST_P(EqualizeHist, Accuracy)
205 cv::Mat src = randomMat(size, CV_8UC1);
208 cv::gpu::equalizeHist(loadMat(src), dst);
211 cv::equalizeHist(src, dst_gold);
213 EXPECT_MAT_NEAR(dst_gold, dst, 3.0);
216 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, EqualizeHist, testing::Combine(
220 ///////////////////////////////////////////////////////////////////////////////////////////////////////
225 IMPLEMENT_PARAM_CLASS(ClipLimit, double)
228 PARAM_TEST_CASE(CLAHE, cv::gpu::DeviceInfo, cv::Size, ClipLimit)
230 cv::gpu::DeviceInfo devInfo;
236 devInfo = GET_PARAM(0);
238 clipLimit = GET_PARAM(2);
240 cv::gpu::setDevice(devInfo.deviceID());
244 GPU_TEST_P(CLAHE, Accuracy)
246 cv::Mat src = randomMat(size, CV_8UC1);
248 cv::Ptr<cv::gpu::CLAHE> clahe = cv::gpu::createCLAHE(clipLimit);
250 clahe->apply(loadMat(src), dst);
252 cv::Ptr<cv::CLAHE> clahe_gold = cv::createCLAHE(clipLimit);
254 clahe_gold->apply(src, dst_gold);
256 ASSERT_MAT_NEAR(dst_gold, dst, 1.0);
259 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CLAHE, testing::Combine(
262 testing::Values(0.0, 40.0)));
264 ////////////////////////////////////////////////////////////////////////
267 PARAM_TEST_CASE(ColumnSum, cv::gpu::DeviceInfo, cv::Size)
269 cv::gpu::DeviceInfo devInfo;
274 devInfo = GET_PARAM(0);
277 cv::gpu::setDevice(devInfo.deviceID());
281 GPU_TEST_P(ColumnSum, Accuracy)
283 cv::Mat src = randomMat(size, CV_32FC1);
285 cv::gpu::GpuMat d_dst;
286 cv::gpu::columnSum(loadMat(src), d_dst);
290 for (int j = 0; j < src.cols; ++j)
292 float gold = src.at<float>(0, j);
293 float res = dst.at<float>(0, j);
294 ASSERT_NEAR(res, gold, 1e-5);
297 for (int i = 1; i < src.rows; ++i)
299 for (int j = 0; j < src.cols; ++j)
301 float gold = src.at<float>(i, j) += src.at<float>(i - 1, j);
302 float res = dst.at<float>(i, j);
303 ASSERT_NEAR(res, gold, 1e-5);
308 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, ColumnSum, testing::Combine(
312 ////////////////////////////////////////////////////////
317 IMPLEMENT_PARAM_CLASS(AppertureSize, int);
318 IMPLEMENT_PARAM_CLASS(L2gradient, bool);
321 PARAM_TEST_CASE(Canny, cv::gpu::DeviceInfo, AppertureSize, L2gradient, UseRoi)
323 cv::gpu::DeviceInfo devInfo;
330 devInfo = GET_PARAM(0);
331 apperture_size = GET_PARAM(1);
332 useL2gradient = GET_PARAM(2);
333 useRoi = GET_PARAM(3);
335 cv::gpu::setDevice(devInfo.deviceID());
339 GPU_TEST_P(Canny, Accuracy)
341 cv::Mat img = readImage("stereobm/aloe-L.png", cv::IMREAD_GRAYSCALE);
342 ASSERT_FALSE(img.empty());
344 double low_thresh = 50.0;
345 double high_thresh = 100.0;
347 if (!supportFeature(devInfo, cv::gpu::SHARED_ATOMICS))
351 cv::gpu::GpuMat edges;
352 cv::gpu::Canny(loadMat(img), edges, low_thresh, high_thresh, apperture_size, useL2gradient);
354 catch (const cv::Exception& e)
356 ASSERT_EQ(CV_StsNotImplemented, e.code);
361 cv::gpu::GpuMat edges;
362 cv::gpu::Canny(loadMat(img, useRoi), edges, low_thresh, high_thresh, apperture_size, useL2gradient);
365 cv::Canny(img, edges_gold, low_thresh, high_thresh, apperture_size, useL2gradient);
367 EXPECT_MAT_SIMILAR(edges_gold, edges, 2e-2);
371 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Canny, testing::Combine(
373 testing::Values(AppertureSize(3), AppertureSize(5)),
374 testing::Values(L2gradient(false), L2gradient(true)),
377 ////////////////////////////////////////////////////////////////////////////////
380 struct MeanShift : testing::TestWithParam<cv::gpu::DeviceInfo>
382 cv::gpu::DeviceInfo devInfo;
391 devInfo = GetParam();
393 cv::gpu::setDevice(devInfo.deviceID());
395 img = readImageType("meanshift/cones.png", CV_8UC4);
396 ASSERT_FALSE(img.empty());
403 GPU_TEST_P(MeanShift, Filtering)
405 cv::Mat img_template;
406 if (supportFeature(devInfo, cv::gpu::FEATURE_SET_COMPUTE_20))
407 img_template = readImage("meanshift/con_result.png");
409 img_template = readImage("meanshift/con_result_CC1X.png");
410 ASSERT_FALSE(img_template.empty());
412 cv::gpu::GpuMat d_dst;
413 cv::gpu::meanShiftFiltering(loadMat(img), d_dst, spatialRad, colorRad);
415 ASSERT_EQ(CV_8UC4, d_dst.type());
420 cv::cvtColor(dst, result, CV_BGRA2BGR);
422 EXPECT_MAT_NEAR(img_template, result, 0.0);
425 GPU_TEST_P(MeanShift, Proc)
428 if (supportFeature(devInfo, cv::gpu::FEATURE_SET_COMPUTE_20))
429 fs.open(std::string(cvtest::TS::ptr()->get_data_path()) + "meanshift/spmap.yaml", cv::FileStorage::READ);
431 fs.open(std::string(cvtest::TS::ptr()->get_data_path()) + "meanshift/spmap_CC1X.yaml", cv::FileStorage::READ);
432 ASSERT_TRUE(fs.isOpened());
434 cv::Mat spmap_template;
435 fs["spmap"] >> spmap_template;
436 ASSERT_FALSE(spmap_template.empty());
438 cv::gpu::GpuMat rmap_filtered;
439 cv::gpu::meanShiftFiltering(loadMat(img), rmap_filtered, spatialRad, colorRad);
441 cv::gpu::GpuMat rmap;
442 cv::gpu::GpuMat spmap;
443 cv::gpu::meanShiftProc(loadMat(img), rmap, spmap, spatialRad, colorRad);
445 ASSERT_EQ(CV_8UC4, rmap.type());
447 EXPECT_MAT_NEAR(rmap_filtered, rmap, 0.0);
448 EXPECT_MAT_NEAR(spmap_template, spmap, 0.0);
451 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MeanShift, ALL_DEVICES);
453 ////////////////////////////////////////////////////////////////////////////////
454 // MeanShiftSegmentation
458 IMPLEMENT_PARAM_CLASS(MinSize, int);
461 PARAM_TEST_CASE(MeanShiftSegmentation, cv::gpu::DeviceInfo, MinSize)
463 cv::gpu::DeviceInfo devInfo;
468 devInfo = GET_PARAM(0);
469 minsize = GET_PARAM(1);
471 cv::gpu::setDevice(devInfo.deviceID());
475 GPU_TEST_P(MeanShiftSegmentation, Regression)
477 cv::Mat img = readImageType("meanshift/cones.png", CV_8UC4);
478 ASSERT_FALSE(img.empty());
480 std::ostringstream path;
481 path << "meanshift/cones_segmented_sp10_sr10_minsize" << minsize;
482 if (supportFeature(devInfo, cv::gpu::FEATURE_SET_COMPUTE_20))
486 cv::Mat dst_gold = readImage(path.str());
487 ASSERT_FALSE(dst_gold.empty());
490 cv::gpu::meanShiftSegmentation(loadMat(img), dst, 10, 10, minsize);
493 cv::cvtColor(dst, dst_rgb, CV_BGRA2BGR);
495 EXPECT_MAT_SIMILAR(dst_gold, dst_rgb, 1e-3);
498 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MeanShiftSegmentation, testing::Combine(
500 testing::Values(MinSize(0), MinSize(4), MinSize(20), MinSize(84), MinSize(340), MinSize(1364))));
502 ////////////////////////////////////////////////////////////////////////////
507 template <typename T>
508 void blendLinearGold(const cv::Mat& img1, const cv::Mat& img2, const cv::Mat& weights1, const cv::Mat& weights2, cv::Mat& result_gold)
510 result_gold.create(img1.size(), img1.type());
512 int cn = img1.channels();
514 for (int y = 0; y < img1.rows; ++y)
516 const float* weights1_row = weights1.ptr<float>(y);
517 const float* weights2_row = weights2.ptr<float>(y);
518 const T* img1_row = img1.ptr<T>(y);
519 const T* img2_row = img2.ptr<T>(y);
520 T* result_gold_row = result_gold.ptr<T>(y);
522 for (int x = 0; x < img1.cols * cn; ++x)
524 float w1 = weights1_row[x / cn];
525 float w2 = weights2_row[x / cn];
526 result_gold_row[x] = static_cast<T>((img1_row[x] * w1 + img2_row[x] * w2) / (w1 + w2 + 1e-5f));
532 PARAM_TEST_CASE(Blend, cv::gpu::DeviceInfo, cv::Size, MatType, UseRoi)
534 cv::gpu::DeviceInfo devInfo;
541 devInfo = GET_PARAM(0);
544 useRoi = GET_PARAM(3);
546 cv::gpu::setDevice(devInfo.deviceID());
550 GPU_TEST_P(Blend, Accuracy)
552 int depth = CV_MAT_DEPTH(type);
554 cv::Mat img1 = randomMat(size, type, 0.0, depth == CV_8U ? 255.0 : 1.0);
555 cv::Mat img2 = randomMat(size, type, 0.0, depth == CV_8U ? 255.0 : 1.0);
556 cv::Mat weights1 = randomMat(size, CV_32F, 0, 1);
557 cv::Mat weights2 = randomMat(size, CV_32F, 0, 1);
559 cv::gpu::GpuMat result;
560 cv::gpu::blendLinear(loadMat(img1, useRoi), loadMat(img2, useRoi), loadMat(weights1, useRoi), loadMat(weights2, useRoi), result);
564 blendLinearGold<uchar>(img1, img2, weights1, weights2, result_gold);
566 blendLinearGold<float>(img1, img2, weights1, weights2, result_gold);
568 EXPECT_MAT_NEAR(result_gold, result, CV_MAT_DEPTH(type) == CV_8U ? 1.0 : 1e-5);
571 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Blend, testing::Combine(
574 testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
577 ////////////////////////////////////////////////////////
582 void convolveDFT(const cv::Mat& A, const cv::Mat& B, cv::Mat& C, bool ccorr = false)
584 // reallocate the output array if needed
585 C.create(std::abs(A.rows - B.rows) + 1, std::abs(A.cols - B.cols) + 1, A.type());
588 // compute the size of DFT transform
589 dftSize.width = cv::getOptimalDFTSize(A.cols + B.cols - 1);
590 dftSize.height = cv::getOptimalDFTSize(A.rows + B.rows - 1);
592 // allocate temporary buffers and initialize them with 0s
593 cv::Mat tempA(dftSize, A.type(), cv::Scalar::all(0));
594 cv::Mat tempB(dftSize, B.type(), cv::Scalar::all(0));
596 // copy A and B to the top-left corners of tempA and tempB, respectively
597 cv::Mat roiA(tempA, cv::Rect(0, 0, A.cols, A.rows));
599 cv::Mat roiB(tempB, cv::Rect(0, 0, B.cols, B.rows));
602 // now transform the padded A & B in-place;
603 // use "nonzeroRows" hint for faster processing
604 cv::dft(tempA, tempA, 0, A.rows);
605 cv::dft(tempB, tempB, 0, B.rows);
607 // multiply the spectrums;
608 // the function handles packed spectrum representations well
609 cv::mulSpectrums(tempA, tempB, tempA, 0, ccorr);
611 // transform the product back from the frequency domain.
612 // Even though all the result rows will be non-zero,
613 // you need only the first C.rows of them, and thus you
614 // pass nonzeroRows == C.rows
615 cv::dft(tempA, tempA, cv::DFT_INVERSE + cv::DFT_SCALE, C.rows);
617 // now copy the result back to C.
618 tempA(cv::Rect(0, 0, C.cols, C.rows)).copyTo(C);
621 IMPLEMENT_PARAM_CLASS(KSize, int);
622 IMPLEMENT_PARAM_CLASS(Ccorr, bool);
625 PARAM_TEST_CASE(Convolve, cv::gpu::DeviceInfo, cv::Size, KSize, Ccorr)
627 cv::gpu::DeviceInfo devInfo;
634 devInfo = GET_PARAM(0);
636 ksize = GET_PARAM(2);
637 ccorr = GET_PARAM(3);
639 cv::gpu::setDevice(devInfo.deviceID());
643 GPU_TEST_P(Convolve, Accuracy)
645 cv::Mat src = randomMat(size, CV_32FC1, 0.0, 100.0);
646 cv::Mat kernel = randomMat(cv::Size(ksize, ksize), CV_32FC1, 0.0, 1.0);
649 cv::gpu::convolve(loadMat(src), loadMat(kernel), dst, ccorr);
652 convolveDFT(src, kernel, dst_gold, ccorr);
654 EXPECT_MAT_NEAR(dst, dst_gold, 1e-1);
657 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Convolve, testing::Combine(
660 testing::Values(KSize(3), KSize(7), KSize(11), KSize(17), KSize(19), KSize(23), KSize(45)),
661 testing::Values(Ccorr(false), Ccorr(true))));
663 ////////////////////////////////////////////////////////////////////////////////
666 CV_ENUM(TemplateMethod, TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED)
670 IMPLEMENT_PARAM_CLASS(TemplateSize, cv::Size);
673 PARAM_TEST_CASE(MatchTemplate8U, cv::gpu::DeviceInfo, cv::Size, TemplateSize, Channels, TemplateMethod)
675 cv::gpu::DeviceInfo devInfo;
683 devInfo = GET_PARAM(0);
685 templ_size = GET_PARAM(2);
687 method = GET_PARAM(4);
689 cv::gpu::setDevice(devInfo.deviceID());
693 GPU_TEST_P(MatchTemplate8U, Accuracy)
695 cv::Mat image = randomMat(size, CV_MAKETYPE(CV_8U, cn));
696 cv::Mat templ = randomMat(templ_size, CV_MAKETYPE(CV_8U, cn));
699 cv::gpu::matchTemplate(loadMat(image), loadMat(templ), dst, method);
702 cv::matchTemplate(image, templ, dst_gold, method);
704 EXPECT_MAT_NEAR(dst_gold, dst, templ_size.area() * 1e-1);
707 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplate8U, testing::Combine(
710 testing::Values(TemplateSize(cv::Size(5, 5)), TemplateSize(cv::Size(16, 16)), TemplateSize(cv::Size(30, 30))),
711 testing::Values(Channels(1), Channels(3), Channels(4)),
712 TemplateMethod::all()));
714 ////////////////////////////////////////////////////////////////////////////////
717 PARAM_TEST_CASE(MatchTemplate32F, cv::gpu::DeviceInfo, cv::Size, TemplateSize, Channels, TemplateMethod)
719 cv::gpu::DeviceInfo devInfo;
729 devInfo = GET_PARAM(0);
731 templ_size = GET_PARAM(2);
733 method = GET_PARAM(4);
735 cv::gpu::setDevice(devInfo.deviceID());
739 GPU_TEST_P(MatchTemplate32F, Regression)
741 cv::Mat image = randomMat(size, CV_MAKETYPE(CV_32F, cn));
742 cv::Mat templ = randomMat(templ_size, CV_MAKETYPE(CV_32F, cn));
745 cv::gpu::matchTemplate(loadMat(image), loadMat(templ), dst, method);
748 cv::matchTemplate(image, templ, dst_gold, method);
750 EXPECT_MAT_NEAR(dst_gold, dst, templ_size.area() * 1e-1);
753 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplate32F, testing::Combine(
756 testing::Values(TemplateSize(cv::Size(5, 5)), TemplateSize(cv::Size(16, 16)), TemplateSize(cv::Size(30, 30))),
757 testing::Values(Channels(1), Channels(3), Channels(4)),
758 testing::Values(TemplateMethod(cv::TM_SQDIFF), TemplateMethod(cv::TM_CCORR))));
760 ////////////////////////////////////////////////////////////////////////////////
761 // MatchTemplateBlackSource
763 PARAM_TEST_CASE(MatchTemplateBlackSource, cv::gpu::DeviceInfo, TemplateMethod)
765 cv::gpu::DeviceInfo devInfo;
770 devInfo = GET_PARAM(0);
771 method = GET_PARAM(1);
773 cv::gpu::setDevice(devInfo.deviceID());
777 GPU_TEST_P(MatchTemplateBlackSource, Accuracy)
779 cv::Mat image = readImage("matchtemplate/black.png");
780 ASSERT_FALSE(image.empty());
782 cv::Mat pattern = readImage("matchtemplate/cat.png");
783 ASSERT_FALSE(pattern.empty());
785 cv::gpu::GpuMat d_dst;
786 cv::gpu::matchTemplate(loadMat(image), loadMat(pattern), d_dst, method);
792 cv::minMaxLoc(dst, NULL, &maxValue, NULL, &maxLoc);
794 cv::Point maxLocGold = cv::Point(284, 12);
796 ASSERT_EQ(maxLocGold, maxLoc);
799 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplateBlackSource, testing::Combine(
801 testing::Values(TemplateMethod(cv::TM_CCOEFF_NORMED), TemplateMethod(cv::TM_CCORR_NORMED))));
803 ////////////////////////////////////////////////////////////////////////////////
804 // MatchTemplate_CCOEF_NORMED
806 PARAM_TEST_CASE(MatchTemplate_CCOEF_NORMED, cv::gpu::DeviceInfo, std::pair<std::string, std::string>)
808 cv::gpu::DeviceInfo devInfo;
809 std::string imageName;
810 std::string patternName;
814 devInfo = GET_PARAM(0);
815 imageName = GET_PARAM(1).first;
816 patternName = GET_PARAM(1).second;
818 cv::gpu::setDevice(devInfo.deviceID());
822 GPU_TEST_P(MatchTemplate_CCOEF_NORMED, Accuracy)
824 cv::Mat image = readImage(imageName);
825 ASSERT_FALSE(image.empty());
827 cv::Mat pattern = readImage(patternName);
828 ASSERT_FALSE(pattern.empty());
830 cv::gpu::GpuMat d_dst;
831 cv::gpu::matchTemplate(loadMat(image), loadMat(pattern), d_dst, CV_TM_CCOEFF_NORMED);
835 cv::Point minLoc, maxLoc;
836 double minVal, maxVal;
837 cv::minMaxLoc(dst, &minVal, &maxVal, &minLoc, &maxLoc);
840 cv::matchTemplate(image, pattern, dstGold, CV_TM_CCOEFF_NORMED);
842 double minValGold, maxValGold;
843 cv::Point minLocGold, maxLocGold;
844 cv::minMaxLoc(dstGold, &minValGold, &maxValGold, &minLocGold, &maxLocGold);
846 ASSERT_EQ(minLocGold, minLoc);
847 ASSERT_EQ(maxLocGold, maxLoc);
848 ASSERT_LE(maxVal, 1.0);
849 ASSERT_GE(minVal, -1.0);
852 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplate_CCOEF_NORMED, testing::Combine(
854 testing::Values(std::make_pair(std::string("matchtemplate/source-0.png"), std::string("matchtemplate/target-0.png")))));
856 ////////////////////////////////////////////////////////////////////////////////
857 // MatchTemplate_CanFindBigTemplate
859 struct MatchTemplate_CanFindBigTemplate : testing::TestWithParam<cv::gpu::DeviceInfo>
861 cv::gpu::DeviceInfo devInfo;
865 devInfo = GetParam();
867 cv::gpu::setDevice(devInfo.deviceID());
871 GPU_TEST_P(MatchTemplate_CanFindBigTemplate, SQDIFF_NORMED)
873 cv::Mat scene = readImage("matchtemplate/scene.png");
874 ASSERT_FALSE(scene.empty());
876 cv::Mat templ = readImage("matchtemplate/template.png");
877 ASSERT_FALSE(templ.empty());
879 cv::gpu::GpuMat d_result;
880 cv::gpu::matchTemplate(loadMat(scene), loadMat(templ), d_result, CV_TM_SQDIFF_NORMED);
882 cv::Mat result(d_result);
886 cv::minMaxLoc(result, &minVal, 0, &minLoc, 0);
888 ASSERT_GE(minVal, 0);
889 ASSERT_LT(minVal, 1e-3);
890 ASSERT_EQ(344, minLoc.x);
891 ASSERT_EQ(0, minLoc.y);
894 GPU_TEST_P(MatchTemplate_CanFindBigTemplate, SQDIFF)
896 cv::Mat scene = readImage("matchtemplate/scene.png");
897 ASSERT_FALSE(scene.empty());
899 cv::Mat templ = readImage("matchtemplate/template.png");
900 ASSERT_FALSE(templ.empty());
902 cv::gpu::GpuMat d_result;
903 cv::gpu::matchTemplate(loadMat(scene), loadMat(templ), d_result, CV_TM_SQDIFF);
905 cv::Mat result(d_result);
909 cv::minMaxLoc(result, &minVal, 0, &minLoc, 0);
911 ASSERT_GE(minVal, 0);
912 ASSERT_EQ(344, minLoc.x);
913 ASSERT_EQ(0, minLoc.y);
916 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplate_CanFindBigTemplate, ALL_DEVICES);
918 ////////////////////////////////////////////////////////////////////////////
921 CV_FLAGS(DftFlags, 0, DFT_INVERSE, DFT_SCALE, DFT_ROWS, DFT_COMPLEX_OUTPUT, DFT_REAL_OUTPUT)
923 PARAM_TEST_CASE(MulSpectrums, cv::gpu::DeviceInfo, cv::Size, DftFlags)
925 cv::gpu::DeviceInfo devInfo;
933 devInfo = GET_PARAM(0);
937 cv::gpu::setDevice(devInfo.deviceID());
939 a = randomMat(size, CV_32FC2);
940 b = randomMat(size, CV_32FC2);
944 GPU_TEST_P(MulSpectrums, Simple)
947 cv::gpu::mulSpectrums(loadMat(a), loadMat(b), c, flag, false);
950 cv::mulSpectrums(a, b, c_gold, flag, false);
952 EXPECT_MAT_NEAR(c_gold, c, 1e-2);
955 GPU_TEST_P(MulSpectrums, Scaled)
957 float scale = 1.f / size.area();
960 cv::gpu::mulAndScaleSpectrums(loadMat(a), loadMat(b), c, flag, scale, false);
963 cv::mulSpectrums(a, b, c_gold, flag, false);
964 c_gold.convertTo(c_gold, c_gold.type(), scale);
966 EXPECT_MAT_NEAR(c_gold, c, 1e-2);
969 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MulSpectrums, testing::Combine(
972 testing::Values(DftFlags(0), DftFlags(cv::DFT_ROWS))));
974 ////////////////////////////////////////////////////////////////////////////
977 struct Dft : testing::TestWithParam<cv::gpu::DeviceInfo>
979 cv::gpu::DeviceInfo devInfo;
983 devInfo = GetParam();
985 cv::gpu::setDevice(devInfo.deviceID());
991 void testC2C(const std::string& hint, int cols, int rows, int flags, bool inplace)
995 cv::Mat a = randomMat(cv::Size(cols, rows), CV_32FC2, 0.0, 10.0);
998 cv::dft(a, b_gold, flags);
1000 cv::gpu::GpuMat d_b;
1001 cv::gpu::GpuMat d_b_data;
1004 d_b_data.create(1, a.size().area(), CV_32FC2);
1005 d_b = cv::gpu::GpuMat(a.rows, a.cols, CV_32FC2, d_b_data.ptr(), a.cols * d_b_data.elemSize());
1007 cv::gpu::dft(loadMat(a), d_b, cv::Size(cols, rows), flags);
1009 EXPECT_TRUE(!inplace || d_b.ptr() == d_b_data.ptr());
1010 ASSERT_EQ(CV_32F, d_b.depth());
1011 ASSERT_EQ(2, d_b.channels());
1012 EXPECT_MAT_NEAR(b_gold, cv::Mat(d_b), rows * cols * 1e-4);
1016 GPU_TEST_P(Dft, C2C)
1018 int cols = randomInt(2, 100);
1019 int rows = randomInt(2, 100);
1021 for (int i = 0; i < 2; ++i)
1023 bool inplace = i != 0;
1025 testC2C("no flags", cols, rows, 0, inplace);
1026 testC2C("no flags 0 1", cols, rows + 1, 0, inplace);
1027 testC2C("no flags 1 0", cols, rows + 1, 0, inplace);
1028 testC2C("no flags 1 1", cols + 1, rows, 0, inplace);
1029 testC2C("DFT_INVERSE", cols, rows, cv::DFT_INVERSE, inplace);
1030 testC2C("DFT_ROWS", cols, rows, cv::DFT_ROWS, inplace);
1031 testC2C("single col", 1, rows, 0, inplace);
1032 testC2C("single row", cols, 1, 0, inplace);
1033 testC2C("single col inversed", 1, rows, cv::DFT_INVERSE, inplace);
1034 testC2C("single row inversed", cols, 1, cv::DFT_INVERSE, inplace);
1035 testC2C("single row DFT_ROWS", cols, 1, cv::DFT_ROWS, inplace);
1036 testC2C("size 1 2", 1, 2, 0, inplace);
1037 testC2C("size 2 1", 2, 1, 0, inplace);
1043 void testR2CThenC2R(const std::string& hint, int cols, int rows, bool inplace)
1047 cv::Mat a = randomMat(cv::Size(cols, rows), CV_32FC1, 0.0, 10.0);
1049 cv::gpu::GpuMat d_b, d_c;
1050 cv::gpu::GpuMat d_b_data, d_c_data;
1055 d_b_data.create(1, (a.rows / 2 + 1) * a.cols, CV_32FC2);
1056 d_b = cv::gpu::GpuMat(a.rows / 2 + 1, a.cols, CV_32FC2, d_b_data.ptr(), a.cols * d_b_data.elemSize());
1060 d_b_data.create(1, a.rows * (a.cols / 2 + 1), CV_32FC2);
1061 d_b = cv::gpu::GpuMat(a.rows, a.cols / 2 + 1, CV_32FC2, d_b_data.ptr(), (a.cols / 2 + 1) * d_b_data.elemSize());
1063 d_c_data.create(1, a.size().area(), CV_32F);
1064 d_c = cv::gpu::GpuMat(a.rows, a.cols, CV_32F, d_c_data.ptr(), a.cols * d_c_data.elemSize());
1067 cv::gpu::dft(loadMat(a), d_b, cv::Size(cols, rows), 0);
1068 cv::gpu::dft(d_b, d_c, cv::Size(cols, rows), cv::DFT_REAL_OUTPUT | cv::DFT_SCALE);
1070 EXPECT_TRUE(!inplace || d_b.ptr() == d_b_data.ptr());
1071 EXPECT_TRUE(!inplace || d_c.ptr() == d_c_data.ptr());
1072 ASSERT_EQ(CV_32F, d_c.depth());
1073 ASSERT_EQ(1, d_c.channels());
1076 EXPECT_MAT_NEAR(a, c, rows * cols * 1e-5);
1080 GPU_TEST_P(Dft, R2CThenC2R)
1082 int cols = randomInt(2, 100);
1083 int rows = randomInt(2, 100);
1085 testR2CThenC2R("sanity", cols, rows, false);
1086 testR2CThenC2R("sanity 0 1", cols, rows + 1, false);
1087 testR2CThenC2R("sanity 1 0", cols + 1, rows, false);
1088 testR2CThenC2R("sanity 1 1", cols + 1, rows + 1, false);
1089 testR2CThenC2R("single col", 1, rows, false);
1090 testR2CThenC2R("single col 1", 1, rows + 1, false);
1091 testR2CThenC2R("single row", cols, 1, false);
1092 testR2CThenC2R("single row 1", cols + 1, 1, false);
1094 testR2CThenC2R("sanity", cols, rows, true);
1095 testR2CThenC2R("sanity 0 1", cols, rows + 1, true);
1096 testR2CThenC2R("sanity 1 0", cols + 1, rows, true);
1097 testR2CThenC2R("sanity 1 1", cols + 1, rows + 1, true);
1098 testR2CThenC2R("single row", cols, 1, true);
1099 testR2CThenC2R("single row 1", cols + 1, 1, true);
1102 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Dft, ALL_DEVICES);
1104 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1109 IMPLEMENT_PARAM_CLASS(BlockSize, int);
1110 IMPLEMENT_PARAM_CLASS(ApertureSize, int);
1113 PARAM_TEST_CASE(CornerHarris, cv::gpu::DeviceInfo, MatType, BorderType, BlockSize, ApertureSize)
1115 cv::gpu::DeviceInfo devInfo;
1121 virtual void SetUp()
1123 devInfo = GET_PARAM(0);
1124 type = GET_PARAM(1);
1125 borderType = GET_PARAM(2);
1126 blockSize = GET_PARAM(3);
1127 apertureSize = GET_PARAM(4);
1129 cv::gpu::setDevice(devInfo.deviceID());
1133 GPU_TEST_P(CornerHarris, Accuracy)
1135 cv::Mat src = readImageType("stereobm/aloe-L.png", type);
1136 ASSERT_FALSE(src.empty());
1138 double k = randomDouble(0.1, 0.9);
1140 cv::gpu::GpuMat dst;
1141 cv::gpu::cornerHarris(loadMat(src), dst, blockSize, apertureSize, k, borderType);
1144 cv::cornerHarris(src, dst_gold, blockSize, apertureSize, k, borderType);
1146 EXPECT_MAT_NEAR(dst_gold, dst, 0.02);
1149 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CornerHarris, testing::Combine(
1151 testing::Values(MatType(CV_8UC1), MatType(CV_32FC1)),
1152 testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_REFLECT)),
1153 testing::Values(BlockSize(3), BlockSize(5), BlockSize(7)),
1154 testing::Values(ApertureSize(0), ApertureSize(3), ApertureSize(5), ApertureSize(7))));
1156 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1159 PARAM_TEST_CASE(CornerMinEigen, cv::gpu::DeviceInfo, MatType, BorderType, BlockSize, ApertureSize)
1161 cv::gpu::DeviceInfo devInfo;
1167 virtual void SetUp()
1169 devInfo = GET_PARAM(0);
1170 type = GET_PARAM(1);
1171 borderType = GET_PARAM(2);
1172 blockSize = GET_PARAM(3);
1173 apertureSize = GET_PARAM(4);
1175 cv::gpu::setDevice(devInfo.deviceID());
1179 GPU_TEST_P(CornerMinEigen, Accuracy)
1181 cv::Mat src = readImageType("stereobm/aloe-L.png", type);
1182 ASSERT_FALSE(src.empty());
1184 cv::gpu::GpuMat dst;
1185 cv::gpu::cornerMinEigenVal(loadMat(src), dst, blockSize, apertureSize, borderType);
1188 cv::cornerMinEigenVal(src, dst_gold, blockSize, apertureSize, borderType);
1190 EXPECT_MAT_NEAR(dst_gold, dst, 0.02);
1193 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CornerMinEigen, testing::Combine(
1195 testing::Values(MatType(CV_8UC1), MatType(CV_32FC1)),
1196 testing::Values(BorderType(cv::BORDER_REFLECT101), BorderType(cv::BORDER_REPLICATE), BorderType(cv::BORDER_REFLECT)),
1197 testing::Values(BlockSize(3), BlockSize(5), BlockSize(7)),
1198 testing::Values(ApertureSize(0), ApertureSize(3), ApertureSize(5), ApertureSize(7))));