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
This commit is contained in:
Rostislav Vasilikhin 2024-06-15 11:44:36 +02:00 committed by GitHub
parent a03b813167
commit 7ff531b8ab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -2026,5 +2026,74 @@ TEST(Imgproc_Hist_Calc, IPP_ranges_with_nonequal_exponent_21595)
ASSERT_EQ(histogram_u.at<float>(2), 4.f) << "1 not counts correctly, res: " << histogram_u.at<float>(2);
}
////////////////////////////////////////// equalizeHist() /////////////////////////////////////////
void equalizeHistReference(const Mat& src, Mat& dst)
{
std::vector<int> 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<uchar> 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<uchar>(sum * scale);
}
cv::LUT(src, lut, dst);
}
typedef ::testing::TestWithParam<std::tuple<cv::Size, int>> 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 */