Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/photo/test/test_denoise_tvl1.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) 2013, OpenCV Foundation, all rights reserved.
14
// Third party copyrights are property of their respective owners.
15
//
16
// Redistribution and use in source and binary forms, with or without modification,
17
// are permitted provided that the following conditions are met:
18
//
19
// * Redistribution's of source code must retain the above copyright notice,
20
// this list of conditions and the following disclaimer.
21
//
22
// * Redistribution's in binary form must reproduce the above copyright notice,
23
// this list of conditions and the following disclaimer in the documentation
24
// and/or other materials provided with the distribution.
25
//
26
// * The name of the copyright holders may not be used to endorse or promote products
27
// derived from this software without specific prior written permission.
28
//
29
// This software is provided by the copyright holders and contributors "as is" and
30
// any express or implied warranties, including, but not limited to, the implied
31
// warranties of merchantability and fitness for a particular purpose are disclaimed.
32
// In no event shall the OpenCV Foundation or contributors be liable for any direct,
33
// indirect, incidental, special, exemplary, or consequential damages
34
// (including, but not limited to, procurement of substitute goods or services;
35
// loss of use, data, or profits; or business interruption) however caused
36
// and on any theory of liability, whether in contract, strict liability,
37
// or tort (including negligence or otherwise) arising in any way out of
38
// the use of this software, even if advised of the possibility of such damage.
39
//
40
//M*/
41
#include "test_precomp.hpp"
42
43
namespace opencv_test { namespace {
44
45
void make_noisy(const cv::Mat& img, cv::Mat& noisy, double sigma, double pepper_salt_ratio,cv::RNG& rng)
46
{
47
noisy.create(img.size(), img.type());
48
cv::Mat noise(img.size(), img.type()), mask(img.size(), CV_8U);
49
rng.fill(noise,cv::RNG::NORMAL,128.0,sigma);
50
cv::addWeighted(img, 1, noise, 1, -128, noisy);
51
cv::randn(noise, cv::Scalar::all(0), cv::Scalar::all(2));
52
noise *= 255;
53
cv::randu(mask, 0, cvRound(1./pepper_salt_ratio));
54
cv::Mat half = mask.colRange(0, img.cols/2);
55
half = cv::Scalar::all(1);
56
noise.setTo(128, mask);
57
cv::addWeighted(noisy, 1, noise, 1, -128, noisy);
58
}
59
60
#if 0
61
void make_spotty(cv::Mat& img,cv::RNG& rng, int r=3,int n=1000)
62
{
63
for(int i=0;i<n;i++)
64
{
65
int x=rng(img.cols-r),y=rng(img.rows-r);
66
if(rng(2)==0)
67
img(cv::Range(y,y+r),cv::Range(x,x+r))=(uchar)0;
68
else
69
img(cv::Range(y,y+r),cv::Range(x,x+r))=(uchar)255;
70
}
71
}
72
#endif
73
74
bool validate_pixel(const cv::Mat& image,int x,int y,uchar val)
75
{
76
bool ok = std::abs(image.at<uchar>(x,y) - val) < 10;
77
printf("test: image(%d,%d)=%d vs %d - %s\n",x,y,(int)image.at<uchar>(x,y),val,ok?"ok":"bad");
78
return ok;
79
}
80
81
TEST(Optim_denoise_tvl1, regression_basic)
82
{
83
cv::RNG rng(42);
84
cv::Mat img = cv::imread(cvtest::TS::ptr()->get_data_path() + "shared/lena.png", 0), noisy, res;
85
86
ASSERT_FALSE(img.empty()) << "Error: can't open 'lena.png'";
87
88
const int obs_num=5;
89
std::vector<cv::Mat> images(obs_num, cv::Mat());
90
for(int i=0;i<(int)images.size();i++)
91
{
92
make_noisy(img,images[i], 20, 0.02,rng);
93
//make_spotty(images[i],rng);
94
}
95
96
//cv::imshow("test", images[0]);
97
cv::denoise_TVL1(images, res);
98
//cv::imshow("denoised", res);
99
//cv::waitKey();
100
101
#if 0
102
ASSERT_TRUE(validate_pixel(res,248,334,179));
103
ASSERT_TRUE(validate_pixel(res,489,333,172));
104
ASSERT_TRUE(validate_pixel(res,425,507,104));
105
ASSERT_TRUE(validate_pixel(res,489,486,105));
106
ASSERT_TRUE(validate_pixel(res,223,208,64));
107
ASSERT_TRUE(validate_pixel(res,418,3,78));
108
ASSERT_TRUE(validate_pixel(res,63,76,97));
109
ASSERT_TRUE(validate_pixel(res,29,134,126));
110
ASSERT_TRUE(validate_pixel(res,219,291,174));
111
ASSERT_TRUE(validate_pixel(res,384,124,76));
112
#endif
113
114
#if 1
115
ASSERT_TRUE(validate_pixel(res,248,334,194));
116
ASSERT_TRUE(validate_pixel(res,489,333,171));
117
ASSERT_TRUE(validate_pixel(res,425,507,103));
118
ASSERT_TRUE(validate_pixel(res,489,486,109));
119
ASSERT_TRUE(validate_pixel(res,223,208,72));
120
ASSERT_TRUE(validate_pixel(res,418,3,58));
121
ASSERT_TRUE(validate_pixel(res,63,76,93));
122
ASSERT_TRUE(validate_pixel(res,29,134,127));
123
ASSERT_TRUE(validate_pixel(res,219,291,180));
124
ASSERT_TRUE(validate_pixel(res,384,124,80));
125
#endif
126
127
}
128
129
}} // namespace
130
131