File size: 12,744 Bytes
be94e5d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 |
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html
#include "test_precomp.hpp"
namespace opencv_test { namespace {
#ifdef HAVE_JPEG
extern "C" {
#include "jpeglib.h"
}
/**
* Test for check whether reading exif orientation tag was processed successfully or not
* The test info is the set of 8 images named testExifRotate_{1 to 8}.jpg
* The test image is the square 10x10 points divided by four sub-squares:
* (R corresponds to Red, G to Green, B to Blue, W to white)
* --------- ---------
* | R | G | | G | R |
* |-------| - (tag 1) |-------| - (tag 2)
* | B | W | | W | B |
* --------- ---------
*
* --------- ---------
* | W | B | | B | W |
* |-------| - (tag 3) |-------| - (tag 4)
* | G | R | | R | G |
* --------- ---------
*
* --------- ---------
* | R | B | | G | W |
* |-------| - (tag 5) |-------| - (tag 6)
* | G | W | | R | B |
* --------- ---------
*
* --------- ---------
* | W | G | | B | R |
* |-------| - (tag 7) |-------| - (tag 8)
* | B | R | | W | G |
* --------- ---------
*
*
* Every image contains exif field with orientation tag (0x112)
* After reading each image the corresponding matrix must be read as
* ---------
* | R | G |
* |-------|
* | B | W |
* ---------
*
*/
typedef testing::TestWithParam<string> Imgcodecs_Jpeg_Exif;
TEST_P(Imgcodecs_Jpeg_Exif, exif_orientation)
{
const string root = cvtest::TS::ptr()->get_data_path();
const string filename = root + GetParam();
const int colorThresholdHigh = 250;
const int colorThresholdLow = 5;
Mat m_img = imread(filename);
ASSERT_FALSE(m_img.empty());
Vec3b vec;
//Checking the first quadrant (with supposed red)
vec = m_img.at<Vec3b>(2, 2); //some point inside the square
EXPECT_LE(vec.val[0], colorThresholdLow);
EXPECT_LE(vec.val[1], colorThresholdLow);
EXPECT_GE(vec.val[2], colorThresholdHigh);
//Checking the second quadrant (with supposed green)
vec = m_img.at<Vec3b>(2, 7); //some point inside the square
EXPECT_LE(vec.val[0], colorThresholdLow);
EXPECT_GE(vec.val[1], colorThresholdHigh);
EXPECT_LE(vec.val[2], colorThresholdLow);
//Checking the third quadrant (with supposed blue)
vec = m_img.at<Vec3b>(7, 2); //some point inside the square
EXPECT_GE(vec.val[0], colorThresholdHigh);
EXPECT_LE(vec.val[1], colorThresholdLow);
EXPECT_LE(vec.val[2], colorThresholdLow);
}
const string exif_files[] =
{
"readwrite/testExifOrientation_1.jpg",
"readwrite/testExifOrientation_2.jpg",
"readwrite/testExifOrientation_3.jpg",
"readwrite/testExifOrientation_4.jpg",
"readwrite/testExifOrientation_5.jpg",
"readwrite/testExifOrientation_6.jpg",
"readwrite/testExifOrientation_7.jpg",
"readwrite/testExifOrientation_8.jpg"
};
INSTANTIATE_TEST_CASE_P(ExifFiles, Imgcodecs_Jpeg_Exif,
testing::ValuesIn(exif_files));
//==================================================================================================
TEST(Imgcodecs_Jpeg, encode_empty)
{
cv::Mat img;
std::vector<uchar> jpegImg;
ASSERT_THROW(cv::imencode(".jpg", img, jpegImg), cv::Exception);
}
TEST(Imgcodecs_Jpeg, encode_decode_progressive_jpeg)
{
cvtest::TS& ts = *cvtest::TS::ptr();
string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
cv::Mat img = cv::imread(input);
ASSERT_FALSE(img.empty());
std::vector<int> params;
params.push_back(IMWRITE_JPEG_PROGRESSIVE);
params.push_back(1);
string output_progressive = cv::tempfile(".jpg");
EXPECT_NO_THROW(cv::imwrite(output_progressive, img, params));
cv::Mat img_jpg_progressive = cv::imread(output_progressive);
string output_normal = cv::tempfile(".jpg");
EXPECT_NO_THROW(cv::imwrite(output_normal, img));
cv::Mat img_jpg_normal = cv::imread(output_normal);
EXPECT_EQ(0, cvtest::norm(img_jpg_progressive, img_jpg_normal, NORM_INF));
EXPECT_EQ(0, remove(output_progressive.c_str()));
EXPECT_EQ(0, remove(output_normal.c_str()));
}
TEST(Imgcodecs_Jpeg, encode_decode_optimize_jpeg)
{
cvtest::TS& ts = *cvtest::TS::ptr();
string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
cv::Mat img = cv::imread(input);
ASSERT_FALSE(img.empty());
std::vector<int> params;
params.push_back(IMWRITE_JPEG_OPTIMIZE);
params.push_back(1);
string output_optimized = cv::tempfile(".jpg");
EXPECT_NO_THROW(cv::imwrite(output_optimized, img, params));
cv::Mat img_jpg_optimized = cv::imread(output_optimized);
string output_normal = cv::tempfile(".jpg");
EXPECT_NO_THROW(cv::imwrite(output_normal, img));
cv::Mat img_jpg_normal = cv::imread(output_normal);
EXPECT_EQ(0, cvtest::norm(img_jpg_optimized, img_jpg_normal, NORM_INF));
EXPECT_EQ(0, remove(output_optimized.c_str()));
EXPECT_EQ(0, remove(output_normal.c_str()));
}
TEST(Imgcodecs_Jpeg, encode_decode_rst_jpeg)
{
cvtest::TS& ts = *cvtest::TS::ptr();
string input = string(ts.get_data_path()) + "../cv/shared/lena.png";
cv::Mat img = cv::imread(input);
ASSERT_FALSE(img.empty());
std::vector<int> params;
params.push_back(IMWRITE_JPEG_RST_INTERVAL);
params.push_back(1);
string output_rst = cv::tempfile(".jpg");
EXPECT_NO_THROW(cv::imwrite(output_rst, img, params));
cv::Mat img_jpg_rst = cv::imread(output_rst);
string output_normal = cv::tempfile(".jpg");
EXPECT_NO_THROW(cv::imwrite(output_normal, img));
cv::Mat img_jpg_normal = cv::imread(output_normal);
EXPECT_EQ(0, cvtest::norm(img_jpg_rst, img_jpg_normal, NORM_INF));
EXPECT_EQ(0, remove(output_rst.c_str()));
EXPECT_EQ(0, remove(output_normal.c_str()));
}
// See https://github.com/opencv/opencv/issues/25274
typedef testing::TestWithParam<int> Imgcodecs_Jpeg_decode_cmyk;
TEST_P(Imgcodecs_Jpeg_decode_cmyk, regression25274)
{
const int imread_flag = GetParam();
/*
* "test_1_c4.jpg" is CMYK-JPEG.
* $ convert test_1_c3.jpg -colorspace CMYK test_1_c4.jpg
* $ identify test_1_c4.jpg
* test_1_c4.jpg JPEG 480x640 480x640+0+0 8-bit CMYK 11240B 0.000u 0:00.000
*/
cvtest::TS& ts = *cvtest::TS::ptr();
string rgb_filename = string(ts.get_data_path()) + "readwrite/test_1_c3.jpg";
cv::Mat rgb_img = cv::imread(rgb_filename, imread_flag);
ASSERT_FALSE(rgb_img.empty());
string cmyk_filename = string(ts.get_data_path()) + "readwrite/test_1_c4.jpg";
cv::Mat cmyk_img = cv::imread(cmyk_filename, imread_flag);
ASSERT_FALSE(cmyk_img.empty());
EXPECT_EQ(rgb_img.size(), cmyk_img.size());
EXPECT_EQ(rgb_img.type(), cmyk_img.type());
// Jpeg is lossy compression.
// There may be small differences in decoding results by environments.
// -> 255 * 1% = 2.55 .
EXPECT_LE(cvtest::norm(rgb_img, cmyk_img, NORM_INF), 3); // norm() <= 3
}
INSTANTIATE_TEST_CASE_P( /* nothing */,
Imgcodecs_Jpeg_decode_cmyk,
testing::Values(cv::IMREAD_COLOR,
cv::IMREAD_GRAYSCALE,
cv::IMREAD_ANYCOLOR));
//==================================================================================================
static const uint32_t default_sampling_factor = static_cast<uint32_t>(0x221111);
static uint32_t test_jpeg_subsampling( const Mat src, const vector<int> param )
{
vector<uint8_t> jpeg;
if ( cv::imencode(".jpg", src, jpeg, param ) == false )
{
return 0;
}
if ( src.channels() != 3 )
{
return 0;
}
// Find SOF Marker(FFC0)
int sof_offset = 0; // not found.
int jpeg_size = static_cast<int>( jpeg.size() );
for ( int i = 0 ; i < jpeg_size - 1; i++ )
{
if ( (jpeg[i] == 0xff ) && ( jpeg[i+1] == 0xC0 ) )
{
sof_offset = i;
break;
}
}
if ( sof_offset == 0 )
{
return 0;
}
// Extract Subsampling Factor from SOF.
return ( jpeg[sof_offset + 0x0A + 3 * 0 + 1] << 16 ) +
( jpeg[sof_offset + 0x0A + 3 * 1 + 1] << 8 ) +
( jpeg[sof_offset + 0x0A + 3 * 2 + 1] ) ;
}
TEST(Imgcodecs_Jpeg, encode_subsamplingfactor_default)
{
vector<int> param;
Mat src( 48, 64, CV_8UC3, cv::Scalar::all(0) );
EXPECT_EQ( default_sampling_factor, test_jpeg_subsampling(src, param) );
}
TEST(Imgcodecs_Jpeg, encode_subsamplingfactor_usersetting_valid)
{
Mat src( 48, 64, CV_8UC3, cv::Scalar::all(0) );
const uint32_t sampling_factor_list[] = {
IMWRITE_JPEG_SAMPLING_FACTOR_411,
IMWRITE_JPEG_SAMPLING_FACTOR_420,
IMWRITE_JPEG_SAMPLING_FACTOR_422,
IMWRITE_JPEG_SAMPLING_FACTOR_440,
IMWRITE_JPEG_SAMPLING_FACTOR_444,
};
const int sampling_factor_list_num = 5;
for ( int i = 0 ; i < sampling_factor_list_num; i ++ )
{
vector<int> param;
param.push_back( IMWRITE_JPEG_SAMPLING_FACTOR );
param.push_back( sampling_factor_list[i] );
EXPECT_EQ( sampling_factor_list[i], test_jpeg_subsampling(src, param) );
}
}
TEST(Imgcodecs_Jpeg, encode_subsamplingfactor_usersetting_invalid)
{
Mat src( 48, 64, CV_8UC3, cv::Scalar::all(0) );
const uint32_t sampling_factor_list[] = { // Invalid list
0x111112,
0x000000,
0x001111,
0xFF1111,
0x141111, // 1x4,1x1,1x1 - unknown
0x241111, // 2x4,1x1,1x1 - unknown
0x421111, // 4x2,1x1,1x1 - unknown
0x441111, // 4x4,1x1,1x1 - 410(libjpeg cannot handle it)
};
const int sampling_factor_list_num = 8;
for ( int i = 0 ; i < sampling_factor_list_num; i ++ )
{
vector<int> param;
param.push_back( IMWRITE_JPEG_SAMPLING_FACTOR );
param.push_back( sampling_factor_list[i] );
EXPECT_EQ( default_sampling_factor, test_jpeg_subsampling(src, param) );
}
}
//==================================================================================================
// See https://github.com/opencv/opencv/issues/25646
typedef testing::TestWithParam<std::tuple<int, int>> Imgcodecs_Jpeg_encode_withLumaChromaQuality;
TEST_P(Imgcodecs_Jpeg_encode_withLumaChromaQuality, basic)
{
const int luma = get<0>(GetParam());
const int chroma = get<1>(GetParam());
cvtest::TS& ts = *cvtest::TS::ptr();
string fname = string(ts.get_data_path()) + "../cv/shared/lena.png";
cv::Mat src = imread(fname, cv::IMREAD_COLOR);
ASSERT_FALSE(src.empty());
std::vector<uint8_t> jpegNormal;
ASSERT_NO_THROW(cv::imencode(".jpg", src, jpegNormal));
std::vector<int> param;
param.push_back(IMWRITE_JPEG_LUMA_QUALITY);
param.push_back(luma);
param.push_back(IMWRITE_JPEG_CHROMA_QUALITY);
param.push_back(chroma);
std::vector<uint8_t> jpegCustom;
ASSERT_NO_THROW(cv::imencode(".jpg", src, jpegCustom, param));
#if JPEG_LIB_VERSION >= 70
// For jpeg7+, we can support IMWRITE_JPEG_LUMA_QUALITY and IMWRITE_JPEG_CHROMA_QUALITY.
if( (luma == 95 /* Default Luma Quality */ ) && ( chroma == 95 /* Default Chroma Quality */))
{
EXPECT_EQ(jpegNormal, jpegCustom);
}
else
{
EXPECT_NE(jpegNormal, jpegCustom);
}
#else
// For jpeg6-, we cannot support IMWRITE_JPEG_LUMA/CHROMA_QUALITY because jpeg_default_qtables() is missing.
// - IMWRITE_JPEG_LUMA_QUALITY updates internal parameter of IMWRITE_JPEG_QUALITY.
// - IMWRITE_JPEG_CHROMA_QUALITY updates nothing.
if( luma == 95 /* Default Jpeg Quality */ )
{
EXPECT_EQ(jpegNormal, jpegCustom);
}
else
{
EXPECT_NE(jpegNormal, jpegCustom);
}
#endif
}
INSTANTIATE_TEST_CASE_P( /* nothing */,
Imgcodecs_Jpeg_encode_withLumaChromaQuality,
testing::Combine(
testing::Values(70, 95, 100), // IMWRITE_JPEG_LUMA_QUALITY
testing::Values(70, 95, 100) )); // IMWRITE_JPEG_CHROMA_QUALITY
#endif // HAVE_JPEG
}} // namespace
|