From 7ff531b8ab57df6a63a2524f3a911663a3eb5457 Mon Sep 17 00:00:00 2001 From: Rostislav Vasilikhin Date: Sat, 15 Jun 2024 11:44:36 +0200 Subject: [PATCH] Merge pull request #25759 from savuor:rv/equalizeHist_tests Accuracy tests for equalizeHist() added #25759 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- modules/imgproc/test/test_histograms.cpp | 69 ++++++++++++++++++++++++ 1 file changed, 69 insertions(+) diff --git a/modules/imgproc/test/test_histograms.cpp b/modules/imgproc/test/test_histograms.cpp index efd045d31b..584b7087c3 100644 --- a/modules/imgproc/test/test_histograms.cpp +++ b/modules/imgproc/test/test_histograms.cpp @@ -2026,5 +2026,74 @@ TEST(Imgproc_Hist_Calc, IPP_ranges_with_nonequal_exponent_21595) ASSERT_EQ(histogram_u.at(2), 4.f) << "1 not counts correctly, res: " << histogram_u.at(2); } +////////////////////////////////////////// equalizeHist() ///////////////////////////////////////// + +void equalizeHistReference(const Mat& src, Mat& dst) +{ + std::vector hist(256, 0); + for (int y = 0; y < src.rows; y++) + { + const uchar* srow = src.ptr(y); + for (int x = 0; x < src.cols; x++) + { + hist[srow[x]]++; + } + } + + int first = 0; + while (!hist[first]) ++first; + + int total = (int)src.total(); + if (hist[first] == total) + { + dst.setTo(first); + return; + } + + std::vector lut(256); + lut[first] = 0; + float scale = (255.f)/(total - hist[first]); + + int sum = 0; + for (int i = first + 1; i < 256; ++i) + { + sum += hist[i]; + lut[i] = saturate_cast(sum * scale); + } + + cv::LUT(src, lut, dst); +} + +typedef ::testing::TestWithParam> Imgproc_Equalize_Hist; + +TEST_P(Imgproc_Equalize_Hist, accuracy) +{ + auto p = GetParam(); + cv::Size size = std::get<0>(p); + int idx = std::get<1>(p); + + RNG &rng = cvtest::TS::ptr()->get_rng(); + rng.state += idx; + + cv::Mat src(size, CV_8U); + cvtest::randUni(rng, src, Scalar::all(0), Scalar::all(255)); + + cv::Mat dst, gold; + + equalizeHistReference(src, gold); + + cv::equalizeHist(src, dst); + + ASSERT_EQ(CV_8UC1, dst.type()); + ASSERT_EQ(gold.size(), dst.size()); + + int nz = cv::countNonZero(dst != gold); + ASSERT_EQ(nz, 0); +} + +INSTANTIATE_TEST_CASE_P(Imgproc_Hist, Imgproc_Equalize_Hist, ::testing::Combine( + ::testing::Values(cv::Size(123, 321), cv::Size(256, 256), cv::Size(1024, 768)), + ::testing::Range(0, 10))); + }} // namespace /* End Of File */