CLAHE Python bindings
[profile/ivi/opencv.git] / modules / gpu / test / test_imgproc.cpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
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.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
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.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
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.
26 //
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.
29 //
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.
40 //
41 //M*/
42
43 #include "test_precomp.hpp"
44
45 #ifdef HAVE_CUDA
46
47 using namespace cvtest;
48
49 ///////////////////////////////////////////////////////////////////////////////////////////////////////
50 // Integral
51
52 PARAM_TEST_CASE(Integral, cv::gpu::DeviceInfo, cv::Size, UseRoi)
53 {
54     cv::gpu::DeviceInfo devInfo;
55     cv::Size size;
56     bool useRoi;
57
58     virtual void SetUp()
59     {
60         devInfo = GET_PARAM(0);
61         size = GET_PARAM(1);
62         useRoi = GET_PARAM(2);
63
64         cv::gpu::setDevice(devInfo.deviceID());
65     }
66 };
67
68 GPU_TEST_P(Integral, Accuracy)
69 {
70     cv::Mat src = randomMat(size, CV_8UC1);
71
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);
74
75     cv::Mat dst_gold;
76     cv::integral(src, dst_gold, CV_32S);
77
78     EXPECT_MAT_NEAR(dst_gold, dst, 0.0);
79 }
80
81 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Integral, testing::Combine(
82     ALL_DEVICES,
83     DIFFERENT_SIZES,
84     WHOLE_SUBMAT));
85
86 ///////////////////////////////////////////////////////////////////////////////////////////////////////
87 // HistEven
88
89 struct HistEven : testing::TestWithParam<cv::gpu::DeviceInfo>
90 {
91     cv::gpu::DeviceInfo devInfo;
92
93     virtual void SetUp()
94     {
95         devInfo = GetParam();
96
97         cv::gpu::setDevice(devInfo.deviceID());
98     }
99 };
100
101 GPU_TEST_P(HistEven, Accuracy)
102 {
103     cv::Mat img = readImage("stereobm/aloe-L.png");
104     ASSERT_FALSE(img.empty());
105
106     cv::Mat hsv;
107     cv::cvtColor(img, hsv, CV_BGR2HSV);
108
109     int hbins = 30;
110     float hranges[] = {0.0f, 180.0f};
111
112     std::vector<cv::gpu::GpuMat> srcs;
113     cv::gpu::split(loadMat(hsv), srcs);
114
115     cv::gpu::GpuMat hist;
116     cv::gpu::histEven(srcs[0], hist, hbins, (int)hranges[0], (int)hranges[1]);
117
118     cv::MatND histnd;
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);
123
124     cv::Mat hist_gold = histnd;
125     hist_gold = hist_gold.t();
126     hist_gold.convertTo(hist_gold, CV_32S);
127
128     EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
129 }
130
131 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, HistEven, ALL_DEVICES);
132
133 ///////////////////////////////////////////////////////////////////////////////////////////////////////
134 // CalcHist
135
136 namespace
137 {
138     void calcHistGold(const cv::Mat& src, cv::Mat& hist)
139     {
140         hist.create(1, 256, CV_32SC1);
141         hist.setTo(cv::Scalar::all(0));
142
143         int* hist_row = hist.ptr<int>();
144         for (int y = 0; y < src.rows; ++y)
145         {
146             const uchar* src_row = src.ptr(y);
147
148             for (int x = 0; x < src.cols; ++x)
149                 ++hist_row[src_row[x]];
150         }
151     }
152 }
153
154 PARAM_TEST_CASE(CalcHist, cv::gpu::DeviceInfo, cv::Size)
155 {
156     cv::gpu::DeviceInfo devInfo;
157
158     cv::Size size;
159
160     virtual void SetUp()
161     {
162         devInfo = GET_PARAM(0);
163         size = GET_PARAM(1);
164
165         cv::gpu::setDevice(devInfo.deviceID());
166     }
167 };
168
169 GPU_TEST_P(CalcHist, Accuracy)
170 {
171     cv::Mat src = randomMat(size, CV_8UC1);
172
173     cv::gpu::GpuMat hist;
174     cv::gpu::calcHist(loadMat(src), hist);
175
176     cv::Mat hist_gold;
177     calcHistGold(src, hist_gold);
178
179     EXPECT_MAT_NEAR(hist_gold, hist, 0.0);
180 }
181
182 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CalcHist, testing::Combine(
183     ALL_DEVICES,
184     DIFFERENT_SIZES));
185
186 ///////////////////////////////////////////////////////////////////////////////////////////////////////
187 // EqualizeHist
188
189 PARAM_TEST_CASE(EqualizeHist, cv::gpu::DeviceInfo, cv::Size)
190 {
191     cv::gpu::DeviceInfo devInfo;
192     cv::Size size;
193
194     virtual void SetUp()
195     {
196         devInfo = GET_PARAM(0);
197         size = GET_PARAM(1);
198
199         cv::gpu::setDevice(devInfo.deviceID());
200     }
201 };
202
203 GPU_TEST_P(EqualizeHist, Accuracy)
204 {
205     cv::Mat src = randomMat(size, CV_8UC1);
206
207     cv::gpu::GpuMat dst;
208     cv::gpu::equalizeHist(loadMat(src), dst);
209
210     cv::Mat dst_gold;
211     cv::equalizeHist(src, dst_gold);
212
213     EXPECT_MAT_NEAR(dst_gold, dst, 3.0);
214 }
215
216 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, EqualizeHist, testing::Combine(
217     ALL_DEVICES,
218     DIFFERENT_SIZES));
219
220 ///////////////////////////////////////////////////////////////////////////////////////////////////////
221 // CLAHE
222
223 namespace
224 {
225     IMPLEMENT_PARAM_CLASS(ClipLimit, double)
226 }
227
228 PARAM_TEST_CASE(CLAHE, cv::gpu::DeviceInfo, cv::Size, ClipLimit)
229 {
230     cv::gpu::DeviceInfo devInfo;
231     cv::Size size;
232     double clipLimit;
233
234     virtual void SetUp()
235     {
236         devInfo = GET_PARAM(0);
237         size = GET_PARAM(1);
238         clipLimit = GET_PARAM(2);
239
240         cv::gpu::setDevice(devInfo.deviceID());
241     }
242 };
243
244 GPU_TEST_P(CLAHE, Accuracy)
245 {
246     cv::Mat src = randomMat(size, CV_8UC1);
247
248     cv::Ptr<cv::gpu::CLAHE> clahe = cv::gpu::createCLAHE(clipLimit);
249     cv::gpu::GpuMat dst;
250     clahe->apply(loadMat(src), dst);
251
252     cv::Ptr<cv::CLAHE> clahe_gold = cv::createCLAHE(clipLimit);
253     cv::Mat dst_gold;
254     clahe_gold->apply(src, dst_gold);
255
256     ASSERT_MAT_NEAR(dst_gold, dst, 1.0);
257 }
258
259 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CLAHE, testing::Combine(
260     ALL_DEVICES,
261     DIFFERENT_SIZES,
262     testing::Values(0.0, 40.0)));
263
264 ////////////////////////////////////////////////////////////////////////
265 // ColumnSum
266
267 PARAM_TEST_CASE(ColumnSum, cv::gpu::DeviceInfo, cv::Size)
268 {
269     cv::gpu::DeviceInfo devInfo;
270     cv::Size size;
271
272     virtual void SetUp()
273     {
274         devInfo = GET_PARAM(0);
275         size = GET_PARAM(1);
276
277         cv::gpu::setDevice(devInfo.deviceID());
278     }
279 };
280
281 GPU_TEST_P(ColumnSum, Accuracy)
282 {
283     cv::Mat src = randomMat(size, CV_32FC1);
284
285     cv::gpu::GpuMat d_dst;
286     cv::gpu::columnSum(loadMat(src), d_dst);
287
288     cv::Mat dst(d_dst);
289
290     for (int j = 0; j < src.cols; ++j)
291     {
292         float gold = src.at<float>(0, j);
293         float res = dst.at<float>(0, j);
294         ASSERT_NEAR(res, gold, 1e-5);
295     }
296
297     for (int i = 1; i < src.rows; ++i)
298     {
299         for (int j = 0; j < src.cols; ++j)
300         {
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);
304         }
305     }
306 }
307
308 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, ColumnSum, testing::Combine(
309     ALL_DEVICES,
310     DIFFERENT_SIZES));
311
312 ////////////////////////////////////////////////////////
313 // Canny
314
315 namespace
316 {
317     IMPLEMENT_PARAM_CLASS(AppertureSize, int);
318     IMPLEMENT_PARAM_CLASS(L2gradient, bool);
319 }
320
321 PARAM_TEST_CASE(Canny, cv::gpu::DeviceInfo, AppertureSize, L2gradient, UseRoi)
322 {
323     cv::gpu::DeviceInfo devInfo;
324     int apperture_size;
325     bool useL2gradient;
326     bool useRoi;
327
328     virtual void SetUp()
329     {
330         devInfo = GET_PARAM(0);
331         apperture_size = GET_PARAM(1);
332         useL2gradient = GET_PARAM(2);
333         useRoi = GET_PARAM(3);
334
335         cv::gpu::setDevice(devInfo.deviceID());
336     }
337 };
338
339 GPU_TEST_P(Canny, Accuracy)
340 {
341     cv::Mat img = readImage("stereobm/aloe-L.png", cv::IMREAD_GRAYSCALE);
342     ASSERT_FALSE(img.empty());
343
344     double low_thresh = 50.0;
345     double high_thresh = 100.0;
346
347     if (!supportFeature(devInfo, cv::gpu::SHARED_ATOMICS))
348     {
349         try
350         {
351         cv::gpu::GpuMat edges;
352         cv::gpu::Canny(loadMat(img), edges, low_thresh, high_thresh, apperture_size, useL2gradient);
353         }
354         catch (const cv::Exception& e)
355         {
356             ASSERT_EQ(CV_StsNotImplemented, e.code);
357         }
358     }
359     else
360     {
361         cv::gpu::GpuMat edges;
362         cv::gpu::Canny(loadMat(img, useRoi), edges, low_thresh, high_thresh, apperture_size, useL2gradient);
363
364         cv::Mat edges_gold;
365         cv::Canny(img, edges_gold, low_thresh, high_thresh, apperture_size, useL2gradient);
366
367         EXPECT_MAT_SIMILAR(edges_gold, edges, 2e-2);
368     }
369 }
370
371 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Canny, testing::Combine(
372     ALL_DEVICES,
373     testing::Values(AppertureSize(3), AppertureSize(5)),
374     testing::Values(L2gradient(false), L2gradient(true)),
375     WHOLE_SUBMAT));
376
377 ////////////////////////////////////////////////////////////////////////////////
378 // MeanShift
379
380 struct MeanShift : testing::TestWithParam<cv::gpu::DeviceInfo>
381 {
382     cv::gpu::DeviceInfo devInfo;
383
384     cv::Mat img;
385
386     int spatialRad;
387     int colorRad;
388
389     virtual void SetUp()
390     {
391         devInfo = GetParam();
392
393         cv::gpu::setDevice(devInfo.deviceID());
394
395         img = readImageType("meanshift/cones.png", CV_8UC4);
396         ASSERT_FALSE(img.empty());
397
398         spatialRad = 30;
399         colorRad = 30;
400     }
401 };
402
403 GPU_TEST_P(MeanShift, Filtering)
404 {
405     cv::Mat img_template;
406     if (supportFeature(devInfo, cv::gpu::FEATURE_SET_COMPUTE_20))
407         img_template = readImage("meanshift/con_result.png");
408     else
409         img_template = readImage("meanshift/con_result_CC1X.png");
410     ASSERT_FALSE(img_template.empty());
411
412     cv::gpu::GpuMat d_dst;
413     cv::gpu::meanShiftFiltering(loadMat(img), d_dst, spatialRad, colorRad);
414
415     ASSERT_EQ(CV_8UC4, d_dst.type());
416
417     cv::Mat dst(d_dst);
418
419     cv::Mat result;
420     cv::cvtColor(dst, result, CV_BGRA2BGR);
421
422     EXPECT_MAT_NEAR(img_template, result, 0.0);
423 }
424
425 GPU_TEST_P(MeanShift, Proc)
426 {
427     cv::FileStorage fs;
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);
430     else
431         fs.open(std::string(cvtest::TS::ptr()->get_data_path()) + "meanshift/spmap_CC1X.yaml", cv::FileStorage::READ);
432     ASSERT_TRUE(fs.isOpened());
433
434     cv::Mat spmap_template;
435     fs["spmap"] >> spmap_template;
436     ASSERT_FALSE(spmap_template.empty());
437
438     cv::gpu::GpuMat rmap_filtered;
439     cv::gpu::meanShiftFiltering(loadMat(img), rmap_filtered, spatialRad, colorRad);
440
441     cv::gpu::GpuMat rmap;
442     cv::gpu::GpuMat spmap;
443     cv::gpu::meanShiftProc(loadMat(img), rmap, spmap, spatialRad, colorRad);
444
445     ASSERT_EQ(CV_8UC4, rmap.type());
446
447     EXPECT_MAT_NEAR(rmap_filtered, rmap, 0.0);
448     EXPECT_MAT_NEAR(spmap_template, spmap, 0.0);
449 }
450
451 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MeanShift, ALL_DEVICES);
452
453 ////////////////////////////////////////////////////////////////////////////////
454 // MeanShiftSegmentation
455
456 namespace
457 {
458     IMPLEMENT_PARAM_CLASS(MinSize, int);
459 }
460
461 PARAM_TEST_CASE(MeanShiftSegmentation, cv::gpu::DeviceInfo, MinSize)
462 {
463     cv::gpu::DeviceInfo devInfo;
464     int minsize;
465
466     virtual void SetUp()
467     {
468         devInfo = GET_PARAM(0);
469         minsize = GET_PARAM(1);
470
471         cv::gpu::setDevice(devInfo.deviceID());
472     }
473 };
474
475 GPU_TEST_P(MeanShiftSegmentation, Regression)
476 {
477     cv::Mat img = readImageType("meanshift/cones.png", CV_8UC4);
478     ASSERT_FALSE(img.empty());
479
480     std::ostringstream path;
481     path << "meanshift/cones_segmented_sp10_sr10_minsize" << minsize;
482     if (supportFeature(devInfo, cv::gpu::FEATURE_SET_COMPUTE_20))
483         path << ".png";
484     else
485         path << "_CC1X.png";
486     cv::Mat dst_gold = readImage(path.str());
487     ASSERT_FALSE(dst_gold.empty());
488
489     cv::Mat dst;
490     cv::gpu::meanShiftSegmentation(loadMat(img), dst, 10, 10, minsize);
491
492     cv::Mat dst_rgb;
493     cv::cvtColor(dst, dst_rgb, CV_BGRA2BGR);
494
495     EXPECT_MAT_SIMILAR(dst_gold, dst_rgb, 1e-3);
496 }
497
498 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MeanShiftSegmentation, testing::Combine(
499     ALL_DEVICES,
500     testing::Values(MinSize(0), MinSize(4), MinSize(20), MinSize(84), MinSize(340), MinSize(1364))));
501
502 ////////////////////////////////////////////////////////////////////////////
503 // Blend
504
505 namespace
506 {
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)
509     {
510         result_gold.create(img1.size(), img1.type());
511
512         int cn = img1.channels();
513
514         for (int y = 0; y < img1.rows; ++y)
515         {
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);
521
522             for (int x = 0; x < img1.cols * cn; ++x)
523             {
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));
527             }
528         }
529     }
530 }
531
532 PARAM_TEST_CASE(Blend, cv::gpu::DeviceInfo, cv::Size, MatType, UseRoi)
533 {
534     cv::gpu::DeviceInfo devInfo;
535     cv::Size size;
536     int type;
537     bool useRoi;
538
539     virtual void SetUp()
540     {
541         devInfo = GET_PARAM(0);
542         size = GET_PARAM(1);
543         type = GET_PARAM(2);
544         useRoi = GET_PARAM(3);
545
546         cv::gpu::setDevice(devInfo.deviceID());
547     }
548 };
549
550 GPU_TEST_P(Blend, Accuracy)
551 {
552     int depth = CV_MAT_DEPTH(type);
553
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);
558
559     cv::gpu::GpuMat result;
560     cv::gpu::blendLinear(loadMat(img1, useRoi), loadMat(img2, useRoi), loadMat(weights1, useRoi), loadMat(weights2, useRoi), result);
561
562     cv::Mat result_gold;
563     if (depth == CV_8U)
564         blendLinearGold<uchar>(img1, img2, weights1, weights2, result_gold);
565     else
566         blendLinearGold<float>(img1, img2, weights1, weights2, result_gold);
567
568     EXPECT_MAT_NEAR(result_gold, result, CV_MAT_DEPTH(type) == CV_8U ? 1.0 : 1e-5);
569 }
570
571 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Blend, testing::Combine(
572     ALL_DEVICES,
573     DIFFERENT_SIZES,
574     testing::Values(MatType(CV_8UC1), MatType(CV_8UC3), MatType(CV_8UC4), MatType(CV_32FC1), MatType(CV_32FC3), MatType(CV_32FC4)),
575     WHOLE_SUBMAT));
576
577 ////////////////////////////////////////////////////////
578 // Convolve
579
580 namespace
581 {
582     void convolveDFT(const cv::Mat& A, const cv::Mat& B, cv::Mat& C, bool ccorr = false)
583     {
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());
586         cv::Size dftSize;
587
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);
591
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));
595
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));
598         A.copyTo(roiA);
599         cv::Mat roiB(tempB, cv::Rect(0, 0, B.cols, B.rows));
600         B.copyTo(roiB);
601
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);
606
607         // multiply the spectrums;
608         // the function handles packed spectrum representations well
609         cv::mulSpectrums(tempA, tempB, tempA, 0, ccorr);
610
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);
616
617         // now copy the result back to C.
618         tempA(cv::Rect(0, 0, C.cols, C.rows)).copyTo(C);
619     }
620
621     IMPLEMENT_PARAM_CLASS(KSize, int);
622     IMPLEMENT_PARAM_CLASS(Ccorr, bool);
623 }
624
625 PARAM_TEST_CASE(Convolve, cv::gpu::DeviceInfo, cv::Size, KSize, Ccorr)
626 {
627     cv::gpu::DeviceInfo devInfo;
628     cv::Size size;
629     int ksize;
630     bool ccorr;
631
632     virtual void SetUp()
633     {
634         devInfo = GET_PARAM(0);
635         size = GET_PARAM(1);
636         ksize = GET_PARAM(2);
637         ccorr = GET_PARAM(3);
638
639         cv::gpu::setDevice(devInfo.deviceID());
640     }
641 };
642
643 GPU_TEST_P(Convolve, Accuracy)
644 {
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);
647
648     cv::gpu::GpuMat dst;
649     cv::gpu::convolve(loadMat(src), loadMat(kernel), dst, ccorr);
650
651     cv::Mat dst_gold;
652     convolveDFT(src, kernel, dst_gold, ccorr);
653
654     EXPECT_MAT_NEAR(dst, dst_gold, 1e-1);
655 }
656
657 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Convolve, testing::Combine(
658     ALL_DEVICES,
659     DIFFERENT_SIZES,
660     testing::Values(KSize(3), KSize(7), KSize(11), KSize(17), KSize(19), KSize(23), KSize(45)),
661     testing::Values(Ccorr(false), Ccorr(true))));
662
663 ////////////////////////////////////////////////////////////////////////////////
664 // MatchTemplate8U
665
666 CV_ENUM(TemplateMethod, TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED)
667
668 namespace
669 {
670     IMPLEMENT_PARAM_CLASS(TemplateSize, cv::Size);
671 }
672
673 PARAM_TEST_CASE(MatchTemplate8U, cv::gpu::DeviceInfo, cv::Size, TemplateSize, Channels, TemplateMethod)
674 {
675     cv::gpu::DeviceInfo devInfo;
676     cv::Size size;
677     cv::Size templ_size;
678     int cn;
679     int method;
680
681     virtual void SetUp()
682     {
683         devInfo = GET_PARAM(0);
684         size = GET_PARAM(1);
685         templ_size = GET_PARAM(2);
686         cn = GET_PARAM(3);
687         method = GET_PARAM(4);
688
689         cv::gpu::setDevice(devInfo.deviceID());
690     }
691 };
692
693 GPU_TEST_P(MatchTemplate8U, Accuracy)
694 {
695     cv::Mat image = randomMat(size, CV_MAKETYPE(CV_8U, cn));
696     cv::Mat templ = randomMat(templ_size, CV_MAKETYPE(CV_8U, cn));
697
698     cv::gpu::GpuMat dst;
699     cv::gpu::matchTemplate(loadMat(image), loadMat(templ), dst, method);
700
701     cv::Mat dst_gold;
702     cv::matchTemplate(image, templ, dst_gold, method);
703
704     EXPECT_MAT_NEAR(dst_gold, dst, templ_size.area() * 1e-1);
705 }
706
707 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplate8U, testing::Combine(
708     ALL_DEVICES,
709     DIFFERENT_SIZES,
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()));
713
714 ////////////////////////////////////////////////////////////////////////////////
715 // MatchTemplate32F
716
717 PARAM_TEST_CASE(MatchTemplate32F, cv::gpu::DeviceInfo, cv::Size, TemplateSize, Channels, TemplateMethod)
718 {
719     cv::gpu::DeviceInfo devInfo;
720     cv::Size size;
721     cv::Size templ_size;
722     int cn;
723     int method;
724
725     int n, m, h, w;
726
727     virtual void SetUp()
728     {
729         devInfo = GET_PARAM(0);
730         size = GET_PARAM(1);
731         templ_size = GET_PARAM(2);
732         cn = GET_PARAM(3);
733         method = GET_PARAM(4);
734
735         cv::gpu::setDevice(devInfo.deviceID());
736     }
737 };
738
739 GPU_TEST_P(MatchTemplate32F, Regression)
740 {
741     cv::Mat image = randomMat(size, CV_MAKETYPE(CV_32F, cn));
742     cv::Mat templ = randomMat(templ_size, CV_MAKETYPE(CV_32F, cn));
743
744     cv::gpu::GpuMat dst;
745     cv::gpu::matchTemplate(loadMat(image), loadMat(templ), dst, method);
746
747     cv::Mat dst_gold;
748     cv::matchTemplate(image, templ, dst_gold, method);
749
750     EXPECT_MAT_NEAR(dst_gold, dst, templ_size.area() * 1e-1);
751 }
752
753 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplate32F, testing::Combine(
754     ALL_DEVICES,
755     DIFFERENT_SIZES,
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))));
759
760 ////////////////////////////////////////////////////////////////////////////////
761 // MatchTemplateBlackSource
762
763 PARAM_TEST_CASE(MatchTemplateBlackSource, cv::gpu::DeviceInfo, TemplateMethod)
764 {
765     cv::gpu::DeviceInfo devInfo;
766     int method;
767
768     virtual void SetUp()
769     {
770         devInfo = GET_PARAM(0);
771         method = GET_PARAM(1);
772
773         cv::gpu::setDevice(devInfo.deviceID());
774     }
775 };
776
777 GPU_TEST_P(MatchTemplateBlackSource, Accuracy)
778 {
779     cv::Mat image = readImage("matchtemplate/black.png");
780     ASSERT_FALSE(image.empty());
781
782     cv::Mat pattern = readImage("matchtemplate/cat.png");
783     ASSERT_FALSE(pattern.empty());
784
785     cv::gpu::GpuMat d_dst;
786     cv::gpu::matchTemplate(loadMat(image), loadMat(pattern), d_dst, method);
787
788     cv::Mat dst(d_dst);
789
790     double maxValue;
791     cv::Point maxLoc;
792     cv::minMaxLoc(dst, NULL, &maxValue, NULL, &maxLoc);
793
794     cv::Point maxLocGold = cv::Point(284, 12);
795
796     ASSERT_EQ(maxLocGold, maxLoc);
797 }
798
799 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplateBlackSource, testing::Combine(
800     ALL_DEVICES,
801     testing::Values(TemplateMethod(cv::TM_CCOEFF_NORMED), TemplateMethod(cv::TM_CCORR_NORMED))));
802
803 ////////////////////////////////////////////////////////////////////////////////
804 // MatchTemplate_CCOEF_NORMED
805
806 PARAM_TEST_CASE(MatchTemplate_CCOEF_NORMED, cv::gpu::DeviceInfo, std::pair<std::string, std::string>)
807 {
808     cv::gpu::DeviceInfo devInfo;
809     std::string imageName;
810     std::string patternName;
811
812     virtual void SetUp()
813     {
814         devInfo = GET_PARAM(0);
815         imageName = GET_PARAM(1).first;
816         patternName = GET_PARAM(1).second;
817
818         cv::gpu::setDevice(devInfo.deviceID());
819     }
820 };
821
822 GPU_TEST_P(MatchTemplate_CCOEF_NORMED, Accuracy)
823 {
824     cv::Mat image = readImage(imageName);
825     ASSERT_FALSE(image.empty());
826
827     cv::Mat pattern = readImage(patternName);
828     ASSERT_FALSE(pattern.empty());
829
830     cv::gpu::GpuMat d_dst;
831     cv::gpu::matchTemplate(loadMat(image), loadMat(pattern), d_dst, CV_TM_CCOEFF_NORMED);
832
833     cv::Mat dst(d_dst);
834
835     cv::Point minLoc, maxLoc;
836     double minVal, maxVal;
837     cv::minMaxLoc(dst, &minVal, &maxVal, &minLoc, &maxLoc);
838
839     cv::Mat dstGold;
840     cv::matchTemplate(image, pattern, dstGold, CV_TM_CCOEFF_NORMED);
841
842     double minValGold, maxValGold;
843     cv::Point minLocGold, maxLocGold;
844     cv::minMaxLoc(dstGold, &minValGold, &maxValGold, &minLocGold, &maxLocGold);
845
846     ASSERT_EQ(minLocGold, minLoc);
847     ASSERT_EQ(maxLocGold, maxLoc);
848     ASSERT_LE(maxVal, 1.0);
849     ASSERT_GE(minVal, -1.0);
850 }
851
852 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplate_CCOEF_NORMED, testing::Combine(
853     ALL_DEVICES,
854     testing::Values(std::make_pair(std::string("matchtemplate/source-0.png"), std::string("matchtemplate/target-0.png")))));
855
856 ////////////////////////////////////////////////////////////////////////////////
857 // MatchTemplate_CanFindBigTemplate
858
859 struct MatchTemplate_CanFindBigTemplate : testing::TestWithParam<cv::gpu::DeviceInfo>
860 {
861     cv::gpu::DeviceInfo devInfo;
862
863     virtual void SetUp()
864     {
865         devInfo = GetParam();
866
867         cv::gpu::setDevice(devInfo.deviceID());
868     }
869 };
870
871 GPU_TEST_P(MatchTemplate_CanFindBigTemplate, SQDIFF_NORMED)
872 {
873     cv::Mat scene = readImage("matchtemplate/scene.png");
874     ASSERT_FALSE(scene.empty());
875
876     cv::Mat templ = readImage("matchtemplate/template.png");
877     ASSERT_FALSE(templ.empty());
878
879     cv::gpu::GpuMat d_result;
880     cv::gpu::matchTemplate(loadMat(scene), loadMat(templ), d_result, CV_TM_SQDIFF_NORMED);
881
882     cv::Mat result(d_result);
883
884     double minVal;
885     cv::Point minLoc;
886     cv::minMaxLoc(result, &minVal, 0, &minLoc, 0);
887
888     ASSERT_GE(minVal, 0);
889     ASSERT_LT(minVal, 1e-3);
890     ASSERT_EQ(344, minLoc.x);
891     ASSERT_EQ(0, minLoc.y);
892 }
893
894 GPU_TEST_P(MatchTemplate_CanFindBigTemplate, SQDIFF)
895 {
896     cv::Mat scene = readImage("matchtemplate/scene.png");
897     ASSERT_FALSE(scene.empty());
898
899     cv::Mat templ = readImage("matchtemplate/template.png");
900     ASSERT_FALSE(templ.empty());
901
902     cv::gpu::GpuMat d_result;
903     cv::gpu::matchTemplate(loadMat(scene), loadMat(templ), d_result, CV_TM_SQDIFF);
904
905     cv::Mat result(d_result);
906
907     double minVal;
908     cv::Point minLoc;
909     cv::minMaxLoc(result, &minVal, 0, &minLoc, 0);
910
911     ASSERT_GE(minVal, 0);
912     ASSERT_EQ(344, minLoc.x);
913     ASSERT_EQ(0, minLoc.y);
914 }
915
916 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MatchTemplate_CanFindBigTemplate, ALL_DEVICES);
917
918 ////////////////////////////////////////////////////////////////////////////
919 // MulSpectrums
920
921 CV_FLAGS(DftFlags, 0, DFT_INVERSE, DFT_SCALE, DFT_ROWS, DFT_COMPLEX_OUTPUT, DFT_REAL_OUTPUT)
922
923 PARAM_TEST_CASE(MulSpectrums, cv::gpu::DeviceInfo, cv::Size, DftFlags)
924 {
925     cv::gpu::DeviceInfo devInfo;
926     cv::Size size;
927     int flag;
928
929     cv::Mat a, b;
930
931     virtual void SetUp()
932     {
933         devInfo = GET_PARAM(0);
934         size = GET_PARAM(1);
935         flag = GET_PARAM(2);
936
937         cv::gpu::setDevice(devInfo.deviceID());
938
939         a = randomMat(size, CV_32FC2);
940         b = randomMat(size, CV_32FC2);
941     }
942 };
943
944 GPU_TEST_P(MulSpectrums, Simple)
945 {
946     cv::gpu::GpuMat c;
947     cv::gpu::mulSpectrums(loadMat(a), loadMat(b), c, flag, false);
948
949     cv::Mat c_gold;
950     cv::mulSpectrums(a, b, c_gold, flag, false);
951
952     EXPECT_MAT_NEAR(c_gold, c, 1e-2);
953 }
954
955 GPU_TEST_P(MulSpectrums, Scaled)
956 {
957     float scale = 1.f / size.area();
958
959     cv::gpu::GpuMat c;
960     cv::gpu::mulAndScaleSpectrums(loadMat(a), loadMat(b), c, flag, scale, false);
961
962     cv::Mat c_gold;
963     cv::mulSpectrums(a, b, c_gold, flag, false);
964     c_gold.convertTo(c_gold, c_gold.type(), scale);
965
966     EXPECT_MAT_NEAR(c_gold, c, 1e-2);
967 }
968
969 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, MulSpectrums, testing::Combine(
970     ALL_DEVICES,
971     DIFFERENT_SIZES,
972     testing::Values(DftFlags(0), DftFlags(cv::DFT_ROWS))));
973
974 ////////////////////////////////////////////////////////////////////////////
975 // Dft
976
977 struct Dft : testing::TestWithParam<cv::gpu::DeviceInfo>
978 {
979     cv::gpu::DeviceInfo devInfo;
980
981     virtual void SetUp()
982     {
983         devInfo = GetParam();
984
985         cv::gpu::setDevice(devInfo.deviceID());
986     }
987 };
988
989 namespace
990 {
991     void testC2C(const std::string& hint, int cols, int rows, int flags, bool inplace)
992     {
993         SCOPED_TRACE(hint);
994
995         cv::Mat a = randomMat(cv::Size(cols, rows), CV_32FC2, 0.0, 10.0);
996
997         cv::Mat b_gold;
998         cv::dft(a, b_gold, flags);
999
1000         cv::gpu::GpuMat d_b;
1001         cv::gpu::GpuMat d_b_data;
1002         if (inplace)
1003         {
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());
1006         }
1007         cv::gpu::dft(loadMat(a), d_b, cv::Size(cols, rows), flags);
1008
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);
1013     }
1014 }
1015
1016 GPU_TEST_P(Dft, C2C)
1017 {
1018     int cols = randomInt(2, 100);
1019     int rows = randomInt(2, 100);
1020
1021     for (int i = 0; i < 2; ++i)
1022     {
1023         bool inplace = i != 0;
1024
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);
1038     }
1039 }
1040
1041 namespace
1042 {
1043     void testR2CThenC2R(const std::string& hint, int cols, int rows, bool inplace)
1044     {
1045         SCOPED_TRACE(hint);
1046
1047         cv::Mat a = randomMat(cv::Size(cols, rows), CV_32FC1, 0.0, 10.0);
1048
1049         cv::gpu::GpuMat d_b, d_c;
1050         cv::gpu::GpuMat d_b_data, d_c_data;
1051         if (inplace)
1052         {
1053             if (a.cols == 1)
1054             {
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());
1057             }
1058             else
1059             {
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());
1062             }
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());
1065         }
1066
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);
1069
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());
1074
1075         cv::Mat c(d_c);
1076         EXPECT_MAT_NEAR(a, c, rows * cols * 1e-5);
1077     }
1078 }
1079
1080 GPU_TEST_P(Dft, R2CThenC2R)
1081 {
1082     int cols = randomInt(2, 100);
1083     int rows = randomInt(2, 100);
1084
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);
1093
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);
1100 }
1101
1102 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, Dft, ALL_DEVICES);
1103
1104 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1105 // CornerHarris
1106
1107 namespace
1108 {
1109     IMPLEMENT_PARAM_CLASS(BlockSize, int);
1110     IMPLEMENT_PARAM_CLASS(ApertureSize, int);
1111 }
1112
1113 PARAM_TEST_CASE(CornerHarris, cv::gpu::DeviceInfo, MatType, BorderType, BlockSize, ApertureSize)
1114 {
1115     cv::gpu::DeviceInfo devInfo;
1116     int type;
1117     int borderType;
1118     int blockSize;
1119     int apertureSize;
1120
1121     virtual void SetUp()
1122     {
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);
1128
1129         cv::gpu::setDevice(devInfo.deviceID());
1130     }
1131 };
1132
1133 GPU_TEST_P(CornerHarris, Accuracy)
1134 {
1135     cv::Mat src = readImageType("stereobm/aloe-L.png", type);
1136     ASSERT_FALSE(src.empty());
1137
1138     double k = randomDouble(0.1, 0.9);
1139
1140     cv::gpu::GpuMat dst;
1141     cv::gpu::cornerHarris(loadMat(src), dst, blockSize, apertureSize, k, borderType);
1142
1143     cv::Mat dst_gold;
1144     cv::cornerHarris(src, dst_gold, blockSize, apertureSize, k, borderType);
1145
1146     EXPECT_MAT_NEAR(dst_gold, dst, 0.02);
1147 }
1148
1149 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CornerHarris, testing::Combine(
1150     ALL_DEVICES,
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))));
1155
1156 ///////////////////////////////////////////////////////////////////////////////////////////////////////
1157 // cornerMinEigen
1158
1159 PARAM_TEST_CASE(CornerMinEigen, cv::gpu::DeviceInfo, MatType, BorderType, BlockSize, ApertureSize)
1160 {
1161     cv::gpu::DeviceInfo devInfo;
1162     int type;
1163     int borderType;
1164     int blockSize;
1165     int apertureSize;
1166
1167     virtual void SetUp()
1168     {
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);
1174
1175         cv::gpu::setDevice(devInfo.deviceID());
1176     }
1177 };
1178
1179 GPU_TEST_P(CornerMinEigen, Accuracy)
1180 {
1181     cv::Mat src = readImageType("stereobm/aloe-L.png", type);
1182     ASSERT_FALSE(src.empty());
1183
1184     cv::gpu::GpuMat dst;
1185     cv::gpu::cornerMinEigenVal(loadMat(src), dst, blockSize, apertureSize, borderType);
1186
1187     cv::Mat dst_gold;
1188     cv::cornerMinEigenVal(src, dst_gold, blockSize, apertureSize, borderType);
1189
1190     EXPECT_MAT_NEAR(dst_gold, dst, 0.02);
1191 }
1192
1193 INSTANTIATE_TEST_CASE_P(GPU_ImgProc, CornerMinEigen, testing::Combine(
1194     ALL_DEVICES,
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))));
1199
1200 #endif // HAVE_CUDA