test_exposure_compensate.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "test_precomp.hpp"
  5. namespace opencv_test {
  6. namespace {
  7. double minPSNR(UMat src1, UMat src2)
  8. {
  9. std::vector<UMat> src1_channels, src2_channels;
  10. split(src1, src1_channels);
  11. split(src2, src2_channels);
  12. double psnr = cvtest::PSNR(src1_channels[0], src2_channels[0]);
  13. psnr = std::min(psnr, cvtest::PSNR(src1_channels[1], src2_channels[1]));
  14. return std::min(psnr, cvtest::PSNR(src1_channels[2], src2_channels[2]));
  15. }
  16. TEST(ExposureCompensate, SimilarityThreshold)
  17. {
  18. UMat source;
  19. imread(cvtest::TS::ptr()->get_data_path() + "stitching/s1.jpg").copyTo(source);
  20. UMat image1 = source.clone();
  21. UMat image2 = source.clone();
  22. // Add a big artifact
  23. image2(Rect(150, 150, 100, 100)).setTo(Scalar(0, 0, 255));
  24. UMat mask(image1.size(), CV_8U);
  25. mask.setTo(255);
  26. detail::BlocksChannelsCompensator compensator;
  27. compensator.setNrGainsFilteringIterations(0); // makes it more clear
  28. // Feed the compensator, image 1 and 2 are perfectly
  29. // identical, except for the red artifact in image 2
  30. // Apart from that artifact, there is no exposure to compensate
  31. compensator.setSimilarityThreshold(1);
  32. uchar xff = 255;
  33. compensator.feed(
  34. {{}, {}},
  35. {image1, image2},
  36. {{mask, xff}, {mask, xff}}
  37. );
  38. // Verify that the artifact in image 2 did create
  39. // an artifact in image1 during the exposure compensation
  40. UMat image1_result = image1.clone();
  41. compensator.apply(0, {}, image1_result, mask);
  42. double psnr_no_similarity_mask = minPSNR(image1, image1_result);
  43. EXPECT_LT(psnr_no_similarity_mask, 45);
  44. // Add a similarity threshold and verify that
  45. // the artifact in image1 is gone
  46. compensator.setSimilarityThreshold(0.1);
  47. compensator.feed(
  48. {{}, {}},
  49. {image1, image2},
  50. {{mask, xff}, {mask, xff}}
  51. );
  52. image1_result = image1.clone();
  53. compensator.apply(0, {}, image1_result, mask);
  54. double psnr_similarity_mask = minPSNR(image1, image1_result);
  55. EXPECT_GT(psnr_similarity_mask, 300);
  56. }
  57. } // namespace
  58. } // namespace opencv_test