2011-02-09 21:55:11 +01:00
|
|
|
/*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"
|
2011-06-02 17:23:37 +02:00
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2011-02-09 21:55:11 +01:00
|
|
|
|
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
class CV_HighGuiTest : public cvtest::BaseTest
|
2011-02-09 21:55:11 +01:00
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
protected:
|
|
|
|
void ImagesTest(const string& dir);
|
|
|
|
void VideoTest (const string& dir, int fourcc);
|
2011-02-09 21:55:11 +01:00
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
public:
|
|
|
|
void run(int);
|
2011-02-09 21:55:11 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
double PSNR(const Mat& m1, const Mat& m2)
|
|
|
|
{
|
|
|
|
Mat tmp;
|
|
|
|
absdiff( m1.reshape(1), m2.reshape(1), tmp);
|
|
|
|
multiply(tmp, tmp, tmp);
|
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
double MSE = 1.0/(tmp.cols * tmp.rows) * sum(tmp)[0];
|
2011-02-09 21:55:11 +01:00
|
|
|
|
|
|
|
return 20 * log10(255.0 / sqrt(MSE));
|
|
|
|
}
|
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
void CV_HighGuiTest::ImagesTest(const string& dir)
|
2011-02-09 21:55:11 +01:00
|
|
|
{
|
2011-06-03 10:33:27 +02:00
|
|
|
string _name = dir + string("../cv/shared/baboon.jpg");
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->printf(ts->LOG, "reading image : %s\n", _name.c_str());
|
|
|
|
|
2011-06-03 10:33:27 +02:00
|
|
|
Mat image = imread(_name);
|
2011-06-02 17:23:37 +02:00
|
|
|
image.convertTo(image, CV_8UC3);
|
2011-02-09 21:55:11 +01:00
|
|
|
|
|
|
|
if (image.empty())
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->set_failed_test_info(ts->FAIL_MISSING_TEST_DATA);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const string exts[] = {"png", "bmp", "tiff", "jpg", "jp2", "ppm", "ras" };
|
2011-02-09 21:55:11 +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];
|
2011-06-02 17:23:37 +02:00
|
|
|
string full_name = dir + "img." + ext;
|
|
|
|
ts->printf(ts->LOG, " full_name : %s\n", full_name.c_str());
|
|
|
|
|
|
|
|
imwrite(full_name, image);
|
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
Mat loaded = imread(full_name);
|
|
|
|
if (loaded.empty())
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->printf(ts->LOG, "Reading failed at fmt=%s\n", ext.c_str());
|
|
|
|
ts->set_failed_test_info(ts->FAIL_MISMATCH);
|
2011-02-09 21:55:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const double thresDbell = 20;
|
|
|
|
double psnr = PSNR(loaded, image);
|
|
|
|
if (psnr < thresDbell)
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
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);
|
2011-02-09 21:55:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
vector<uchar> from_file;
|
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
FILE *f = fopen(full_name.c_str(), "rb");
|
|
|
|
fseek(f, 0, SEEK_END);
|
2011-06-02 17:23:37 +02:00
|
|
|
long len = ftell(f);
|
|
|
|
from_file.resize((size_t)len);
|
2011-02-09 21:55:11 +01:00
|
|
|
fseek(f, 0, SEEK_SET);
|
2011-06-02 17:23:37 +02:00
|
|
|
from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
|
2011-02-09 21:55:11 +01:00
|
|
|
fclose(f);
|
2011-06-02 17:23:37 +02:00
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
vector<uchar> buf;
|
|
|
|
imencode("." + exts[i], image, buf);
|
|
|
|
|
|
|
|
if (buf != from_file)
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->printf(ts->LOG, "Encoding failed with fmt=%s\n", ext.c_str());
|
|
|
|
ts->set_failed_test_info(ts->FAIL_MISMATCH);
|
2011-02-09 21:55:11 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
Mat buf_loaded = imdecode(Mat(buf), 1);
|
2011-06-02 17:23:37 +02:00
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
if (buf_loaded.empty())
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->printf(ts->LOG, "Decoding failed with fmt=%s\n", ext.c_str());
|
|
|
|
ts->set_failed_test_info(ts->FAIL_MISMATCH);
|
|
|
|
continue;
|
2011-02-09 21:55:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
psnr = PSNR(buf_loaded, image);
|
2011-06-02 17:23:37 +02:00
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
if (psnr < thresDbell)
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
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);
|
2011-02-09 21:55:11 +01:00
|
|
|
continue;
|
2011-06-02 17:23:37 +02:00
|
|
|
}
|
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
}
|
2011-06-02 17:23:37 +02:00
|
|
|
|
|
|
|
ts->printf(ts->LOG, "end test function : ImagesTest \n");
|
|
|
|
ts->set_failed_test_info(ts->OK);
|
2011-02-09 21:55:11 +01:00
|
|
|
}
|
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
void CV_HighGuiTest::VideoTest(const string& dir, int fourcc)
|
2011-02-09 21:55:11 +01:00
|
|
|
{
|
2011-06-03 10:33:27 +02:00
|
|
|
string src_file = dir + "../cv/shared/video_for_test.avi";
|
2011-06-02 17:23:37 +02:00
|
|
|
string tmp_name = dir + "video.avi";
|
|
|
|
|
|
|
|
ts->printf(ts->LOG, "reading video : %s\n", src_file.c_str());
|
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
CvCapture* cap = cvCaptureFromFile(src_file.c_str());
|
|
|
|
|
|
|
|
if (!cap)
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->set_failed_test_info(ts->FAIL_MISMATCH);
|
|
|
|
return;
|
2011-02-09 21:55:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
CvVideoWriter* writer = 0;
|
|
|
|
|
|
|
|
int counter = 0;
|
|
|
|
for(;;)
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
IplImage * img = cvQueryFrame( cap );
|
2011-02-09 21:55:11 +01:00
|
|
|
|
|
|
|
if (!img)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (writer == 0)
|
|
|
|
{
|
|
|
|
writer = cvCreateVideoWriter(tmp_name.c_str(), fourcc, 24, cvGetSize(img));
|
|
|
|
if (writer == 0)
|
|
|
|
{
|
2011-06-03 10:33:27 +02:00
|
|
|
ts->printf(ts->LOG, "can't create writer (with fourcc : %d)\n", fourcc);
|
2011-02-09 21:55:11 +01:00
|
|
|
cvReleaseCapture( &cap );
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->set_failed_test_info(ts->FAIL_MISMATCH);
|
|
|
|
return;
|
2011-02-09 21:55:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cvWriteFrame(writer, img);
|
|
|
|
}
|
|
|
|
|
|
|
|
cvReleaseVideoWriter( &writer );
|
|
|
|
cvReleaseCapture( &cap );
|
|
|
|
|
|
|
|
cap = cvCaptureFromFile(src_file.c_str());
|
2011-06-02 17:23:37 +02:00
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
CvCapture *saved = cvCaptureFromFile(tmp_name.c_str());
|
|
|
|
if (!saved)
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->set_failed_test_info(ts->FAIL_MISMATCH);
|
|
|
|
return;
|
2011-02-09 21:55:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
const double thresDbell = 20;
|
|
|
|
|
|
|
|
counter = 0;
|
|
|
|
for(;;)
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
IplImage* ipl = cvQueryFrame( cap );
|
2011-02-09 21:55:11 +01:00
|
|
|
IplImage* ipl1 = cvQueryFrame( saved );
|
|
|
|
|
|
|
|
if (!ipl || !ipl1)
|
|
|
|
break;
|
|
|
|
|
|
|
|
Mat img(ipl);
|
|
|
|
Mat img1(ipl1);
|
|
|
|
|
|
|
|
if (PSNR(img1, img) < thresDbell)
|
|
|
|
{
|
2011-06-02 17:23:37 +02:00
|
|
|
ts->set_failed_test_info(ts->FAIL_MISMATCH);
|
2011-02-09 21:55:11 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cvReleaseCapture( &cap );
|
|
|
|
cvReleaseCapture( &saved );
|
2011-06-02 17:23:37 +02:00
|
|
|
|
|
|
|
ts->printf(ts->LOG, "end test function : ImagesVideo \n");
|
2011-02-09 21:55:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void CV_HighGuiTest::run( int /*start_from */)
|
2011-06-02 17:23:37 +02:00
|
|
|
{
|
|
|
|
ImagesTest(ts->get_data_path());
|
|
|
|
|
2011-02-09 21:55:11 +01:00
|
|
|
#if defined WIN32 || defined __linux__
|
|
|
|
#if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
|
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
VideoTest(ts->get_data_path(), CV_FOURCC_DEFAULT);
|
2011-02-09 21:55:11 +01:00
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
VideoTest(ts->get_data_path(), CV_FOURCC('X', 'V', 'I', 'D'));
|
2011-02-09 21:55:11 +01:00
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
VideoTest(ts->get_data_path(), CV_FOURCC('M', 'P', 'G', '2'));
|
2011-02-09 21:55:11 +01:00
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
VideoTest(ts->get_data_path(), CV_FOURCC('M', 'J', 'P', 'G'));
|
2011-02-09 21:55:11 +01:00
|
|
|
|
|
|
|
#endif
|
2011-06-01 16:23:55 +02:00
|
|
|
#endif
|
2011-06-02 17:23:37 +02:00
|
|
|
}
|
2011-06-01 16:23:55 +02:00
|
|
|
|
2011-06-02 17:23:37 +02:00
|
|
|
TEST(Highgui_HighGui, regression) { CV_HighGuiTest test; test.safe_run(); }
|