Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgcodecs/test/test_read_write.cpp
16354 views
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
6
namespace opencv_test { namespace {
7
8
TEST(Imgcodecs_Image, read_write_bmp)
9
{
10
const size_t IMAGE_COUNT = 10;
11
const double thresDbell = 32;
12
13
for (size_t i = 0; i < IMAGE_COUNT; ++i)
14
{
15
stringstream s; s << i;
16
const string digit = s.str();
17
const string src_name = TS::ptr()->get_data_path() + "../python/images/QCIF_0" + digit + ".bmp";
18
const string dst_name = cv::tempfile((digit + ".bmp").c_str());
19
Mat image = imread(src_name);
20
ASSERT_FALSE(image.empty());
21
22
resize(image, image, Size(968, 757), 0.0, 0.0, INTER_CUBIC);
23
imwrite(dst_name, image);
24
Mat loaded = imread(dst_name);
25
ASSERT_FALSE(loaded.empty());
26
27
double psnr = cvtest::PSNR(loaded, image);
28
EXPECT_GT(psnr, thresDbell);
29
30
vector<uchar> from_file;
31
32
FILE *f = fopen(dst_name.c_str(), "rb");
33
fseek(f, 0, SEEK_END);
34
long len = ftell(f);
35
from_file.resize((size_t)len);
36
fseek(f, 0, SEEK_SET);
37
from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
38
fclose(f);
39
40
vector<uchar> buf;
41
imencode(".bmp", image, buf);
42
ASSERT_EQ(buf, from_file);
43
44
Mat buf_loaded = imdecode(Mat(buf), 1);
45
ASSERT_FALSE(buf_loaded.empty());
46
47
psnr = cvtest::PSNR(buf_loaded, image);
48
EXPECT_GT(psnr, thresDbell);
49
50
EXPECT_EQ(0, remove(dst_name.c_str()));
51
}
52
}
53
54
//==================================================================================================
55
56
typedef string Ext;
57
typedef testing::TestWithParam<Ext> Imgcodecs_Image;
58
59
TEST_P(Imgcodecs_Image, read_write)
60
{
61
const string ext = this->GetParam();
62
const string full_name = cv::tempfile(ext.c_str());
63
const string _name = TS::ptr()->get_data_path() + "../cv/shared/baboon.png";
64
const double thresDbell = 32;
65
66
Mat image = imread(_name);
67
image.convertTo(image, CV_8UC3);
68
ASSERT_FALSE(image.empty());
69
70
imwrite(full_name, image);
71
Mat loaded = imread(full_name);
72
ASSERT_FALSE(loaded.empty());
73
74
double psnr = cvtest::PSNR(loaded, image);
75
EXPECT_GT(psnr, thresDbell);
76
77
vector<uchar> from_file;
78
FILE *f = fopen(full_name.c_str(), "rb");
79
fseek(f, 0, SEEK_END);
80
long len = ftell(f);
81
from_file.resize((size_t)len);
82
fseek(f, 0, SEEK_SET);
83
from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
84
fclose(f);
85
vector<uchar> buf;
86
imencode("." + ext, image, buf);
87
ASSERT_EQ(buf, from_file);
88
89
Mat buf_loaded = imdecode(Mat(buf), 1);
90
ASSERT_FALSE(buf_loaded.empty());
91
92
psnr = cvtest::PSNR(buf_loaded, image);
93
EXPECT_GT(psnr, thresDbell);
94
95
EXPECT_EQ(0, remove(full_name.c_str()));
96
}
97
98
const string exts[] = {
99
#ifdef HAVE_PNG
100
"png",
101
#endif
102
#ifdef HAVE_TIFF
103
"tiff",
104
#endif
105
#ifdef HAVE_JPEG
106
"jpg",
107
#endif
108
#ifdef HAVE_JASPER
109
"jp2",
110
#endif
111
#if 0 /*defined HAVE_OPENEXR && !defined __APPLE__*/
112
"exr",
113
#endif
114
"bmp",
115
#ifdef HAVE_IMGCODEC_PXM
116
"ppm",
117
#endif
118
#ifdef HAVE_IMGCODEC_SUNRASTER
119
"ras",
120
#endif
121
};
122
123
INSTANTIATE_TEST_CASE_P(imgcodecs, Imgcodecs_Image, testing::ValuesIn(exts));
124
125
TEST(Imgcodecs_Image, regression_9376)
126
{
127
String path = findDataFile("readwrite/regression_9376.bmp");
128
Mat m = imread(path);
129
ASSERT_FALSE(m.empty());
130
EXPECT_EQ(32, m.cols);
131
EXPECT_EQ(32, m.rows);
132
}
133
134
//==================================================================================================
135
136
TEST(Imgcodecs_Image, write_umat)
137
{
138
const string src_name = TS::ptr()->get_data_path() + "../python/images/baboon.bmp";
139
const string dst_name = cv::tempfile(".bmp");
140
141
Mat image1 = imread(src_name);
142
ASSERT_FALSE(image1.empty());
143
144
UMat image1_umat = image1.getUMat(ACCESS_RW);
145
146
imwrite(dst_name, image1_umat);
147
148
Mat image2 = imread(dst_name);
149
ASSERT_FALSE(image2.empty());
150
151
EXPECT_PRED_FORMAT2(cvtest::MatComparator(0, 0), image1, image2);
152
EXPECT_EQ(0, remove(dst_name.c_str()));
153
}
154
155
}} // namespace
156
157