test_exr.impl.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. //#define GENERATE_DATA
  5. namespace opencv_test { namespace {
  6. size_t getFileSize(const string& filename)
  7. {
  8. std::ifstream ifs(filename.c_str(), std::ios::in | std::ios::binary);
  9. if (ifs.is_open())
  10. {
  11. ifs.seekg(0, std::ios::end);
  12. return (size_t)ifs.tellg();
  13. }
  14. return 0;
  15. }
  16. TEST(Imgcodecs_EXR, readWrite_32FC1)
  17. { // Y channels
  18. const string root = cvtest::TS::ptr()->get_data_path();
  19. const string filenameInput = root + "readwrite/test32FC1.exr";
  20. const string filenameOutput = cv::tempfile(".exr");
  21. #ifndef GENERATE_DATA
  22. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  23. #else
  24. const Size sz(64, 32);
  25. Mat img(sz, CV_32FC1, Scalar(0.5, 0.1, 1));
  26. img(Rect(10, 5, sz.width - 30, sz.height - 20)).setTo(Scalar(1, 0, 0));
  27. ASSERT_TRUE(cv::imwrite(filenameInput, img));
  28. #endif
  29. ASSERT_FALSE(img.empty());
  30. ASSERT_EQ(CV_32FC1,img.type());
  31. ASSERT_TRUE(cv::imwrite(filenameOutput, img));
  32. // Check generated file size to ensure that it's compressed with proper options
  33. ASSERT_EQ(396u, getFileSize(filenameOutput));
  34. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  35. ASSERT_EQ(img2.type(), img.type());
  36. ASSERT_EQ(img2.size(), img.size());
  37. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  38. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  39. }
  40. TEST(Imgcodecs_EXR, readWrite_32FC3)
  41. { // RGB channels
  42. const string root = cvtest::TS::ptr()->get_data_path();
  43. const string filenameInput = root + "readwrite/test32FC3.exr";
  44. const string filenameOutput = cv::tempfile(".exr");
  45. #ifndef GENERATE_DATA
  46. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  47. #else
  48. const Size sz(64, 32);
  49. Mat img(sz, CV_32FC3, Scalar(0.5, 0.1, 1));
  50. img(Rect(10, 5, sz.width - 30, sz.height - 20)).setTo(Scalar(1, 0, 0));
  51. ASSERT_TRUE(cv::imwrite(filenameInput, img));
  52. #endif
  53. ASSERT_FALSE(img.empty());
  54. ASSERT_EQ(CV_32FC3, img.type());
  55. ASSERT_TRUE(cv::imwrite(filenameOutput, img));
  56. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  57. ASSERT_EQ(img2.type(), img.type());
  58. ASSERT_EQ(img2.size(), img.size());
  59. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  60. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  61. }
  62. TEST(Imgcodecs_EXR, readWrite_32FC1_half)
  63. {
  64. const string root = cvtest::TS::ptr()->get_data_path();
  65. const string filenameInput = root + "readwrite/test32FC1_half.exr";
  66. const string filenameOutput = cv::tempfile(".exr");
  67. std::vector<int> params;
  68. params.push_back(IMWRITE_EXR_TYPE);
  69. params.push_back(IMWRITE_EXR_TYPE_HALF);
  70. #ifndef GENERATE_DATA
  71. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  72. #else
  73. const Size sz(64, 32);
  74. Mat img(sz, CV_32FC1, Scalar(0.5, 0.1, 1));
  75. img(Rect(10, 5, sz.width - 30, sz.height - 20)).setTo(Scalar(1, 0, 0));
  76. ASSERT_TRUE(cv::imwrite(filenameInput, img, params));
  77. #endif
  78. ASSERT_FALSE(img.empty());
  79. ASSERT_EQ(CV_32FC1,img.type());
  80. ASSERT_TRUE(cv::imwrite(filenameOutput, img, params));
  81. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  82. ASSERT_EQ(img2.type(), img.type());
  83. ASSERT_EQ(img2.size(), img.size());
  84. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  85. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  86. }
  87. TEST(Imgcodecs_EXR, readWrite_32FC3_half)
  88. {
  89. const string root = cvtest::TS::ptr()->get_data_path();
  90. const string filenameInput = root + "readwrite/test32FC3_half.exr";
  91. const string filenameOutput = cv::tempfile(".exr");
  92. std::vector<int> params;
  93. params.push_back(IMWRITE_EXR_TYPE);
  94. params.push_back(IMWRITE_EXR_TYPE_HALF);
  95. #ifndef GENERATE_DATA
  96. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  97. #else
  98. const Size sz(64, 32);
  99. Mat img(sz, CV_32FC3, Scalar(0.5, 0.1, 1));
  100. img(Rect(10, 5, sz.width - 30, sz.height - 20)).setTo(Scalar(1, 0, 0));
  101. ASSERT_TRUE(cv::imwrite(filenameInput, img, params));
  102. #endif
  103. ASSERT_FALSE(img.empty());
  104. ASSERT_EQ(CV_32FC3, img.type());
  105. ASSERT_TRUE(cv::imwrite(filenameOutput, img, params));
  106. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  107. ASSERT_EQ(img2.type(), img.type());
  108. ASSERT_EQ(img2.size(), img.size());
  109. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  110. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  111. }
  112. TEST(Imgcodecs_EXR, readWrite_32FC1_PIZ)
  113. {
  114. const string root = cvtest::TS::ptr()->get_data_path();
  115. const string filenameInput = root + "readwrite/test32FC1.exr";
  116. const string filenameOutput = cv::tempfile(".exr");
  117. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  118. ASSERT_FALSE(img.empty());
  119. ASSERT_EQ(CV_32FC1, img.type());
  120. std::vector<int> params;
  121. params.push_back(IMWRITE_EXR_COMPRESSION);
  122. params.push_back(IMWRITE_EXR_COMPRESSION_PIZ);
  123. ASSERT_TRUE(cv::imwrite(filenameOutput, img, params));
  124. // Check generated file size to ensure that it's compressed with proper options
  125. ASSERT_EQ(849u, getFileSize(filenameOutput));
  126. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  127. ASSERT_EQ(img2.type(), img.type());
  128. ASSERT_EQ(img2.size(), img.size());
  129. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  130. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  131. }
  132. // Note: YC to GRAYSCALE (IMREAD_GRAYSCALE | IMREAD_ANYDEPTH)
  133. // outputs a black image,
  134. // as does Y to RGB (IMREAD_COLOR | IMREAD_ANYDEPTH).
  135. // This behavoir predates adding EXR alpha support issue
  136. // 16115.
  137. TEST(Imgcodecs_EXR, read_YA_ignore_alpha)
  138. {
  139. const string root = cvtest::TS::ptr()->get_data_path();
  140. const string filenameInput = root + "readwrite/test_YA.exr";
  141. const Mat img = cv::imread(filenameInput, IMREAD_GRAYSCALE | IMREAD_ANYDEPTH);
  142. ASSERT_FALSE(img.empty());
  143. ASSERT_EQ(CV_32FC1, img.type());
  144. // Writing Y covered by test 32FC1
  145. }
  146. TEST(Imgcodecs_EXR, read_YA_unchanged)
  147. {
  148. const string root = cvtest::TS::ptr()->get_data_path();
  149. const string filenameInput = root + "readwrite/test_YA.exr";
  150. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  151. ASSERT_FALSE(img.empty());
  152. ASSERT_EQ(CV_32FC2, img.type());
  153. // Cannot test writing, 2 channel writing not suppported by loadsave
  154. }
  155. TEST(Imgcodecs_EXR, read_YC_changeDepth)
  156. {
  157. const string root = cvtest::TS::ptr()->get_data_path();
  158. const string filenameInput = root + "readwrite/test_YRYBY.exr";
  159. const Mat img = cv::imread(filenameInput, IMREAD_COLOR);
  160. ASSERT_FALSE(img.empty());
  161. ASSERT_EQ(CV_8UC3, img.type());
  162. // Cannot test writing, EXR encoder doesn't support 8U depth
  163. }
  164. TEST(Imgcodecs_EXR, readwrite_YCA_ignore_alpha)
  165. {
  166. const string root = cvtest::TS::ptr()->get_data_path();
  167. const string filenameInput = root + "readwrite/test_YRYBYA.exr";
  168. const string filenameOutput = cv::tempfile(".exr");
  169. const Mat img = cv::imread(filenameInput, IMREAD_COLOR | IMREAD_ANYDEPTH);
  170. ASSERT_FALSE(img.empty());
  171. ASSERT_EQ(CV_32FC3, img.type());
  172. ASSERT_TRUE(cv::imwrite(filenameOutput, img));
  173. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  174. ASSERT_EQ(img2.type(), img.type());
  175. ASSERT_EQ(img2.size(), img.size());
  176. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  177. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  178. }
  179. TEST(Imgcodecs_EXR, read_YC_unchanged)
  180. {
  181. const string root = cvtest::TS::ptr()->get_data_path();
  182. const string filenameInput = root + "readwrite/test_YRYBY.exr";
  183. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  184. ASSERT_FALSE(img.empty());
  185. ASSERT_EQ(CV_32FC3, img.type());
  186. // Writing YC covered by test readwrite_YCA_ignore_alpha
  187. }
  188. TEST(Imgcodecs_EXR, readwrite_YCA_unchanged)
  189. {
  190. const string root = cvtest::TS::ptr()->get_data_path();
  191. const string filenameInput = root + "readwrite/test_YRYBYA.exr";
  192. const string filenameOutput = cv::tempfile(".exr");
  193. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  194. ASSERT_FALSE(img.empty());
  195. ASSERT_EQ(CV_32FC4, img.type());
  196. ASSERT_TRUE(cv::imwrite(filenameOutput, img));
  197. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  198. ASSERT_EQ(img2.type(), img.type());
  199. ASSERT_EQ(img2.size(), img.size());
  200. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  201. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  202. }
  203. TEST(Imgcodecs_EXR, readwrite_RGBA_togreyscale)
  204. {
  205. const string root = cvtest::TS::ptr()->get_data_path();
  206. const string filenameInput = root + "readwrite/test_GeneratedRGBA.exr";
  207. const string filenameOutput = cv::tempfile(".exr");
  208. const Mat img = cv::imread(filenameInput, IMREAD_GRAYSCALE | IMREAD_ANYDEPTH);
  209. ASSERT_FALSE(img.empty());
  210. ASSERT_EQ(CV_32FC1, img.type());
  211. ASSERT_TRUE(cv::imwrite(filenameOutput, img));
  212. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  213. ASSERT_EQ(img2.type(), img.type());
  214. ASSERT_EQ(img2.size(), img.size());
  215. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  216. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  217. }
  218. TEST(Imgcodecs_EXR, read_RGBA_ignore_alpha)
  219. {
  220. const string root = cvtest::TS::ptr()->get_data_path();
  221. const string filenameInput = root + "readwrite/test_GeneratedRGBA.exr";
  222. const Mat img = cv::imread(filenameInput, IMREAD_COLOR | IMREAD_ANYDEPTH);
  223. ASSERT_FALSE(img.empty());
  224. ASSERT_EQ(CV_32FC3, img.type());
  225. // Writing RGB covered by test 32FC3
  226. }
  227. TEST(Imgcodecs_EXR, read_RGBA_unchanged)
  228. {
  229. const string root = cvtest::TS::ptr()->get_data_path();
  230. const string filenameInput = root + "readwrite/test_GeneratedRGBA.exr";
  231. const string filenameOutput = cv::tempfile(".exr");
  232. #ifndef GENERATE_DATA
  233. const Mat img = cv::imread(filenameInput, IMREAD_UNCHANGED);
  234. #else
  235. const Size sz(64, 32);
  236. Mat img(sz, CV_32FC4, Scalar(0.5, 0.1, 1, 1));
  237. img(Rect(10, 5, sz.width - 30, sz.height - 20)).setTo(Scalar(1, 0, 0, 1));
  238. img(Rect(10, 20, sz.width - 30, sz.height - 20)).setTo(Scalar(1, 1, 0, 0));
  239. ASSERT_TRUE(cv::imwrite(filenameInput, img));
  240. #endif
  241. ASSERT_FALSE(img.empty());
  242. ASSERT_EQ(CV_32FC4, img.type());
  243. ASSERT_TRUE(cv::imwrite(filenameOutput, img));
  244. const Mat img2 = cv::imread(filenameOutput, IMREAD_UNCHANGED);
  245. ASSERT_EQ(img2.type(), img.type());
  246. ASSERT_EQ(img2.size(), img.size());
  247. EXPECT_LE(cvtest::norm(img, img2, NORM_INF | NORM_RELATIVE), 1e-3);
  248. EXPECT_EQ(0, remove(filenameOutput.c_str()));
  249. }
  250. }} // namespace