Path: blob/master/modules/imgcodecs/test/test_jpeg.cpp
16354 views
// This file is part of OpenCV project.1// It is subject to the license terms in the LICENSE file found in the top-level directory2// of this distribution and at http://opencv.org/license.html3#include "test_precomp.hpp"45namespace opencv_test { namespace {67#ifdef HAVE_JPEG89/**10* Test for check whether reading exif orientation tag was processed successfully or not11* The test info is the set of 8 images named testExifRotate_{1 to 8}.jpg12* The test image is the square 10x10 points divided by four sub-squares:13* (R corresponds to Red, G to Green, B to Blue, W to white)14* --------- ---------15* | R | G | | G | R |16* |-------| - (tag 1) |-------| - (tag 2)17* | B | W | | W | B |18* --------- ---------19*20* --------- ---------21* | W | B | | B | W |22* |-------| - (tag 3) |-------| - (tag 4)23* | G | R | | R | G |24* --------- ---------25*26* --------- ---------27* | R | B | | G | W |28* |-------| - (tag 5) |-------| - (tag 6)29* | G | W | | R | B |30* --------- ---------31*32* --------- ---------33* | W | G | | B | R |34* |-------| - (tag 7) |-------| - (tag 8)35* | B | R | | W | G |36* --------- ---------37*38*39* Every image contains exif field with orientation tag (0x112)40* After reading each image the corresponding matrix must be read as41* ---------42* | R | G |43* |-------|44* | B | W |45* ---------46*47*/4849typedef testing::TestWithParam<string> Imgcodecs_Jpeg_Exif;5051TEST_P(Imgcodecs_Jpeg_Exif, exif_orientation)52{53const string root = cvtest::TS::ptr()->get_data_path();54const string filename = root + GetParam();55const int colorThresholdHigh = 250;56const int colorThresholdLow = 5;5758Mat m_img = imread(filename);59ASSERT_FALSE(m_img.empty());60Vec3b vec;6162//Checking the first quadrant (with supposed red)63vec = m_img.at<Vec3b>(2, 2); //some point inside the square64EXPECT_LE(vec.val[0], colorThresholdLow);65EXPECT_LE(vec.val[1], colorThresholdLow);66EXPECT_GE(vec.val[2], colorThresholdHigh);6768//Checking the second quadrant (with supposed green)69vec = m_img.at<Vec3b>(2, 7); //some point inside the square70EXPECT_LE(vec.val[0], colorThresholdLow);71EXPECT_GE(vec.val[1], colorThresholdHigh);72EXPECT_LE(vec.val[2], colorThresholdLow);7374//Checking the third quadrant (with supposed blue)75vec = m_img.at<Vec3b>(7, 2); //some point inside the square76EXPECT_GE(vec.val[0], colorThresholdHigh);77EXPECT_LE(vec.val[1], colorThresholdLow);78EXPECT_LE(vec.val[2], colorThresholdLow);79}8081const string exif_files[] =82{83"readwrite/testExifOrientation_1.jpg",84"readwrite/testExifOrientation_2.jpg",85"readwrite/testExifOrientation_3.jpg",86"readwrite/testExifOrientation_4.jpg",87"readwrite/testExifOrientation_5.jpg",88"readwrite/testExifOrientation_6.jpg",89"readwrite/testExifOrientation_7.jpg",90"readwrite/testExifOrientation_8.jpg"91};9293INSTANTIATE_TEST_CASE_P(ExifFiles, Imgcodecs_Jpeg_Exif,94testing::ValuesIn(exif_files));9596//==================================================================================================9798TEST(Imgcodecs_Jpeg, encode_empty)99{100cv::Mat img;101std::vector<uchar> jpegImg;102ASSERT_THROW(cv::imencode(".jpg", img, jpegImg), cv::Exception);103}104105TEST(Imgcodecs_Jpeg, encode_decode_progressive_jpeg)106{107cvtest::TS& ts = *cvtest::TS::ptr();108string input = string(ts.get_data_path()) + "../cv/shared/lena.png";109cv::Mat img = cv::imread(input);110ASSERT_FALSE(img.empty());111112std::vector<int> params;113params.push_back(IMWRITE_JPEG_PROGRESSIVE);114params.push_back(1);115116string output_progressive = cv::tempfile(".jpg");117EXPECT_NO_THROW(cv::imwrite(output_progressive, img, params));118cv::Mat img_jpg_progressive = cv::imread(output_progressive);119120string output_normal = cv::tempfile(".jpg");121EXPECT_NO_THROW(cv::imwrite(output_normal, img));122cv::Mat img_jpg_normal = cv::imread(output_normal);123124EXPECT_EQ(0, cvtest::norm(img_jpg_progressive, img_jpg_normal, NORM_INF));125126EXPECT_EQ(0, remove(output_progressive.c_str()));127EXPECT_EQ(0, remove(output_normal.c_str()));128}129130TEST(Imgcodecs_Jpeg, encode_decode_optimize_jpeg)131{132cvtest::TS& ts = *cvtest::TS::ptr();133string input = string(ts.get_data_path()) + "../cv/shared/lena.png";134cv::Mat img = cv::imread(input);135ASSERT_FALSE(img.empty());136137std::vector<int> params;138params.push_back(IMWRITE_JPEG_OPTIMIZE);139params.push_back(1);140141string output_optimized = cv::tempfile(".jpg");142EXPECT_NO_THROW(cv::imwrite(output_optimized, img, params));143cv::Mat img_jpg_optimized = cv::imread(output_optimized);144145string output_normal = cv::tempfile(".jpg");146EXPECT_NO_THROW(cv::imwrite(output_normal, img));147cv::Mat img_jpg_normal = cv::imread(output_normal);148149EXPECT_EQ(0, cvtest::norm(img_jpg_optimized, img_jpg_normal, NORM_INF));150151EXPECT_EQ(0, remove(output_optimized.c_str()));152EXPECT_EQ(0, remove(output_normal.c_str()));153}154155TEST(Imgcodecs_Jpeg, encode_decode_rst_jpeg)156{157cvtest::TS& ts = *cvtest::TS::ptr();158string input = string(ts.get_data_path()) + "../cv/shared/lena.png";159cv::Mat img = cv::imread(input);160ASSERT_FALSE(img.empty());161162std::vector<int> params;163params.push_back(IMWRITE_JPEG_RST_INTERVAL);164params.push_back(1);165166string output_rst = cv::tempfile(".jpg");167EXPECT_NO_THROW(cv::imwrite(output_rst, img, params));168cv::Mat img_jpg_rst = cv::imread(output_rst);169170string output_normal = cv::tempfile(".jpg");171EXPECT_NO_THROW(cv::imwrite(output_normal, img));172cv::Mat img_jpg_normal = cv::imread(output_normal);173174EXPECT_EQ(0, cvtest::norm(img_jpg_rst, img_jpg_normal, NORM_INF));175176EXPECT_EQ(0, remove(output_rst.c_str()));177EXPECT_EQ(0, remove(output_normal.c_str()));178}179180#endif // HAVE_JPEG181182}} // namespace183184185