1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #include "test_precomp.hpp"
44 #include "opencv2/highgui/highgui.hpp"
49 class CV_HighGuiTest : public cvtest::BaseTest
52 void ImageTest(const string& dir);
53 void VideoTest (const string& dir, int fourcc);
54 void SpecificImageTest (const string& dir);
55 void SpecificVideoFileTest (const string& dir, const char codecchars[4]);
56 void SpecificVideoCameraTest (const string& dir, const char codecchars[4]);
61 virtual void run(int) = 0;
64 class CV_ImageTest : public CV_HighGuiTest
72 class CV_SpecificImageTest : public CV_HighGuiTest
75 CV_SpecificImageTest();
76 ~CV_SpecificImageTest();
80 class CV_VideoTest : public CV_HighGuiTest
88 class CV_SpecificVideoFileTest : public CV_HighGuiTest
91 CV_SpecificVideoFileTest();
92 ~CV_SpecificVideoFileTest();
96 class CV_SpecificVideoCameraTest : public CV_HighGuiTest
99 CV_SpecificVideoCameraTest();
100 ~CV_SpecificVideoCameraTest();
104 CV_HighGuiTest::CV_HighGuiTest() {}
105 CV_HighGuiTest::~CV_HighGuiTest() {}
107 CV_ImageTest::CV_ImageTest() : CV_HighGuiTest() {}
108 CV_VideoTest::CV_VideoTest() : CV_HighGuiTest() {}
109 CV_SpecificImageTest::CV_SpecificImageTest() : CV_HighGuiTest() {}
110 CV_SpecificVideoFileTest::CV_SpecificVideoFileTest() : CV_HighGuiTest() {}
111 CV_SpecificVideoCameraTest::CV_SpecificVideoCameraTest() : CV_HighGuiTest() {}
113 CV_ImageTest::~CV_ImageTest() {}
114 CV_VideoTest::~CV_VideoTest() {}
115 CV_SpecificImageTest::~CV_SpecificImageTest() {}
116 CV_SpecificVideoFileTest::~CV_SpecificVideoFileTest() {}
117 CV_SpecificVideoCameraTest::~CV_SpecificVideoCameraTest() {}
119 double PSNR(const Mat& m1, const Mat& m2)
122 absdiff( m1.reshape(1), m2.reshape(1), tmp);
123 multiply(tmp, tmp, tmp);
125 double MSE = 1.0/(tmp.cols * tmp.rows) * sum(tmp)[0];
127 return 20 * log10(255.0 / sqrt(MSE));
130 void CV_HighGuiTest::ImageTest(const string& dir)
132 string _name = dir + string("../cv/shared/baboon.jpg");
133 ts->printf(ts->LOG, "reading image : %s\n", _name.c_str());
135 Mat image = imread(_name);
136 image.convertTo(image, CV_8UC3);
140 ts->set_failed_test_info(ts->FAIL_MISSING_TEST_DATA);
144 const string exts[] = {"png", "bmp", "tiff", "jpg", "jp2", "ppm", "ras" };
145 const size_t ext_num = sizeof(exts)/sizeof(exts[0]);
147 for(size_t i = 0; i < ext_num; ++i)
149 string ext = exts[i];
150 string full_name = dir + "img." + ext;
151 ts->printf(ts->LOG, " full_name : %s\n", full_name.c_str());
153 imwrite(full_name, image);
155 Mat loaded = imread(full_name);
158 ts->printf(ts->LOG, "Reading failed at fmt=%s\n", ext.c_str());
159 ts->set_failed_test_info(ts->FAIL_MISMATCH);
163 const double thresDbell = 20;
164 double psnr = PSNR(loaded, image);
165 if (psnr < thresDbell)
167 ts->printf(ts->LOG, "Reading image from file: too big difference (=%g) with fmt=%s\n", psnr, ext.c_str());
168 ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
172 vector<uchar> from_file;
174 FILE *f = fopen(full_name.c_str(), "rb");
175 fseek(f, 0, SEEK_END);
177 from_file.resize((size_t)len);
178 fseek(f, 0, SEEK_SET);
179 from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
183 imencode("." + exts[i], image, buf);
185 if (buf != from_file)
187 ts->printf(ts->LOG, "Encoding failed with fmt=%s\n", ext.c_str());
188 ts->set_failed_test_info(ts->FAIL_MISMATCH);
192 Mat buf_loaded = imdecode(Mat(buf), 1);
194 if (buf_loaded.empty())
196 ts->printf(ts->LOG, "Decoding failed with fmt=%s\n", ext.c_str());
197 ts->set_failed_test_info(ts->FAIL_MISMATCH);
201 psnr = PSNR(buf_loaded, image);
203 if (psnr < thresDbell)
205 ts->printf(ts->LOG, "Decoding image from memory: too small PSNR (=%gdb) with fmt=%s\n", psnr, ext.c_str());
206 ts->set_failed_test_info(ts->FAIL_MISMATCH);
212 ts->printf(ts->LOG, "end test function : ImagesTest \n");
213 ts->set_failed_test_info(ts->OK);
216 void CV_HighGuiTest::VideoTest(const string& dir, int fourcc)
218 string src_file = dir + "../cv/shared/video_for_test.avi";
219 string tmp_name = dir + "video.avi";
221 ts->printf(ts->LOG, "reading video : %s\n", src_file.c_str());
223 CvCapture* cap = cvCaptureFromFile(src_file.c_str());
227 ts->set_failed_test_info(ts->FAIL_MISMATCH);
231 CvVideoWriter* writer = 0;
236 IplImage * img = cvQueryFrame( cap );
243 writer = cvCreateVideoWriter(tmp_name.c_str(), fourcc, 24, cvGetSize(img));
246 ts->printf(ts->LOG, "can't create writer (with fourcc : %d)\n", fourcc);
247 cvReleaseCapture( &cap );
248 ts->set_failed_test_info(ts->FAIL_MISMATCH);
253 cvWriteFrame(writer, img);
256 cvReleaseVideoWriter( &writer );
257 cvReleaseCapture( &cap );
259 cap = cvCaptureFromFile(src_file.c_str());
261 CvCapture *saved = cvCaptureFromFile(tmp_name.c_str());
264 ts->set_failed_test_info(ts->FAIL_MISMATCH);
268 const double thresDbell = 20;
273 IplImage* ipl = cvQueryFrame( cap );
274 IplImage* ipl1 = cvQueryFrame( saved );
282 if (PSNR(img1, img) < thresDbell)
284 ts->set_failed_test_info(ts->FAIL_MISMATCH);
289 cvReleaseCapture( &cap );
290 cvReleaseCapture( &saved );
292 ts->printf(ts->LOG, "end test function : ImagesVideo \n");
295 void CV_HighGuiTest::SpecificImageTest(const string& dir)
297 const size_t IMAGE_COUNT = 10;
299 for (size_t i = 0; i < IMAGE_COUNT; ++i)
301 stringstream s; s << i;
302 string file_path = dir+"../python/images/QCIF_0"+s.str()+".bmp";
303 Mat image = imread(file_path);
307 ts->set_failed_test_info(ts->FAIL_MISSING_TEST_DATA);
311 cv::resize(image, image, cv::Size(968, 757), 0.0, 0.0, cv::INTER_CUBIC);
313 stringstream s_digit; s_digit << i;
315 string full_name = dir + "img_"+s_digit.str()+".bmp";
316 ts->printf(ts->LOG, " full_name : %s\n", full_name.c_str());
318 imwrite(full_name, image);
320 Mat loaded = imread(full_name);
323 ts->printf(ts->LOG, "Reading failed at fmt=bmp\n");
324 ts->set_failed_test_info(ts->FAIL_MISMATCH);
328 const double thresDbell = 20;
329 double psnr = PSNR(loaded, image);
330 if (psnr < thresDbell)
332 ts->printf(ts->LOG, "Reading image from file: too big difference (=%g) with fmt=bmp\n", psnr);
333 ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
337 vector<uchar> from_file;
339 FILE *f = fopen(full_name.c_str(), "rb");
340 fseek(f, 0, SEEK_END);
342 from_file.resize((size_t)len);
343 fseek(f, 0, SEEK_SET);
344 from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
348 imencode(".bmp", image, buf);
350 if (buf != from_file)
352 ts->printf(ts->LOG, "Encoding failed with fmt=bmp\n");
353 ts->set_failed_test_info(ts->FAIL_MISMATCH);
357 Mat buf_loaded = imdecode(Mat(buf), 1);
359 if (buf_loaded.empty())
361 ts->printf(ts->LOG, "Decoding failed with fmt=bmp\n");
362 ts->set_failed_test_info(ts->FAIL_MISMATCH);
366 psnr = PSNR(buf_loaded, image);
368 if (psnr < thresDbell)
370 ts->printf(ts->LOG, "Decoding image from memory: too small PSNR (=%gdb) with fmt=bmp\n", psnr);
371 ts->set_failed_test_info(ts->FAIL_MISMATCH);
376 ts->printf(ts->LOG, "end test function : SpecificImageTest \n");
377 ts->set_failed_test_info(ts->OK);
380 void CV_HighGuiTest::SpecificVideoFileTest(const string& dir, const char codecchars[4])
382 const string ext[] = {"avi", "mov", "mp4"/*, "mpg", "wmv"*/};
384 const size_t n = sizeof(ext)/sizeof(ext[0]);
386 for (size_t j = 0; j < n; ++j) if ((ext[j]!="mp4")||(string(&codecchars[0], 4)!="IYUV"))
388 const string video_file = dir + "video_" + string(&codecchars[0], 4) + "." + ext[j];
392 const size_t IMAGE_COUNT = 30;
394 for(size_t i = 0; i < IMAGE_COUNT; ++i)
396 stringstream s_digit;
397 if (i < 10) {s_digit << "0"; s_digit << i;}
400 const string file_path = dir+"../python/images/QCIF_"+s_digit.str()+".bmp";
402 cv::Mat img = imread(file_path, CV_LOAD_IMAGE_COLOR);
406 ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str());
407 ts->printf(ts->LOG, "Error: cannot read frame from %s.\n", (ts->get_data_path()+"../python/images/QCIF_"+s_digit.str()+".bmp").c_str());
408 ts->printf(ts->LOG, "Continue creating the video file...\n");
409 ts->set_failed_test_info(ts->FAIL_INVALID_TEST_DATA);
413 cv::resize(img, img, Size(968, 757), 0.0, 0.0, cv::INTER_CUBIC);
415 for (int k = 0; k < img.rows; ++k)
416 for (int l = 0; l < img.cols; ++l)
417 if (img.at<Vec3b>(k, l) == Vec3b::all(0))
418 img.at<Vec3b>(k, l) = Vec3b(0, 255, 0);
419 else img.at<Vec3b>(k, l) = Vec3b(0, 0, 255);
421 imwrite(dir+"QCIF_"+s_digit.str()+".bmp", img);
423 if (!writer.isOpened())
425 writer = cv::VideoWriter(video_file, CV_FOURCC(codecchars[0], codecchars[1], codecchars[2], codecchars[3]), 25, cv::Size(img.cols, img.rows), true);
427 if (!writer.isOpened())
429 ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str());
430 ts->printf(ts->LOG, "Cannot create VideoWriter with codec %s.\n", string(&codecchars[0], 4).c_str());
431 ts->set_failed_test_info(ts->FAIL_MISMATCH);
439 writer.~VideoWriter();
441 cv::VideoCapture cap(video_file);
443 size_t FRAME_COUNT = (size_t)cap.get(CV_CAP_PROP_FRAME_COUNT);
445 if (FRAME_COUNT != IMAGE_COUNT)
447 ts->printf(ts->LOG, "\nFrame count checking for video_%s.%s...\n", string(&codecchars[0], 4).c_str(), ext[j].c_str());
448 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
449 ts->printf(ts->LOG, "Required frame count: %d; Returned frame count: %d\n", IMAGE_COUNT, FRAME_COUNT);
450 ts->printf(ts->LOG, "Error: Incorrect frame count in the video.\n");
451 ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
455 cap.set(CV_CAP_PROP_POS_FRAMES, -1);
457 for (size_t i = -1; i < FRAME_COUNT-1; i++)
459 cv::Mat frame; cap >> frame;
462 ts->printf(ts->LOG, "\nError: cannot read the next frame with index %d.\n", i+1);
463 ts->set_failed_test_info(ts->FAIL_MISSING_TEST_DATA);
467 stringstream s_digit;
468 if (i < 10) {s_digit << "0"; s_digit << i;}
471 cv::Mat img = imread(dir+"QCIF_"+s_digit.str()+".bmp", CV_LOAD_IMAGE_COLOR);
475 ts->printf(ts->LOG, "\nError: cannot read an image with index %d.\n", i+1);
476 ts->set_failed_test_info(ts->FAIL_MISMATCH);
480 const double thresDbell = 20;
482 double psnr = PSNR(img, frame);
484 if (psnr > thresDbell)
486 ts->printf(ts->LOG, "\nReading frame from the file video_%s.%s...\n", string(&codecchars[0], 4).c_str(), ext[j].c_str());
487 ts->printf(ts->LOG, "Difference between saved and original images: %g\n", psnr);
488 ts->printf(ts->LOG, "Maximum allowed difference: %g", thresDbell);
489 ts->printf(ts->LOG, "Error: too big difference between saved and original images.\n");
499 void CV_HighGuiTest::SpecificVideoCameraTest(const string& dir, const char codecchars[4])
501 const string ext[] = {"avi", "mov", "mp4"/*, "mpg", "wmv"*/};
503 const size_t n = sizeof(ext)/sizeof(ext[0]);
505 const int IMAGE_COUNT = 125;
507 cv::VideoCapture cap(0);
511 ts->printf(ts->LOG, "\nError: cannot start working with device.\n");
512 ts->set_failed_test_info(ts->FAIL_EXCEPTION);
516 for (size_t i = 0; i < n; ++i) if ((ext[i]!="mp4")||(string(&codecchars[0], 4)!="IYUV"))
518 Mat frame; int framecount = 0;
519 cv::VideoWriter writer;
521 std::vector <cv::Mat> tmp_img(IMAGE_COUNT);
523 writer.open(dir+"video_"+string(&codecchars[0], 4)+"."+ext[i], CV_FOURCC(codecchars[0], codecchars[1], codecchars[2], codecchars[3]), 25, Size(968, 757), true);
525 if (!writer.isOpened())
527 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
528 ts->printf(ts->LOG, "Video codec: %s\n", std::string(&codecchars[0], 4).c_str());
529 ts->printf(ts->LOG, "Error: cannot create VideoWriter object for video_%s.%s.\n", string(&codecchars[0]).c_str(), ext[i].c_str());
530 ts->set_failed_test_info(ts->FAIL_EXCEPTION);
540 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
541 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
542 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
543 ts->printf(ts->LOG, "Frame index: %d\n", framecount);
544 ts->printf(ts->LOG, "Error: cannot read next frame from the device.\n");
548 cv::resize(frame, frame, Size(968, 757), 0, 0, INTER_CUBIC);
549 writer << frame; tmp_img[framecount] = frame;
552 if (framecount == IMAGE_COUNT) break;
556 writer.~VideoWriter();
558 cv::VideoCapture vcap(dir+"video_"+string(&codecchars[0], 4)+"."+ext[i]);
560 if (!vcap.isOpened())
562 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
563 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
564 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
565 ts->printf(ts->LOG, "Error: cannot open video file.\n");
569 int FRAME_COUNT = (int)vcap.get(CV_CAP_PROP_FRAME_COUNT);
571 if (FRAME_COUNT != IMAGE_COUNT)
573 ts->printf(ts->LOG, "\nChecking frame count...\n");
574 ts->printf(ts->LOG, "Video file directory: %s\n", dir.c_str());
575 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
576 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
577 ts->printf(ts->LOG, "Required frame count: %d Returned frame count: %d\n", IMAGE_COUNT, FRAME_COUNT);
578 ts->printf(ts->LOG, "Error: required and returned frame count are not matched.\n");
579 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
583 cv::Mat img; framecount = 0;
584 vcap.set(CV_CAP_PROP_POS_FRAMES, 0);
586 for ( ; framecount <= std::min<int>(FRAME_COUNT, IMAGE_COUNT); framecount++ )
592 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
593 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
594 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
595 ts->printf(ts->LOG, "Frame index: %d\n", framecount);
596 ts->printf(ts->LOG, "Error: cannot read next frame from the video.\n");
600 const double thresDbell = 20;
601 double psnr = PSNR(img, tmp_img[framecount]);
603 if (psnr > thresDbell)
605 ts->printf(ts->LOG, "\nReading frame from the file video_%s.%s...\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
606 ts->printf(ts->LOG, "Difference between saved and original images: %g\n", psnr);
607 ts->printf(ts->LOG, "Maximum allowed difference: %g", thresDbell);
608 ts->printf(ts->LOG, "Error: too big difference between saved and original images.\n");
614 vcap.~VideoCapture();
620 void CV_ImageTest::run(int)
622 ImageTest(ts->get_data_path());
625 void CV_SpecificImageTest::run(int)
627 SpecificImageTest(ts->get_data_path());
630 void CV_VideoTest::run(int)
632 #if defined WIN32 || (defined __linux__ && !defined ANDROID)
633 #if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
635 const char codecs[][4] = { {'I', 'Y', 'U', 'V'},
636 {'X', 'V', 'I', 'D'},
637 {'M', 'P', 'G', '2'},
638 {'M', 'J', 'P', 'G'} };
640 printf("%s", ts->get_data_path().c_str());
642 int count = sizeof(codecs)/(4*sizeof(char));
644 for (int i = 0; i < count; ++i)
646 VideoTest(ts->get_data_path(), CV_FOURCC(codecs[i][0], codecs[i][1], codecs[i][2], codecs[i][3]));
653 void CV_SpecificVideoFileTest::run(int)
655 #if defined WIN32 || (defined __linux__ && !defined ANDROID)
656 #if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
658 const char codecs[][4] = { {'M', 'P', 'G', '2'},
659 {'X', 'V', 'I', 'D'},
660 {'M', 'J', 'P', 'G'},
661 {'I', 'Y', 'U', 'V'} };
663 int count = sizeof(codecs)/(4*sizeof(char));
665 for (int i = 0; i < count; ++i)
667 SpecificVideoFileTest(ts->get_data_path(), codecs[i]);
674 void CV_SpecificVideoCameraTest::run(int)
676 #if defined WIN32 || (defined __linux__ && !defined ANDROID)
677 #if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
679 const char codecs[][4] = { {'M', 'P', 'G', '2'},
680 {'X', 'V', 'I', 'D'},
681 {'M', 'J', 'P', 'G'},
682 {'I', 'Y', 'U', 'V'} };
684 int count = sizeof(codecs)/(4*sizeof(char));
686 for (int i = 0; i < count; ++i)
688 SpecificVideoCameraTest(ts->get_data_path(), codecs[i]);
695 TEST(Highgui_Image, regression) { CV_ImageTest test; test.safe_run(); }
696 TEST(Highgui_Video, regression) { CV_VideoTest test; test.safe_run(); }
697 TEST(Highgui_SpecificImage, regression) { CV_SpecificImageTest test; test.safe_run(); }
698 TEST(Highgui_SpecificVideoFile, regression) { CV_SpecificVideoFileTest test; test.safe_run(); }
699 TEST(Highgui_SpecificVideoCamera, regression) { CV_SpecificVideoCameraTest test; test.safe_run(); }