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 #include "opencv2/photo.hpp"
45 #include <string>
46 
47 using namespace cv;
48 using namespace std;
49 
50 //#define DUMP_RESULTS
51 
52 #ifdef DUMP_RESULTS
53 #  define DUMP(image, path) imwrite(path, image)
54 #else
55 #  define DUMP(image, path)
56 #endif
57 
58 
TEST(Photo_DenoisingGrayscale,regression)59 TEST(Photo_DenoisingGrayscale, regression)
60 {
61     string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";
62     string original_path = folder + "lena_noised_gaussian_sigma=10.png";
63     string expected_path = folder + "lena_noised_denoised_grayscale_tw=7_sw=21_h=10.png";
64 
65     Mat original = imread(original_path, IMREAD_GRAYSCALE);
66     Mat expected = imread(expected_path, IMREAD_GRAYSCALE);
67 
68     ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;
69     ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
70 
71     Mat result;
72     fastNlMeansDenoising(original, result, 10);
73 
74     DUMP(result, expected_path + ".res.png");
75 
76     ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));
77 }
78 
TEST(Photo_DenoisingColored,regression)79 TEST(Photo_DenoisingColored, regression)
80 {
81     string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";
82     string original_path = folder + "lena_noised_gaussian_sigma=10.png";
83     string expected_path = folder + "lena_noised_denoised_lab12_tw=7_sw=21_h=10_h2=10.png";
84 
85     Mat original = imread(original_path, IMREAD_COLOR);
86     Mat expected = imread(expected_path, IMREAD_COLOR);
87 
88     ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;
89     ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
90 
91     Mat result;
92     fastNlMeansDenoisingColored(original, result, 10, 10);
93 
94     DUMP(result, expected_path + ".res.png");
95 
96     ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));
97 }
98 
TEST(Photo_DenoisingGrayscaleMulti,regression)99 TEST(Photo_DenoisingGrayscaleMulti, regression)
100 {
101     const int imgs_count = 3;
102     string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";
103 
104     string expected_path = folder + "lena_noised_denoised_multi_tw=7_sw=21_h=15.png";
105     Mat expected = imread(expected_path, IMREAD_GRAYSCALE);
106     ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
107 
108     vector<Mat> original(imgs_count);
109     for (int i = 0; i < imgs_count; i++)
110     {
111         string original_path = format("%slena_noised_gaussian_sigma=20_multi_%d.png", folder.c_str(), i);
112         original[i] = imread(original_path, IMREAD_GRAYSCALE);
113         ASSERT_FALSE(original[i].empty()) << "Could not load input image " << original_path;
114     }
115 
116     Mat result;
117     fastNlMeansDenoisingMulti(original, result, imgs_count / 2, imgs_count, 15);
118 
119     DUMP(result, expected_path + ".res.png");
120 
121     ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));
122 }
123 
TEST(Photo_DenoisingColoredMulti,regression)124 TEST(Photo_DenoisingColoredMulti, regression)
125 {
126     const int imgs_count = 3;
127     string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";
128 
129     string expected_path = folder + "lena_noised_denoised_multi_lab12_tw=7_sw=21_h=10_h2=15.png";
130     Mat expected = imread(expected_path, IMREAD_COLOR);
131     ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;
132 
133     vector<Mat> original(imgs_count);
134     for (int i = 0; i < imgs_count; i++)
135     {
136         string original_path = format("%slena_noised_gaussian_sigma=20_multi_%d.png", folder.c_str(), i);
137         original[i] = imread(original_path, IMREAD_COLOR);
138         ASSERT_FALSE(original[i].empty()) << "Could not load input image " << original_path;
139     }
140 
141     Mat result;
142     fastNlMeansDenoisingColoredMulti(original, result, imgs_count / 2, imgs_count, 10, 15);
143 
144     DUMP(result, expected_path + ".res.png");
145 
146     ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));
147 }
148 
TEST(Photo_White,issue_2646)149 TEST(Photo_White, issue_2646)
150 {
151     cv::Mat img(50, 50, CV_8UC1, cv::Scalar::all(255));
152     cv::Mat filtered;
153     cv::fastNlMeansDenoising(img, filtered);
154 
155     int nonWhitePixelsCount = (int)img.total() - cv::countNonZero(filtered == img);
156 
157     ASSERT_EQ(0, nonWhitePixelsCount);
158 }
159 
TEST(Photo_Denoising,speed)160 TEST(Photo_Denoising, speed)
161 {
162     string imgname = string(cvtest::TS::ptr()->get_data_path()) + "shared/5MP.png";
163     Mat src = imread(imgname, 0), dst;
164 
165     double t = (double)getTickCount();
166     fastNlMeansDenoising(src, dst, 5, 7, 21);
167     t = (double)getTickCount() - t;
168     printf("execution time: %gms\n", t*1000./getTickFrequency());
169 }
170