From 4a63b3dd90a1f5e66c852f294aacc90ea0c5be1f Mon Sep 17 00:00:00 2001 From: Ilya Lavrenov Date: Wed, 9 Apr 2014 00:00:13 +0400 Subject: [PATCH] cv2cvtest part2 --- modules/highgui/test/test_ffmpeg.cpp | 2 +- modules/highgui/test/test_video_io.cpp | 12 +++---- modules/highgui/test/test_video_pos.cpp | 2 +- modules/legacy/test/test_stereomatching.cpp | 2 +- modules/optim/test/test_denoise_tvl1.cpp | 35 +++++++++++---------- modules/photo/test/test_denoising.cpp | 8 ++--- modules/photo/test/test_inpaint.cpp | 8 ++--- modules/stitching/test/test_blenders.cpp | 2 +- modules/ts/include/opencv2/ts.hpp | 1 + modules/ts/src/ts_func.cpp | 6 ++++ modules/video/test/test_estimaterigid.cpp | 10 +++--- 11 files changed, 49 insertions(+), 39 deletions(-) diff --git a/modules/highgui/test/test_ffmpeg.cpp b/modules/highgui/test/test_ffmpeg.cpp index f8491d1a69..61fc3d49a4 100644 --- a/modules/highgui/test/test_ffmpeg.cpp +++ b/modules/highgui/test/test_ffmpeg.cpp @@ -329,7 +329,7 @@ public: EXPECT_EQ(reference.depth(), actual.depth()); EXPECT_EQ(reference.channels(), actual.channels()); - double psnr = PSNR(actual, reference); + double psnr = cvtest::PSNR(actual, reference); if (psnr < eps) { #define SUM cvtest::TS::SUMMARY diff --git a/modules/highgui/test/test_video_io.cpp b/modules/highgui/test/test_video_io.cpp index cacfde0b3c..f380e0d26b 100644 --- a/modules/highgui/test/test_video_io.cpp +++ b/modules/highgui/test/test_video_io.cpp @@ -198,7 +198,7 @@ void CV_HighGuiTest::ImageTest(const string& dir) } const double thresDbell = 20; - double psnr = PSNR(loaded, image); + double psnr = cvtest::PSNR(loaded, image); if (psnr < thresDbell) { ts->printf(ts->LOG, "Reading image from file: too big difference (=%g) with fmt=%s\n", psnr, ext.c_str()); @@ -235,7 +235,7 @@ void CV_HighGuiTest::ImageTest(const string& dir) continue; } - psnr = PSNR(buf_loaded, image); + psnr = cvtest::PSNR(buf_loaded, image); if (psnr < thresDbell) { @@ -316,7 +316,7 @@ void CV_HighGuiTest::VideoTest(const string& dir, const cvtest::VideoFormat& fmt Mat img = frames[i]; Mat img1 = cv::cvarrToMat(ipl1); - double psnr = PSNR(img1, img); + double psnr = cvtest::PSNR(img1, img); if (psnr < thresDbell) { ts->printf(ts->LOG, "Too low frame %d psnr = %gdb\n", i, psnr); @@ -371,7 +371,7 @@ void CV_HighGuiTest::SpecificImageTest(const string& dir) } const double thresDbell = 20; - double psnr = PSNR(loaded, image); + double psnr = cvtest::PSNR(loaded, image); if (psnr < thresDbell) { ts->printf(ts->LOG, "Reading image from file: too big difference (=%g) with fmt=bmp\n", psnr); @@ -408,7 +408,7 @@ void CV_HighGuiTest::SpecificImageTest(const string& dir) continue; } - psnr = PSNR(buf_loaded, image); + psnr = cvtest::PSNR(buf_loaded, image); if (psnr < thresDbell) { @@ -521,7 +521,7 @@ void CV_HighGuiTest::SpecificVideoTest(const string& dir, const cvtest::VideoFor Mat img = images[i]; const double thresDbell = 40; - double psnr = PSNR(img, frame); + double psnr = cvtest::PSNR(img, frame); if (psnr > thresDbell) { diff --git a/modules/highgui/test/test_video_pos.cpp b/modules/highgui/test/test_video_pos.cpp index a502040efb..c8fe4050da 100644 --- a/modules/highgui/test/test_video_pos.cpp +++ b/modules/highgui/test/test_video_pos.cpp @@ -160,7 +160,7 @@ public: return; } - double err = PSNR(img, img0); + double err = cvtest::PSNR(img, img0); if( err < 20 ) { diff --git a/modules/legacy/test/test_stereomatching.cpp b/modules/legacy/test/test_stereomatching.cpp index 7262249844..95aa97bbe4 100644 --- a/modules/legacy/test/test_stereomatching.cpp +++ b/modules/legacy/test/test_stereomatching.cpp @@ -278,7 +278,7 @@ float dispRMS( const Mat& computedDisp, const Mat& groundTruthDisp, const Mat& m checkTypeAndSizeOfMask( mask, sz ); pointsCount = countNonZero(mask); } - return 1.f/sqrt((float)pointsCount) * (float)norm(computedDisp, groundTruthDisp, NORM_L2, mask); + return 1.f/sqrt((float)pointsCount) * (float)cvtest::norm(computedDisp, groundTruthDisp, NORM_L2, mask); } /* diff --git a/modules/optim/test/test_denoise_tvl1.cpp b/modules/optim/test/test_denoise_tvl1.cpp index 9334dc5c5b..76ec2cda3e 100644 --- a/modules/optim/test/test_denoise_tvl1.cpp +++ b/modules/optim/test/test_denoise_tvl1.cpp @@ -41,7 +41,8 @@ #include "test_precomp.hpp" #include "opencv2/highgui.hpp" -void make_noisy(const cv::Mat& img, cv::Mat& noisy, double sigma, double pepper_salt_ratio,cv::RNG& rng){ +void make_noisy(const cv::Mat& img, cv::Mat& noisy, double sigma, double pepper_salt_ratio,cv::RNG& rng) +{ noisy.create(img.size(), img.type()); cv::Mat noise(img.size(), img.type()), mask(img.size(), CV_8U); rng.fill(noise,cv::RNG::NORMAL,128.0,sigma); @@ -54,34 +55,36 @@ void make_noisy(const cv::Mat& img, cv::Mat& noisy, double sigma, double pepper_ noise.setTo(128, mask); cv::addWeighted(noisy, 1, noise, 1, -128, noisy); } -void make_spotty(cv::Mat& img,cv::RNG& rng, int r=3,int n=1000){ - for(int i=0;i(x,y),val,(val==image.at(x,y))?"true":"false"); return (image.at(x,y)==val); } -TEST(Optim_denoise_tvl1, regression_basic){ +TEST(Optim_denoise_tvl1, regression_basic) +{ cv::RNG rng(42); - cv::Mat img = cv::imread("lena.jpg", 0), noisy,res; - if(img.rows!=512 || img.cols!=512){ - printf("\tplease, put lena.jpg from samples/c in the current folder\n"); - printf("\tnow, the test will fail...\n"); - ASSERT_TRUE(false); - } + cv::Mat img = cv::imread(cvtest::TS::ptr()->get_data_path() + "shared/lena.png", 0), noisy, res; + + ASSERT_FALSE(img.empty()) << "Error: can't open 'lena.png'"; const int obs_num=5; - std::vector images(obs_num,cv::Mat()); - for(int i=0;i<(int)images.size();i++){ + std::vector images(obs_num, cv::Mat()); + for(int i=0;i<(int)images.size();i++) + { make_noisy(img,images[i], 20, 0.02,rng); //make_spotty(images[i],rng); } diff --git a/modules/photo/test/test_denoising.cpp b/modules/photo/test/test_denoising.cpp index ca4f63f222..9808e9cddc 100644 --- a/modules/photo/test/test_denoising.cpp +++ b/modules/photo/test/test_denoising.cpp @@ -73,7 +73,7 @@ TEST(Photo_DenoisingGrayscale, regression) DUMP(result, expected_path + ".res.png"); - ASSERT_EQ(0, norm(result != expected)); + ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2)); } TEST(Photo_DenoisingColored, regression) @@ -93,7 +93,7 @@ TEST(Photo_DenoisingColored, regression) DUMP(result, expected_path + ".res.png"); - ASSERT_EQ(0, norm(result != expected)); + ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2)); } TEST(Photo_DenoisingGrayscaleMulti, regression) @@ -118,7 +118,7 @@ TEST(Photo_DenoisingGrayscaleMulti, regression) DUMP(result, expected_path + ".res.png"); - ASSERT_EQ(0, norm(result != expected)); + ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2)); } TEST(Photo_DenoisingColoredMulti, regression) @@ -143,7 +143,7 @@ TEST(Photo_DenoisingColoredMulti, regression) DUMP(result, expected_path + ".res.png"); - ASSERT_EQ(0, norm(result != expected)); + ASSERT_EQ(0, cvtest::norm(result, expected, NORM_L2)); } TEST(Photo_White, issue_2646) diff --git a/modules/photo/test/test_inpaint.cpp b/modules/photo/test/test_inpaint.cpp index 3c341b27a0..8f031e8d38 100644 --- a/modules/photo/test/test_inpaint.cpp +++ b/modules/photo/test/test_inpaint.cpp @@ -91,8 +91,8 @@ void CV_InpaintTest::run( int ) absdiff( orig, res1, diff1 ); absdiff( orig, res2, diff2 ); - double n1 = norm(diff1.reshape(1), NORM_INF, inv_mask.reshape(1)); - double n2 = norm(diff2.reshape(1), NORM_INF, inv_mask.reshape(1)); + double n1 = cvtest::norm(diff1.reshape(1), NORM_INF, inv_mask.reshape(1)); + double n2 = cvtest::norm(diff2.reshape(1), NORM_INF, inv_mask.reshape(1)); if (n1 != 0 || n2 != 0) { @@ -103,8 +103,8 @@ void CV_InpaintTest::run( int ) absdiff( exp1, res1, diff1 ); absdiff( exp2, res2, diff2 ); - n1 = norm(diff1.reshape(1), NORM_INF, mask.reshape(1)); - n2 = norm(diff2.reshape(1), NORM_INF, mask.reshape(1)); + n1 = cvtest::norm(diff1.reshape(1), NORM_INF, mask.reshape(1)); + n2 = cvtest::norm(diff2.reshape(1), NORM_INF, mask.reshape(1)); const int jpeg_thres = 3; if (n1 > jpeg_thres || n2 > jpeg_thres) diff --git a/modules/stitching/test/test_blenders.cpp b/modules/stitching/test/test_blenders.cpp index 6702eabf0c..cb84482f21 100644 --- a/modules/stitching/test/test_blenders.cpp +++ b/modules/stitching/test/test_blenders.cpp @@ -73,6 +73,6 @@ TEST(MultiBandBlender, CanBlendTwoImages) Mat result; result_s.convertTo(result, CV_8U); Mat expected = imread(string(cvtest::TS::ptr()->get_data_path()) + "stitching/baboon_lena.png"); - double rmsErr = norm(expected, result, NORM_L2) / sqrt(double(expected.size().area())); + double rmsErr = cvtest::norm(expected, result, NORM_L2) / sqrt(double(expected.size().area())); ASSERT_LT(rmsErr, 1e-3); } diff --git a/modules/ts/include/opencv2/ts.hpp b/modules/ts/include/opencv2/ts.hpp index 457f00b3e2..8aeec65712 100644 --- a/modules/ts/include/opencv2/ts.hpp +++ b/modules/ts/include/opencv2/ts.hpp @@ -129,6 +129,7 @@ CV_EXPORTS void minMaxLoc(const Mat& src, double* minval, double* maxval, CV_EXPORTS double norm(InputArray src, int normType, InputArray mask=noArray()); CV_EXPORTS double norm(InputArray src1, InputArray src2, int normType, InputArray mask=noArray()); CV_EXPORTS Scalar mean(const Mat& src, const Mat& mask=Mat()); +CV_EXPORTS double PSNR(InputArray src1, InputArray src2); CV_EXPORTS bool cmpUlps(const Mat& data, const Mat& refdata, int expMaxDiff, double* realMaxDiff, vector* idx); diff --git a/modules/ts/src/ts_func.cpp b/modules/ts/src/ts_func.cpp index e3563caa4f..2042f5cf0c 100644 --- a/modules/ts/src/ts_func.cpp +++ b/modules/ts/src/ts_func.cpp @@ -1399,6 +1399,12 @@ double norm(InputArray _src1, InputArray _src2, int normType, InputArray _mask) return isRelative ? result / (cvtest::norm(src2, normType) + DBL_EPSILON) : result; } +double PSNR(InputArray _src1, InputArray _src2) +{ + CV_Assert( _src1.depth() == CV_8U ); + double diff = std::sqrt(cvtest::norm(_src1, _src2, NORM_L2SQR)/(_src1.total()*_src1.channels())); + return 20*log10(255./(diff+DBL_EPSILON)); +} template static double crossCorr_(const _Tp* src1, const _Tp* src2, size_t total) diff --git a/modules/video/test/test_estimaterigid.cpp b/modules/video/test/test_estimaterigid.cpp index 5259ce7ee2..50508b4abb 100644 --- a/modules/video/test/test_estimaterigid.cpp +++ b/modules/video/test/test_estimaterigid.cpp @@ -109,8 +109,8 @@ bool CV_RigidTransform_Test::testNPoints(int from) Mat aff_est = estimateRigidTransform(fpts, tpts, true); - double thres = 0.1*norm(aff); - double d = norm(aff_est, aff, NORM_L2); + double thres = 0.1*cvtest::norm(aff, NORM_L2); + double d = cvtest::norm(aff_est, aff, NORM_L2); if (d > thres) { double dB=0, nB=0; @@ -120,7 +120,7 @@ bool CV_RigidTransform_Test::testNPoints(int from) Mat B = A - repeat(A.row(0), 3, 1), Bt = B.t(); B = Bt*B; dB = cv::determinant(B); - nB = norm(B); + nB = cvtest::norm(B, NORM_L2); if( fabs(dB) < 0.01*nB ) continue; } @@ -154,11 +154,11 @@ bool CV_RigidTransform_Test::testImage() Mat aff_est = estimateRigidTransform(img, rotated, true); const double thres = 0.033; - if (norm(aff_est, aff, NORM_INF) > thres) + if (cvtest::norm(aff_est, aff, NORM_INF) > thres) { ts->set_failed_test_info(cvtest::TS::FAIL_BAD_ACCURACY); ts->printf( cvtest::TS::LOG, "Threshold = %f, norm of difference = %f", thres, - norm(aff_est, aff, NORM_INF) ); + cvtest::norm(aff_est, aff, NORM_INF) ); return false; } -- GitLab