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