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
make_noisy(const cv::Mat & img,cv::Mat & noisy,double sigma,double pepper_salt_ratio,cv::RNG & rng)43 void make_noisy(const cv::Mat& img, cv::Mat& noisy, double sigma, double pepper_salt_ratio,cv::RNG& rng)
44 {
45 noisy.create(img.size(), img.type());
46 cv::Mat noise(img.size(), img.type()), mask(img.size(), CV_8U);
47 rng.fill(noise,cv::RNG::NORMAL,128.0,sigma);
48 cv::addWeighted(img, 1, noise, 1, -128, noisy);
49 cv::randn(noise, cv::Scalar::all(0), cv::Scalar::all(2));
50 noise *= 255;
51 cv::randu(mask, 0, cvRound(1./pepper_salt_ratio));
52 cv::Mat half = mask.colRange(0, img.cols/2);
53 half = cv::Scalar::all(1);
54 noise.setTo(128, mask);
55 cv::addWeighted(noisy, 1, noise, 1, -128, noisy);
56 }
57
make_spotty(cv::Mat & img,cv::RNG & rng,int r=3,int n=1000)58 void make_spotty(cv::Mat& img,cv::RNG& rng, int r=3,int n=1000)
59 {
60 for(int i=0;i<n;i++)
61 {
62 int x=rng(img.cols-r),y=rng(img.rows-r);
63 if(rng(2)==0)
64 img(cv::Range(y,y+r),cv::Range(x,x+r))=(uchar)0;
65 else
66 img(cv::Range(y,y+r),cv::Range(x,x+r))=(uchar)255;
67 }
68 }
69
validate_pixel(const cv::Mat & image,int x,int y,uchar val)70 bool validate_pixel(const cv::Mat& image,int x,int y,uchar val)
71 {
72 bool ok = std::abs(image.at<uchar>(x,y) - val) < 10;
73 printf("test: image(%d,%d)=%d vs %d - %s\n",x,y,(int)image.at<uchar>(x,y),val,ok?"ok":"bad");
74 return ok;
75 }
76
TEST(Optim_denoise_tvl1,regression_basic)77 TEST(Optim_denoise_tvl1, regression_basic)
78 {
79 cv::RNG rng(42);
80 cv::Mat img = cv::imread(cvtest::TS::ptr()->get_data_path() + "shared/lena.png", 0), noisy, res;
81
82 ASSERT_FALSE(img.empty()) << "Error: can't open 'lena.png'";
83
84 const int obs_num=5;
85 std::vector<cv::Mat> images(obs_num, cv::Mat());
86 for(int i=0;i<(int)images.size();i++)
87 {
88 make_noisy(img,images[i], 20, 0.02,rng);
89 //make_spotty(images[i],rng);
90 }
91
92 //cv::imshow("test", images[0]);
93 cv::denoise_TVL1(images, res);
94 //cv::imshow("denoised", res);
95 //cv::waitKey();
96
97 #if 0
98 ASSERT_TRUE(validate_pixel(res,248,334,179));
99 ASSERT_TRUE(validate_pixel(res,489,333,172));
100 ASSERT_TRUE(validate_pixel(res,425,507,104));
101 ASSERT_TRUE(validate_pixel(res,489,486,105));
102 ASSERT_TRUE(validate_pixel(res,223,208,64));
103 ASSERT_TRUE(validate_pixel(res,418,3,78));
104 ASSERT_TRUE(validate_pixel(res,63,76,97));
105 ASSERT_TRUE(validate_pixel(res,29,134,126));
106 ASSERT_TRUE(validate_pixel(res,219,291,174));
107 ASSERT_TRUE(validate_pixel(res,384,124,76));
108 #endif
109
110 #if 1
111 ASSERT_TRUE(validate_pixel(res,248,334,194));
112 ASSERT_TRUE(validate_pixel(res,489,333,171));
113 ASSERT_TRUE(validate_pixel(res,425,507,103));
114 ASSERT_TRUE(validate_pixel(res,489,486,109));
115 ASSERT_TRUE(validate_pixel(res,223,208,72));
116 ASSERT_TRUE(validate_pixel(res,418,3,58));
117 ASSERT_TRUE(validate_pixel(res,63,76,93));
118 ASSERT_TRUE(validate_pixel(res,29,134,127));
119 ASSERT_TRUE(validate_pixel(res,219,291,180));
120 ASSERT_TRUE(validate_pixel(res,384,124,80));
121 #endif
122
123 }
124