Path: blob/master/modules/photo/test/test_denoising.cpp
16337 views
/*M///////////////////////////////////////////////////////////////////////////////////////1//2// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.3//4// By downloading, copying, installing or using the software you agree to this license.5// If you do not agree to this license, do not download, install,6// copy or use the software.7//8//9// License Agreement10// For Open Source Computer Vision Library11//12// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.13// Copyright (C) 2009, Willow Garage Inc., 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 documentation24// and/or other materials provided with the distribution.25//26// * The name of the copyright holders may not be used to endorse or promote products27// derived from this software without specific prior written permission.28//29// This software is provided by the copyright holders and contributors "as is" and30// any express or implied warranties, including, but not limited to, the implied31// warranties of merchantability and fitness for a particular purpose are disclaimed.32// In no event shall the Intel Corporation or contributors be liable for any direct,33// indirect, incidental, special, exemplary, or consequential damages34// (including, but not limited to, procurement of substitute goods or services;35// loss of use, data, or profits; or business interruption) however caused36// and on any theory of liability, whether in contract, strict liability,37// or tort (including negligence or otherwise) arising in any way out of38// the use of this software, even if advised of the possibility of such damage.39//40//M*/4142#include "test_precomp.hpp"4344namespace opencv_test { namespace {4546//#define DUMP_RESULTS4748#ifdef DUMP_RESULTS49# define DUMP(image, path) imwrite(path, image)50#else51# define DUMP(image, path)52#endif535455TEST(Photo_DenoisingGrayscale, regression)56{57string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";58string original_path = folder + "lena_noised_gaussian_sigma=10.png";59string expected_path = folder + "lena_noised_denoised_grayscale_tw=7_sw=21_h=10.png";6061Mat original = imread(original_path, IMREAD_GRAYSCALE);62Mat expected = imread(expected_path, IMREAD_GRAYSCALE);6364ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;65ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;6667Mat result;68fastNlMeansDenoising(original, result, 10);6970DUMP(result, expected_path + ".res.png");7172ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));73}7475TEST(Photo_DenoisingColored, regression)76{77string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";78string original_path = folder + "lena_noised_gaussian_sigma=10.png";79string expected_path = folder + "lena_noised_denoised_lab12_tw=7_sw=21_h=10_h2=10.png";8081Mat original = imread(original_path, IMREAD_COLOR);82Mat expected = imread(expected_path, IMREAD_COLOR);8384ASSERT_FALSE(original.empty()) << "Could not load input image " << original_path;85ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;8687Mat result;88fastNlMeansDenoisingColored(original, result, 10, 10);8990DUMP(result, expected_path + ".res.png");9192ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));93}9495TEST(Photo_DenoisingGrayscaleMulti, regression)96{97const int imgs_count = 3;98string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";99100string expected_path = folder + "lena_noised_denoised_multi_tw=7_sw=21_h=15.png";101Mat expected = imread(expected_path, IMREAD_GRAYSCALE);102ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;103104vector<Mat> original(imgs_count);105for (int i = 0; i < imgs_count; i++)106{107string original_path = format("%slena_noised_gaussian_sigma=20_multi_%d.png", folder.c_str(), i);108original[i] = imread(original_path, IMREAD_GRAYSCALE);109ASSERT_FALSE(original[i].empty()) << "Could not load input image " << original_path;110}111112Mat result;113fastNlMeansDenoisingMulti(original, result, imgs_count / 2, imgs_count, 15);114115DUMP(result, expected_path + ".res.png");116117ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));118}119120TEST(Photo_DenoisingColoredMulti, regression)121{122const int imgs_count = 3;123string folder = string(cvtest::TS::ptr()->get_data_path()) + "denoising/";124125string expected_path = folder + "lena_noised_denoised_multi_lab12_tw=7_sw=21_h=10_h2=15.png";126Mat expected = imread(expected_path, IMREAD_COLOR);127ASSERT_FALSE(expected.empty()) << "Could not load reference image " << expected_path;128129vector<Mat> original(imgs_count);130for (int i = 0; i < imgs_count; i++)131{132string original_path = format("%slena_noised_gaussian_sigma=20_multi_%d.png", folder.c_str(), i);133original[i] = imread(original_path, IMREAD_COLOR);134ASSERT_FALSE(original[i].empty()) << "Could not load input image " << original_path;135}136137Mat result;138fastNlMeansDenoisingColoredMulti(original, result, imgs_count / 2, imgs_count, 10, 15);139140DUMP(result, expected_path + ".res.png");141142ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2));143}144145TEST(Photo_White, issue_2646)146{147cv::Mat img(50, 50, CV_8UC1, cv::Scalar::all(255));148cv::Mat filtered;149cv::fastNlMeansDenoising(img, filtered);150151int nonWhitePixelsCount = (int)img.total() - cv::countNonZero(filtered == img);152153ASSERT_EQ(0, nonWhitePixelsCount);154}155156TEST(Photo_Denoising, speed)157{158string imgname = string(cvtest::TS::ptr()->get_data_path()) + "shared/5MP.png";159Mat src = imread(imgname, 0), dst;160161double t = (double)getTickCount();162fastNlMeansDenoising(src, dst, 5, 7, 21);163t = (double)getTickCount() - t;164printf("execution time: %gms\n", t*1000./getTickFrequency());165}166167}} // namespace168169170