Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgcodecs/test/test_grfmt.cpp
16354 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "test_precomp.hpp"
44
45
namespace opencv_test { namespace {
46
47
typedef tuple<string, int> File_Mode;
48
typedef testing::TestWithParam<File_Mode> Imgcodecs_FileMode;
49
50
TEST_P(Imgcodecs_FileMode, regression)
51
{
52
const string root = cvtest::TS::ptr()->get_data_path();
53
const string filename = root + get<0>(GetParam());
54
const int mode = get<1>(GetParam());
55
56
const Mat single = imread(filename, mode);
57
ASSERT_FALSE(single.empty());
58
59
vector<Mat> pages;
60
ASSERT_TRUE(imreadmulti(filename, pages, mode));
61
ASSERT_FALSE(pages.empty());
62
const Mat page = pages[0];
63
ASSERT_FALSE(page.empty());
64
65
EXPECT_EQ(page.channels(), single.channels());
66
EXPECT_EQ(page.depth(), single.depth());
67
EXPECT_EQ(page.size().height, single.size().height);
68
EXPECT_EQ(page.size().width, single.size().width);
69
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), page, single);
70
}
71
72
const string all_images[] =
73
{
74
#ifdef HAVE_JASPER
75
"readwrite/Rome.jp2",
76
"readwrite/Bretagne2.jp2",
77
"readwrite/Bretagne2.jp2",
78
"readwrite/Grey.jp2",
79
"readwrite/Grey.jp2",
80
#endif
81
#ifdef HAVE_GDCM
82
"readwrite/int16-mono1.dcm",
83
"readwrite/uint8-mono2.dcm",
84
"readwrite/uint16-mono2.dcm",
85
"readwrite/uint8-rgb.dcm",
86
#endif
87
"readwrite/color_palette_alpha.png",
88
"readwrite/multipage.tif",
89
"readwrite/ordinary.bmp",
90
"readwrite/rle8.bmp",
91
"readwrite/test_1_c1.jpg",
92
#ifdef HAVE_IMGCODEC_HDR
93
"readwrite/rle.hdr"
94
#endif
95
};
96
97
const int basic_modes[] =
98
{
99
IMREAD_UNCHANGED,
100
IMREAD_GRAYSCALE,
101
IMREAD_COLOR,
102
IMREAD_ANYDEPTH,
103
IMREAD_ANYCOLOR
104
};
105
106
INSTANTIATE_TEST_CASE_P(All, Imgcodecs_FileMode,
107
testing::Combine(
108
testing::ValuesIn(all_images),
109
testing::ValuesIn(basic_modes)));
110
111
// GDAL does not support "hdr", "dcm" and have problems with "jp2"
112
struct notForGDAL {
113
bool operator()(const string &name) const {
114
const string &ext = name.substr(name.size() - 3, 3);
115
return ext == "hdr" || ext == "dcm" || ext == "jp2" ||
116
name.find("rle8.bmp") != std::string::npos;
117
}
118
};
119
120
inline vector<string> gdal_images()
121
{
122
vector<string> res;
123
std::back_insert_iterator< vector<string> > it(res);
124
std::remove_copy_if(all_images, all_images + sizeof(all_images)/sizeof(all_images[0]), it, notForGDAL());
125
return res;
126
}
127
128
INSTANTIATE_TEST_CASE_P(GDAL, Imgcodecs_FileMode,
129
testing::Combine(
130
testing::ValuesIn(gdal_images()),
131
testing::Values(IMREAD_LOAD_GDAL)));
132
133
//==================================================================================================
134
135
typedef tuple<string, Size> Ext_Size;
136
typedef testing::TestWithParam<Ext_Size> Imgcodecs_ExtSize;
137
138
TEST_P(Imgcodecs_ExtSize, write_imageseq)
139
{
140
const string ext = get<0>(GetParam());
141
const Size size = get<1>(GetParam());
142
const Point2i center = Point2i(size.width / 2, size.height / 2);
143
const int radius = std::min(size.height, size.width / 4);
144
145
for (int cn = 1; cn <= 4; cn++)
146
{
147
SCOPED_TRACE(format("channels %d", cn));
148
std::vector<int> parameters;
149
if (cn == 2)
150
continue;
151
if (cn == 4 && ext != ".tiff")
152
continue;
153
if (cn > 1 && (ext == ".pbm" || ext == ".pgm"))
154
continue;
155
if (cn != 3 && ext == ".ppm")
156
continue;
157
string filename = cv::tempfile(format("%d%s", cn, ext.c_str()).c_str());
158
159
Mat img_gt(size, CV_MAKETYPE(CV_8U, cn), Scalar::all(0));
160
circle(img_gt, center, radius, Scalar::all(255));
161
162
#if 1
163
if (ext == ".pbm" || ext == ".pgm" || ext == ".ppm")
164
{
165
parameters.push_back(IMWRITE_PXM_BINARY);
166
parameters.push_back(0);
167
}
168
#endif
169
ASSERT_TRUE(imwrite(filename, img_gt, parameters));
170
Mat img = imread(filename, IMREAD_UNCHANGED);
171
ASSERT_FALSE(img.empty());
172
EXPECT_EQ(img.size(), img.size());
173
EXPECT_EQ(img.type(), img.type());
174
EXPECT_EQ(cn, img.channels());
175
176
177
if (ext == ".jpg")
178
{
179
// JPEG format does not provide 100% accuracy
180
// using fuzzy image comparison
181
double n = cvtest::norm(img, img_gt, NORM_L1);
182
double expected = 0.07 * img.size().area();
183
EXPECT_LT(n, expected);
184
EXPECT_PRED_FORMAT2(cvtest::MatComparator(10, 0), img, img_gt);
185
}
186
else if (ext == ".pfm")
187
{
188
img_gt.convertTo(img_gt, CV_MAKETYPE(CV_32F, img.channels()));
189
double n = cvtest::norm(img, img_gt, NORM_L2);
190
EXPECT_LT(n, 1.);
191
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);
192
}
193
else
194
{
195
double n = cvtest::norm(img, img_gt, NORM_L2);
196
EXPECT_LT(n, 1.);
197
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), img, img_gt);
198
}
199
200
#if 0
201
imshow("loaded", img);
202
waitKey(0);
203
#else
204
EXPECT_EQ(0, remove(filename.c_str()));
205
#endif
206
}
207
}
208
209
const string all_exts[] =
210
{
211
#ifdef HAVE_PNG
212
".png",
213
#endif
214
#ifdef HAVE_TIFF
215
".tiff",
216
#endif
217
#ifdef HAVE_JPEG
218
".jpg",
219
#endif
220
".bmp",
221
#ifdef HAVE_IMGCODEC_PXM
222
".pam",
223
".ppm",
224
".pgm",
225
".pbm",
226
".pnm",
227
#endif
228
#ifdef HAVE_IMGCODEC_PFM
229
".pfm",
230
#endif
231
};
232
233
vector<Size> all_sizes()
234
{
235
vector<Size> res;
236
for (int k = 1; k <= 5; ++k)
237
res.push_back(Size(640 * k, 480 * k));
238
return res;
239
}
240
241
INSTANTIATE_TEST_CASE_P(All, Imgcodecs_ExtSize,
242
testing::Combine(
243
testing::ValuesIn(all_exts),
244
testing::ValuesIn(all_sizes())));
245
246
#ifdef HAVE_IMGCODEC_PXM
247
typedef testing::TestWithParam<bool> Imgcodecs_pbm;
248
TEST_P(Imgcodecs_pbm, write_read)
249
{
250
bool binary = GetParam();
251
const String ext = "pbm";
252
const string full_name = cv::tempfile(ext.c_str());
253
254
Size size(640, 480);
255
const Point2i center = Point2i(size.width / 2, size.height / 2);
256
const int radius = std::min(size.height, size.width / 4);
257
Mat image(size, CV_8UC1, Scalar::all(0));
258
circle(image, center, radius, Scalar::all(255));
259
260
vector<int> pbm_params;
261
pbm_params.push_back(IMWRITE_PXM_BINARY);
262
pbm_params.push_back(binary);
263
264
imwrite( full_name, image, pbm_params );
265
Mat loaded = imread(full_name, IMREAD_UNCHANGED);
266
ASSERT_FALSE(loaded.empty());
267
268
EXPECT_EQ(0, cvtest::norm(loaded, image, NORM_INF));
269
270
FILE *f = fopen(full_name.c_str(), "rb");
271
ASSERT_TRUE(f != NULL);
272
ASSERT_EQ('P', getc(f));
273
ASSERT_EQ('1' + (binary ? 3 : 0), getc(f));
274
fclose(f);
275
EXPECT_EQ(0, remove(full_name.c_str()));
276
}
277
278
INSTANTIATE_TEST_CASE_P(All, Imgcodecs_pbm, testing::Bool());
279
#endif
280
281
282
//==================================================================================================
283
284
TEST(Imgcodecs_Bmp, read_rle8)
285
{
286
const string root = cvtest::TS::ptr()->get_data_path();
287
Mat rle = imread(root + "readwrite/rle8.bmp");
288
ASSERT_FALSE(rle.empty());
289
Mat ord = imread(root + "readwrite/ordinary.bmp");
290
ASSERT_FALSE(ord.empty());
291
EXPECT_LE(cvtest::norm(rle, ord, NORM_L2), 1.e-10);
292
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), rle, ord);
293
}
294
295
#ifdef HAVE_IMGCODEC_HDR
296
TEST(Imgcodecs_Hdr, regression)
297
{
298
string folder = string(cvtest::TS::ptr()->get_data_path()) + "/readwrite/";
299
string name_rle = folder + "rle.hdr";
300
string name_no_rle = folder + "no_rle.hdr";
301
Mat img_rle = imread(name_rle, -1);
302
ASSERT_FALSE(img_rle.empty()) << "Could not open " << name_rle;
303
Mat img_no_rle = imread(name_no_rle, -1);
304
ASSERT_FALSE(img_no_rle.empty()) << "Could not open " << name_no_rle;
305
306
double min = 0.0, max = 1.0;
307
minMaxLoc(abs(img_rle - img_no_rle), &min, &max);
308
ASSERT_FALSE(max > DBL_EPSILON);
309
string tmp_file_name = tempfile(".hdr");
310
vector<int>param(1);
311
for(int i = 0; i < 2; i++) {
312
param[0] = i;
313
imwrite(tmp_file_name, img_rle, param);
314
Mat written_img = imread(tmp_file_name, -1);
315
ASSERT_FALSE(written_img.empty()) << "Could not open " << tmp_file_name;
316
minMaxLoc(abs(img_rle - written_img), &min, &max);
317
ASSERT_FALSE(max > DBL_EPSILON);
318
}
319
remove(tmp_file_name.c_str());
320
}
321
#endif
322
323
#ifdef HAVE_IMGCODEC_PXM
324
TEST(Imgcodecs_Pam, read_write)
325
{
326
string folder = string(cvtest::TS::ptr()->get_data_path()) + "readwrite/";
327
string filepath = folder + "lena.pam";
328
329
cv::Mat img = cv::imread(filepath);
330
ASSERT_FALSE(img.empty());
331
332
std::vector<int> params;
333
params.push_back(IMWRITE_PAM_TUPLETYPE);
334
params.push_back(IMWRITE_PAM_FORMAT_RGB);
335
336
string writefile = cv::tempfile(".pam");
337
EXPECT_NO_THROW(cv::imwrite(writefile, img, params));
338
cv::Mat reread = cv::imread(writefile);
339
340
string writefile_no_param = cv::tempfile(".pam");
341
EXPECT_NO_THROW(cv::imwrite(writefile_no_param, img));
342
cv::Mat reread_no_param = cv::imread(writefile_no_param);
343
344
EXPECT_EQ(0, cvtest::norm(reread, reread_no_param, NORM_INF));
345
EXPECT_EQ(0, cvtest::norm(img, reread, NORM_INF));
346
347
remove(writefile.c_str());
348
remove(writefile_no_param.c_str());
349
}
350
#endif
351
352
#ifdef HAVE_IMGCODEC_PFM
353
TEST(Imgcodecs_Pfm, read_write)
354
{
355
Mat img = imread(findDataFile("readwrite/lena.pam"));
356
ASSERT_FALSE(img.empty());
357
img.convertTo(img, CV_32F, 1/255.0f);
358
359
std::vector<int> params;
360
string writefile = cv::tempfile(".pfm");
361
EXPECT_NO_THROW(cv::imwrite(writefile, img, params));
362
cv::Mat reread = cv::imread(writefile, IMREAD_UNCHANGED);
363
364
string writefile_no_param = cv::tempfile(".pfm");
365
EXPECT_NO_THROW(cv::imwrite(writefile_no_param, img));
366
cv::Mat reread_no_param = cv::imread(writefile_no_param, IMREAD_UNCHANGED);
367
368
EXPECT_EQ(0, cvtest::norm(reread, reread_no_param, NORM_INF));
369
EXPECT_EQ(0, cvtest::norm(img, reread, NORM_INF));
370
371
EXPECT_EQ(0, remove(writefile.c_str()));
372
EXPECT_EQ(0, remove(writefile_no_param.c_str()));
373
}
374
#endif
375
376
TEST(Imgcodecs, write_parameter_type)
377
{
378
cv::Mat m(10, 10, CV_8UC1, cv::Scalar::all(0));
379
cv::Mat1b m_type = cv::Mat1b::zeros(10, 10);
380
string tmp_file = cv::tempfile(".bmp");
381
EXPECT_NO_THROW(cv::imwrite(tmp_file, cv::Mat(m * 2))) << "* Failed with cv::Mat";
382
EXPECT_NO_THROW(cv::imwrite(tmp_file, m * 2)) << "* Failed with cv::MatExpr";
383
EXPECT_NO_THROW(cv::imwrite(tmp_file, m_type)) << "* Failed with cv::Mat_";
384
EXPECT_NO_THROW(cv::imwrite(tmp_file, m_type * 2)) << "* Failed with cv::MatExpr(Mat_)";
385
cv::Matx<uchar, 10, 10> matx;
386
EXPECT_NO_THROW(cv::imwrite(tmp_file, matx)) << "* Failed with cv::Matx";
387
EXPECT_EQ(0, remove(tmp_file.c_str()));
388
}
389
390
}} // namespace
391
392