opencv/modules/highgui/test/test_video_io.cpp

530 lines
16 KiB
C++
Raw Normal View History

/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "test_precomp.hpp"
#include "opencv2/highgui/highgui.hpp"
using namespace cv;
using namespace std;
2012-04-30 16:33:52 +02:00
namespace cvtest
{
string fourccToString(int fourcc)
{
return format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
}
2012-06-09 17:00:04 +02:00
2012-04-30 16:33:52 +02:00
const VideoFormat g_specific_fmt_list[] =
{
VideoFormat("avi", CV_FOURCC('X', 'V', 'I', 'D')),
VideoFormat("avi", CV_FOURCC('M', 'P', 'E', 'G')),
VideoFormat("avi", CV_FOURCC('M', 'J', 'P', 'G')),
//VideoFormat("avi", CV_FOURCC('I', 'Y', 'U', 'V')),
VideoFormat("mkv", CV_FOURCC('X', 'V', 'I', 'D')),
VideoFormat("mkv", CV_FOURCC('M', 'P', 'E', 'G')),
VideoFormat("mkv", CV_FOURCC('M', 'J', 'P', 'G')),
2012-06-09 17:00:04 +02:00
2012-04-30 16:33:52 +02:00
VideoFormat("mov", CV_FOURCC('m', 'p', '4', 'v')),
VideoFormat()
};
2012-06-09 17:00:04 +02:00
2012-04-30 16:33:52 +02:00
}
class CV_HighGuiTest : public cvtest::BaseTest
{
2011-12-20 08:45:49 +01:00
protected:
2012-02-11 16:28:03 +01:00
void ImageTest(const string& dir);
2012-04-30 16:33:52 +02:00
void VideoTest (const string& dir, const cvtest::VideoFormat& fmt);
2012-02-11 16:28:03 +01:00
void SpecificImageTest (const string& dir);
2012-04-30 16:33:52 +02:00
void SpecificVideoTest (const string& dir, const cvtest::VideoFormat& fmt);
2012-04-30 16:33:52 +02:00
CV_HighGuiTest() {}
~CV_HighGuiTest() {}
2012-02-11 16:28:03 +01:00
virtual void run(int) = 0;
};
class CV_ImageTest : public CV_HighGuiTest
{
public:
2012-04-30 16:33:52 +02:00
CV_ImageTest() {}
~CV_ImageTest() {}
2012-02-11 16:28:03 +01:00
void run(int);
};
class CV_SpecificImageTest : public CV_HighGuiTest
{
public:
2012-04-30 16:33:52 +02:00
CV_SpecificImageTest() {}
~CV_SpecificImageTest() {}
2011-12-20 08:45:49 +01:00
void run(int);
};
2012-02-11 16:28:03 +01:00
class CV_VideoTest : public CV_HighGuiTest
{
public:
2012-04-30 16:33:52 +02:00
CV_VideoTest() {}
~CV_VideoTest() {}
2012-02-11 16:28:03 +01:00
void run(int);
};
2012-04-30 16:33:52 +02:00
class CV_SpecificVideoTest : public CV_HighGuiTest
2012-02-11 16:28:03 +01:00
{
public:
2012-04-30 16:33:52 +02:00
CV_SpecificVideoTest() {}
~CV_SpecificVideoTest() {}
2012-02-11 16:28:03 +01:00
void run(int);
};
2012-02-11 16:28:03 +01:00
void CV_HighGuiTest::ImageTest(const string& dir)
{
2011-12-20 08:45:49 +01:00
string _name = dir + string("../cv/shared/baboon.jpg");
ts->printf(ts->LOG, "reading image : %s\n", _name.c_str());
2011-12-20 08:45:49 +01:00
Mat image = imread(_name);
image.convertTo(image, CV_8UC3);
if (image.empty())
{
ts->set_failed_test_info(ts->FAIL_MISSING_TEST_DATA);
return;
}
2012-04-30 16:33:52 +02:00
const string exts[] = {
#ifdef HAVE_PNG
"png",
#endif
#ifdef HAVE_TIFF
"tiff",
#endif
#ifdef HAVE_JPEG
"jpg",
#endif
#ifdef HAVE_JASPER
"jp2",
#endif
#ifdef HAVE_OPENEXR
"exr",
#endif
"bmp",
"ppm",
"ras"
};
2011-12-20 08:45:49 +01:00
const size_t ext_num = sizeof(exts)/sizeof(exts[0]);
for(size_t i = 0; i < ext_num; ++i)
{
string ext = exts[i];
string full_name = cv::tempfile(ext.c_str());
2011-12-20 08:45:49 +01:00
ts->printf(ts->LOG, " full_name : %s\n", full_name.c_str());
imwrite(full_name, image);
Mat loaded = imread(full_name);
if (loaded.empty())
{
ts->printf(ts->LOG, "Reading failed at fmt=%s\n", ext.c_str());
ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue;
}
const double thresDbell = 20;
double psnr = PSNR(loaded, image);
if (psnr < thresDbell)
{
ts->printf(ts->LOG, "Reading image from file: too big difference (=%g) with fmt=%s\n", psnr, ext.c_str());
ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
continue;
}
vector<uchar> from_file;
FILE *f = fopen(full_name.c_str(), "rb");
fseek(f, 0, SEEK_END);
long len = ftell(f);
from_file.resize((size_t)len);
fseek(f, 0, SEEK_SET);
from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
fclose(f);
2012-02-11 16:28:03 +01:00
2011-12-20 08:45:49 +01:00
vector<uchar> buf;
imencode("." + exts[i], image, buf);
if (buf != from_file)
{
ts->printf(ts->LOG, "Encoding failed with fmt=%s\n", ext.c_str());
ts->set_failed_test_info(ts->FAIL_MISMATCH);
2011-12-20 08:45:49 +01:00
continue;
}
Mat buf_loaded = imdecode(Mat(buf), 1);
if (buf_loaded.empty())
{
ts->printf(ts->LOG, "Decoding failed with fmt=%s\n", ext.c_str());
ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue;
}
psnr = PSNR(buf_loaded, image);
2011-12-20 08:45:49 +01:00
if (psnr < thresDbell)
{
ts->printf(ts->LOG, "Decoding image from memory: too small PSNR (=%gdb) with fmt=%s\n", psnr, ext.c_str());
ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue;
}
}
ts->printf(ts->LOG, "end test function : ImagesTest \n");
ts->set_failed_test_info(ts->OK);
}
2012-04-30 16:33:52 +02:00
void CV_HighGuiTest::VideoTest(const string& dir, const cvtest::VideoFormat& fmt)
2012-03-28 17:27:55 +02:00
{
2011-12-20 08:45:49 +01:00
string src_file = dir + "../cv/shared/video_for_test.avi";
string tmp_name = cv::tempfile((cvtest::fourccToString(fmt.fourcc) + "." + fmt.ext).c_str());
2012-04-30 16:33:52 +02:00
ts->printf(ts->LOG, "reading video : %s and converting it to %s\n", src_file.c_str(), tmp_name.c_str());
2011-12-20 08:45:49 +01:00
CvCapture* cap = cvCaptureFromFile(src_file.c_str());
2011-12-20 08:45:49 +01:00
if (!cap)
{
ts->set_failed_test_info(ts->FAIL_MISMATCH);
return;
}
CvVideoWriter* writer = 0;
2012-04-30 16:33:52 +02:00
vector<Mat> frames;
2011-12-20 08:45:49 +01:00
for(;;)
{
IplImage * img = cvQueryFrame( cap );
if (!img)
break;
2012-06-09 17:00:04 +02:00
2012-04-30 16:33:52 +02:00
frames.push_back(Mat(img).clone());
2011-12-20 08:45:49 +01:00
if (writer == 0)
{
2012-04-30 16:33:52 +02:00
writer = cvCreateVideoWriter(tmp_name.c_str(), fmt.fourcc, 24, cvGetSize(img));
2011-12-20 08:45:49 +01:00
if (writer == 0)
{
2012-04-30 16:33:52 +02:00
ts->printf(ts->LOG, "can't create writer (with fourcc : %d)\n",
cvtest::fourccToString(fmt.fourcc).c_str());
2011-12-20 08:45:49 +01:00
cvReleaseCapture( &cap );
ts->set_failed_test_info(ts->FAIL_MISMATCH);
return;
}
}
cvWriteFrame(writer, img);
}
cvReleaseVideoWriter( &writer );
cvReleaseCapture( &cap );
CvCapture *saved = cvCaptureFromFile(tmp_name.c_str());
if (!saved)
{
ts->set_failed_test_info(ts->FAIL_MISMATCH);
return;
}
const double thresDbell = 20;
2012-04-30 16:33:52 +02:00
for(int i = 0;; i++)
2011-12-20 08:45:49 +01:00
{
IplImage* ipl1 = cvQueryFrame( saved );
2012-04-30 16:33:52 +02:00
if (!ipl1)
2011-12-20 08:45:49 +01:00
break;
2012-04-30 16:33:52 +02:00
Mat img = frames[i];
2011-12-20 08:45:49 +01:00
Mat img1(ipl1);
2012-04-30 16:33:52 +02:00
double psnr = PSNR(img1, img);
if (psnr < thresDbell)
2011-12-20 08:45:49 +01:00
{
2012-04-30 16:33:52 +02:00
printf("Too low psnr = %gdb\n", psnr);
// imwrite("img.png", img);
// imwrite("img1.png", img1);
2011-12-20 08:45:49 +01:00
ts->set_failed_test_info(ts->FAIL_MISMATCH);
break;
}
}
cvReleaseCapture( &saved );
ts->printf(ts->LOG, "end test function : ImagesVideo \n");
}
2012-02-11 16:28:03 +01:00
void CV_HighGuiTest::SpecificImageTest(const string& dir)
{
2012-02-11 16:28:03 +01:00
const size_t IMAGE_COUNT = 10;
for (size_t i = 0; i < IMAGE_COUNT; ++i)
{
stringstream s; s << i;
string file_path = dir+"../python/images/QCIF_0"+s.str()+".bmp";
Mat image = imread(file_path);
if (image.empty())
{
ts->set_failed_test_info(ts->FAIL_MISSING_TEST_DATA);
return;
}
2012-04-30 16:33:52 +02:00
resize(image, image, Size(968, 757), 0.0, 0.0, INTER_CUBIC);
2012-02-11 16:28:03 +01:00
stringstream s_digit; s_digit << i;
string full_name = cv::tempfile((s_digit.str() + ".bmp").c_str());
2012-02-11 16:28:03 +01:00
ts->printf(ts->LOG, " full_name : %s\n", full_name.c_str());
imwrite(full_name, image);
Mat loaded = imread(full_name);
if (loaded.empty())
{
ts->printf(ts->LOG, "Reading failed at fmt=bmp\n");
ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue;
}
const double thresDbell = 20;
double psnr = PSNR(loaded, image);
if (psnr < thresDbell)
{
ts->printf(ts->LOG, "Reading image from file: too big difference (=%g) with fmt=bmp\n", psnr);
ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
continue;
}
vector<uchar> from_file;
FILE *f = fopen(full_name.c_str(), "rb");
fseek(f, 0, SEEK_END);
long len = ftell(f);
from_file.resize((size_t)len);
fseek(f, 0, SEEK_SET);
from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
fclose(f);
vector<uchar> buf;
imencode(".bmp", image, buf);
if (buf != from_file)
{
ts->printf(ts->LOG, "Encoding failed with fmt=bmp\n");
ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue;
}
Mat buf_loaded = imdecode(Mat(buf), 1);
if (buf_loaded.empty())
{
ts->printf(ts->LOG, "Decoding failed with fmt=bmp\n");
ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue;
}
psnr = PSNR(buf_loaded, image);
if (psnr < thresDbell)
{
ts->printf(ts->LOG, "Decoding image from memory: too small PSNR (=%gdb) with fmt=bmp\n", psnr);
ts->set_failed_test_info(ts->FAIL_MISMATCH);
continue;
}
}
2012-02-11 16:28:03 +01:00
ts->printf(ts->LOG, "end test function : SpecificImageTest \n");
ts->set_failed_test_info(ts->OK);
}
2012-04-30 16:33:52 +02:00
void CV_HighGuiTest::SpecificVideoTest(const string& dir, const cvtest::VideoFormat& fmt)
2012-02-11 16:28:03 +01:00
{
2012-04-30 16:33:52 +02:00
string ext = fmt.ext;
int fourcc = fmt.fourcc;
2012-06-09 17:00:04 +02:00
2012-04-30 16:33:52 +02:00
string fourcc_str = cvtest::fourccToString(fourcc);
const string video_file = cv::tempfile((fourcc_str + "." + ext).c_str());
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
Size frame_size(968 & -2, 757 & -2);
VideoWriter writer(video_file, fourcc, 25, frame_size, true);
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
if (!writer.isOpened())
{
// call it repeatedly for easier debugging
2012-06-09 17:00:04 +02:00
VideoWriter writer2(video_file, fourcc, 25, frame_size, true);
2012-04-30 16:33:52 +02:00
ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str());
ts->printf(ts->LOG, "Cannot create VideoWriter object with codec %s.\n", fourcc_str.c_str());
ts->set_failed_test_info(ts->FAIL_MISMATCH);
return;
}
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
const size_t IMAGE_COUNT = 30;
vector<Mat> images;
2012-06-09 17:00:04 +02:00
2012-04-30 16:33:52 +02:00
for( size_t i = 0; i < IMAGE_COUNT; ++i )
{
string file_path = format("%s../python/images/QCIF_%02d.bmp", dir.c_str(), i);
Mat img = imread(file_path, CV_LOAD_IMAGE_COLOR);
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
if (img.empty())
2012-02-11 16:28:03 +01:00
{
2012-04-30 16:33:52 +02:00
ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str());
ts->printf(ts->LOG, "Error: cannot read frame from %s.\n", file_path.c_str());
ts->printf(ts->LOG, "Continue creating the video file...\n");
ts->set_failed_test_info(ts->FAIL_INVALID_TEST_DATA);
break;
2012-02-11 16:28:03 +01:00
}
2012-04-30 16:33:52 +02:00
for (int k = 0; k < img.rows; ++k)
for (int l = 0; l < img.cols; ++l)
if (img.at<Vec3b>(k, l) == Vec3b::all(0))
img.at<Vec3b>(k, l) = Vec3b(0, 255, 0);
else img.at<Vec3b>(k, l) = Vec3b(0, 0, 255);
2012-06-09 17:00:04 +02:00
2012-04-30 16:33:52 +02:00
resize(img, img, frame_size, 0.0, 0.0, INTER_CUBIC);
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
images.push_back(img);
writer << img;
2012-02-11 16:28:03 +01:00
}
2012-04-30 16:33:52 +02:00
writer.release();
VideoCapture cap(video_file);
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
size_t FRAME_COUNT = (size_t)cap.get(CV_CAP_PROP_FRAME_COUNT);
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
if (FRAME_COUNT != IMAGE_COUNT )
2012-02-11 16:28:03 +01:00
{
2012-04-30 16:33:52 +02:00
ts->printf(ts->LOG, "\nFrame count checking for video_%s.%s...\n", fourcc_str.c_str(), ext.c_str());
ts->printf(ts->LOG, "Video codec: %s\n", fourcc_str.c_str());
ts->printf(ts->LOG, "Required frame count: %d; Returned frame count: %d\n", IMAGE_COUNT, FRAME_COUNT);
ts->printf(ts->LOG, "Error: Incorrect frame count in the video.\n");
ts->printf(ts->LOG, "Continue checking...\n");
ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
2012-02-11 16:28:03 +01:00
return;
}
2012-04-30 16:33:52 +02:00
for (int i = 0; (size_t)i < FRAME_COUNT; i++)
2012-02-11 16:28:03 +01:00
{
2012-04-30 16:33:52 +02:00
Mat frame; cap >> frame;
if (frame.empty())
2012-02-11 16:28:03 +01:00
{
2012-03-28 17:27:55 +02:00
ts->printf(ts->LOG, "\nVideo file directory: %s\n", ".");
2012-04-30 16:33:52 +02:00
ts->printf(ts->LOG, "File name: video_%s.%s\n", fourcc_str.c_str(), ext.c_str());
ts->printf(ts->LOG, "Video codec: %s\n", fourcc_str.c_str());
ts->printf(ts->LOG, "Error: cannot read the next frame with index %d.\n", i+1);
ts->set_failed_test_info(ts->FAIL_MISSING_TEST_DATA);
break;
2012-02-11 16:28:03 +01:00
}
2012-04-30 16:33:52 +02:00
Mat img = images[i];
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
const double thresDbell = 40;
double psnr = PSNR(img, frame);
2012-02-11 16:28:03 +01:00
2012-04-30 16:33:52 +02:00
if (psnr > thresDbell)
2012-02-11 16:28:03 +01:00
{
2012-04-30 16:33:52 +02:00
ts->printf(ts->LOG, "\nReading frame from the file video_%s.%s...\n", fourcc_str.c_str(), ext.c_str());
ts->printf(ts->LOG, "Frame index: %d\n", i+1);
ts->printf(ts->LOG, "Difference between saved and original images: %g\n", psnr);
ts->printf(ts->LOG, "Maximum allowed difference: %g\n", thresDbell);
ts->printf(ts->LOG, "Error: too big difference between saved and original images.\n");
break;
2012-02-11 16:28:03 +01:00
}
}
}
void CV_ImageTest::run(int)
{
ImageTest(ts->get_data_path());
}
void CV_SpecificImageTest::run(int)
{
SpecificImageTest(ts->get_data_path());
}
void CV_VideoTest::run(int)
{
2012-04-30 16:33:52 +02:00
for (int i = 0; ; ++i)
2012-02-11 16:28:03 +01:00
{
2012-04-30 16:33:52 +02:00
const cvtest::VideoFormat& fmt = cvtest::g_specific_fmt_list[i];
if( fmt.empty() )
break;
VideoTest(ts->get_data_path(), fmt);
2012-02-11 16:28:03 +01:00
}
}
2012-04-30 16:33:52 +02:00
void CV_SpecificVideoTest::run(int)
2012-02-11 16:28:03 +01:00
{
2012-04-30 16:33:52 +02:00
for (int i = 0; ; ++i)
{
2012-04-30 16:33:52 +02:00
const cvtest::VideoFormat& fmt = cvtest::g_specific_fmt_list[i];
if( fmt.empty() )
break;
SpecificVideoTest(ts->get_data_path(), fmt);
}
2012-02-11 16:28:03 +01:00
}
2012-04-30 16:33:52 +02:00
#ifdef HAVE_JPEG
TEST(Highgui_Image, regression) { CV_ImageTest test; test.safe_run(); }
#endif
2011-06-01 16:23:55 +02:00
2012-05-29 13:41:24 +02:00
#if BUILD_WITH_VIDEO_INPUT_SUPPORT && BUILD_WITH_VIDEO_OUTPUT_SUPPORT && !defined(__APPLE__)
2012-02-11 16:28:03 +01:00
TEST(Highgui_Video, regression) { CV_VideoTest test; test.safe_run(); }
2012-04-30 16:33:52 +02:00
TEST(Highgui_Video, write_read) { CV_SpecificVideoTest test; test.safe_run(); }
#endif
TEST(Highgui_Image, write_read) { CV_SpecificImageTest test; test.safe_run(); }