test_jpeg.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 { namespace {
  6. #ifdef HAVE_JPEG
  7. /**
  8. * Test for check whether reading exif orientation tag was processed successfully or not
  9. * The test info is the set of 8 images named testExifRotate_{1 to 8}.jpg
  10. * The test image is the square 10x10 points divided by four sub-squares:
  11. * (R corresponds to Red, G to Green, B to Blue, W to white)
  12. * --------- ---------
  13. * | R | G | | G | R |
  14. * |-------| - (tag 1) |-------| - (tag 2)
  15. * | B | W | | W | B |
  16. * --------- ---------
  17. *
  18. * --------- ---------
  19. * | W | B | | B | W |
  20. * |-------| - (tag 3) |-------| - (tag 4)
  21. * | G | R | | R | G |
  22. * --------- ---------
  23. *
  24. * --------- ---------
  25. * | R | B | | G | W |
  26. * |-------| - (tag 5) |-------| - (tag 6)
  27. * | G | W | | R | B |
  28. * --------- ---------
  29. *
  30. * --------- ---------
  31. * | W | G | | B | R |
  32. * |-------| - (tag 7) |-------| - (tag 8)
  33. * | B | R | | W | G |
  34. * --------- ---------
  35. *
  36. *
  37. * Every image contains exif field with orientation tag (0x112)
  38. * After reading each image the corresponding matrix must be read as
  39. * ---------
  40. * | R | G |
  41. * |-------|
  42. * | B | W |
  43. * ---------
  44. *
  45. */
  46. typedef testing::TestWithParam<string> Imgcodecs_Jpeg_Exif;
  47. TEST_P(Imgcodecs_Jpeg_Exif, exif_orientation)
  48. {
  49. const string root = cvtest::TS::ptr()->get_data_path();
  50. const string filename = root + GetParam();
  51. const int colorThresholdHigh = 250;
  52. const int colorThresholdLow = 5;
  53. Mat m_img = imread(filename);
  54. ASSERT_FALSE(m_img.empty());
  55. Vec3b vec;
  56. //Checking the first quadrant (with supposed red)
  57. vec = m_img.at<Vec3b>(2, 2); //some point inside the square
  58. EXPECT_LE(vec.val[0], colorThresholdLow);
  59. EXPECT_LE(vec.val[1], colorThresholdLow);
  60. EXPECT_GE(vec.val[2], colorThresholdHigh);
  61. //Checking the second quadrant (with supposed green)
  62. vec = m_img.at<Vec3b>(2, 7); //some point inside the square
  63. EXPECT_LE(vec.val[0], colorThresholdLow);
  64. EXPECT_GE(vec.val[1], colorThresholdHigh);
  65. EXPECT_LE(vec.val[2], colorThresholdLow);
  66. //Checking the third quadrant (with supposed blue)
  67. vec = m_img.at<Vec3b>(7, 2); //some point inside the square
  68. EXPECT_GE(vec.val[0], colorThresholdHigh);
  69. EXPECT_LE(vec.val[1], colorThresholdLow);
  70. EXPECT_LE(vec.val[2], colorThresholdLow);
  71. }
  72. const string exif_files[] =
  73. {
  74. "readwrite/testExifOrientation_1.jpg",
  75. "readwrite/testExifOrientation_2.jpg",
  76. "readwrite/testExifOrientation_3.jpg",
  77. "readwrite/testExifOrientation_4.jpg",
  78. "readwrite/testExifOrientation_5.jpg",
  79. "readwrite/testExifOrientation_6.jpg",
  80. "readwrite/testExifOrientation_7.jpg",
  81. "readwrite/testExifOrientation_8.jpg"
  82. };
  83. INSTANTIATE_TEST_CASE_P(ExifFiles, Imgcodecs_Jpeg_Exif,
  84. testing::ValuesIn(exif_files));
  85. //==================================================================================================
  86. TEST(Imgcodecs_Jpeg, encode_empty)
  87. {
  88. cv::Mat img;
  89. std::vector<uchar> jpegImg;
  90. ASSERT_THROW(cv::imencode(".jpg", img, jpegImg), cv::Exception);
  91. }
  92. TEST(Imgcodecs_Jpeg, encode_decode_progressive_jpeg)
  93. {
  94. cvtest::TS& ts = *cvtest::TS::ptr();
  95. string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
  96. cv::Mat img = cv::imread(input);
  97. ASSERT_FALSE(img.empty());
  98. std::vector<int> params;
  99. params.push_back(IMWRITE_JPEG_PROGRESSIVE);
  100. params.push_back(1);
  101. string output_progressive = cv::tempfile(".jpg");
  102. EXPECT_NO_THROW(cv::imwrite(output_progressive, img, params));
  103. cv::Mat img_jpg_progressive = cv::imread(output_progressive);
  104. string output_normal = cv::tempfile(".jpg");
  105. EXPECT_NO_THROW(cv::imwrite(output_normal, img));
  106. cv::Mat img_jpg_normal = cv::imread(output_normal);
  107. EXPECT_EQ(0, cvtest::norm(img_jpg_progressive, img_jpg_normal, NORM_INF));
  108. EXPECT_EQ(0, remove(output_progressive.c_str()));
  109. EXPECT_EQ(0, remove(output_normal.c_str()));
  110. }
  111. TEST(Imgcodecs_Jpeg, encode_decode_optimize_jpeg)
  112. {
  113. cvtest::TS& ts = *cvtest::TS::ptr();
  114. string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
  115. cv::Mat img = cv::imread(input);
  116. ASSERT_FALSE(img.empty());
  117. std::vector<int> params;
  118. params.push_back(IMWRITE_JPEG_OPTIMIZE);
  119. params.push_back(1);
  120. string output_optimized = cv::tempfile(".jpg");
  121. EXPECT_NO_THROW(cv::imwrite(output_optimized, img, params));
  122. cv::Mat img_jpg_optimized = cv::imread(output_optimized);
  123. string output_normal = cv::tempfile(".jpg");
  124. EXPECT_NO_THROW(cv::imwrite(output_normal, img));
  125. cv::Mat img_jpg_normal = cv::imread(output_normal);
  126. EXPECT_EQ(0, cvtest::norm(img_jpg_optimized, img_jpg_normal, NORM_INF));
  127. EXPECT_EQ(0, remove(output_optimized.c_str()));
  128. EXPECT_EQ(0, remove(output_normal.c_str()));
  129. }
  130. TEST(Imgcodecs_Jpeg, encode_decode_rst_jpeg)
  131. {
  132. cvtest::TS& ts = *cvtest::TS::ptr();
  133. string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
  134. cv::Mat img = cv::imread(input);
  135. ASSERT_FALSE(img.empty());
  136. std::vector<int> params;
  137. params.push_back(IMWRITE_JPEG_RST_INTERVAL);
  138. params.push_back(1);
  139. string output_rst = cv::tempfile(".jpg");
  140. EXPECT_NO_THROW(cv::imwrite(output_rst, img, params));
  141. cv::Mat img_jpg_rst = cv::imread(output_rst);
  142. string output_normal = cv::tempfile(".jpg");
  143. EXPECT_NO_THROW(cv::imwrite(output_normal, img));
  144. cv::Mat img_jpg_normal = cv::imread(output_normal);
  145. EXPECT_EQ(0, cvtest::norm(img_jpg_rst, img_jpg_normal, NORM_INF));
  146. EXPECT_EQ(0, remove(output_rst.c_str()));
  147. EXPECT_EQ(0, remove(output_normal.c_str()));
  148. }
  149. #endif // HAVE_JPEG
  150. }} // namespace