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;
235 IplImage * img = cvQueryFrame( cap );
242 writer = cvCreateVideoWriter(tmp_name.c_str(), fourcc, 24, cvGetSize(img));
245 ts->printf(ts->LOG, "can't create writer (with fourcc : %d)\n", fourcc);
246 cvReleaseCapture( &cap );
247 ts->set_failed_test_info(ts->FAIL_MISMATCH);
252 cvWriteFrame(writer, img);
255 cvReleaseVideoWriter( &writer );
256 cvReleaseCapture( &cap );
258 cap = cvCaptureFromFile(src_file.c_str());
260 CvCapture *saved = cvCaptureFromFile(tmp_name.c_str());
263 ts->set_failed_test_info(ts->FAIL_MISMATCH);
267 const double thresDbell = 20;
271 IplImage* ipl = cvQueryFrame( cap );
272 IplImage* ipl1 = cvQueryFrame( saved );
280 if (PSNR(img1, img) < thresDbell)
282 ts->set_failed_test_info(ts->FAIL_MISMATCH);
287 cvReleaseCapture( &cap );
288 cvReleaseCapture( &saved );
290 ts->printf(ts->LOG, "end test function : ImagesVideo \n");
293 void CV_HighGuiTest::SpecificImageTest(const string& dir)
295 const size_t IMAGE_COUNT = 10;
297 for (size_t i = 0; i < IMAGE_COUNT; ++i)
299 stringstream s; s << i;
300 string file_path = dir+"../python/images/QCIF_0"+s.str()+".bmp";
301 Mat image = imread(file_path);
305 ts->set_failed_test_info(ts->FAIL_MISSING_TEST_DATA);
309 cv::resize(image, image, cv::Size(968, 757), 0.0, 0.0, cv::INTER_CUBIC);
311 stringstream s_digit; s_digit << i;
313 string full_name = dir + "img_"+s_digit.str()+".bmp";
314 ts->printf(ts->LOG, " full_name : %s\n", full_name.c_str());
316 imwrite(full_name, image);
318 Mat loaded = imread(full_name);
321 ts->printf(ts->LOG, "Reading failed at fmt=bmp\n");
322 ts->set_failed_test_info(ts->FAIL_MISMATCH);
326 const double thresDbell = 20;
327 double psnr = PSNR(loaded, image);
328 if (psnr < thresDbell)
330 ts->printf(ts->LOG, "Reading image from file: too big difference (=%g) with fmt=bmp\n", psnr);
331 ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
335 vector<uchar> from_file;
337 FILE *f = fopen(full_name.c_str(), "rb");
338 fseek(f, 0, SEEK_END);
340 from_file.resize((size_t)len);
341 fseek(f, 0, SEEK_SET);
342 from_file.resize(fread(&from_file[0], 1, from_file.size(), f));
346 imencode(".bmp", image, buf);
348 if (buf != from_file)
350 ts->printf(ts->LOG, "Encoding failed with fmt=bmp\n");
351 ts->set_failed_test_info(ts->FAIL_MISMATCH);
355 Mat buf_loaded = imdecode(Mat(buf), 1);
357 if (buf_loaded.empty())
359 ts->printf(ts->LOG, "Decoding failed with fmt=bmp\n");
360 ts->set_failed_test_info(ts->FAIL_MISMATCH);
364 psnr = PSNR(buf_loaded, image);
366 if (psnr < thresDbell)
368 ts->printf(ts->LOG, "Decoding image from memory: too small PSNR (=%gdb) with fmt=bmp\n", psnr);
369 ts->set_failed_test_info(ts->FAIL_MISMATCH);
374 ts->printf(ts->LOG, "end test function : SpecificImageTest \n");
375 ts->set_failed_test_info(ts->OK);
378 void CV_HighGuiTest::SpecificVideoFileTest(const string& dir, const char codecchars[4])
380 const string ext[] = {"avi", "mov", "mp4", "mpg", "wmv"};
382 const size_t n = sizeof(ext)/sizeof(ext[0]);
384 for (size_t j = 0; j < n; ++j)
385 if ((ext[j]!="mp4")||(string(&codecchars[0], 4)!="IYUV"))
386 #if defined WIN32 || defined _WIN32
387 if (((ext[j]!="mov")||(string(&codecchars[0], 4)=="XVID"))&&(ext[j]!="mp4"))
390 const string video_file = dir + "video_" + string(&codecchars[0], 4) + "." + ext[j];
392 VideoWriter writer = cv::VideoWriter(video_file, CV_FOURCC(codecchars[0], codecchars[1], codecchars[2], codecchars[3]), 25, cv::Size(968, 757), true);
394 if (!writer.isOpened())
396 ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str());
397 ts->printf(ts->LOG, "Cannot create VideoWriter object with codec %s.\n", string(&codecchars[0], 4).c_str());
398 ts->set_failed_test_info(ts->FAIL_MISMATCH);
402 const size_t IMAGE_COUNT = 30;
404 for(size_t i = 0; i < IMAGE_COUNT; ++i)
406 stringstream s_digit;
407 if (i < 10) {s_digit << "0"; s_digit << i;}
410 const string file_path = dir+"../python/images/QCIF_"+s_digit.str()+".bmp";
412 cv::Mat img = imread(file_path, CV_LOAD_IMAGE_COLOR);
416 ts->printf(ts->LOG, "Creating a video in %s...\n", video_file.c_str());
417 ts->printf(ts->LOG, "Error: cannot read frame from %s.\n", (ts->get_data_path()+"../python/images/QCIF_"+s_digit.str()+".bmp").c_str());
418 ts->printf(ts->LOG, "Continue creating the video file...\n");
419 ts->set_failed_test_info(ts->FAIL_INVALID_TEST_DATA);
423 cv::resize(img, img, Size(968, 757), 0.0, 0.0, cv::INTER_CUBIC);
425 for (int k = 0; k < img.rows; ++k)
426 for (int l = 0; l < img.cols; ++l)
427 if (img.at<Vec3b>(k, l) == Vec3b::all(0))
428 img.at<Vec3b>(k, l) = Vec3b(0, 255, 0);
429 else img.at<Vec3b>(k, l) = Vec3b(0, 0, 255);
431 imwrite(dir+"QCIF_"+s_digit.str()+".bmp", img);
436 writer.~VideoWriter();
438 cv::VideoCapture cap(video_file);
440 size_t FRAME_COUNT = (size_t)cap.get(CV_CAP_PROP_FRAME_COUNT);
442 if (FRAME_COUNT != IMAGE_COUNT)
444 ts->printf(ts->LOG, "\nFrame count checking for video_%s.%s...\n", string(&codecchars[0], 4).c_str(), ext[j].c_str());
445 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
446 ts->printf(ts->LOG, "Required frame count: %d; Returned frame count: %d\n", IMAGE_COUNT, FRAME_COUNT);
447 ts->printf(ts->LOG, "Error: Incorrect frame count in the video.\n");
448 ts->printf(ts->LOG, "Continue checking...\n");
449 ts->set_failed_test_info(ts->FAIL_BAD_ACCURACY);
452 cap.set(CV_CAP_PROP_POS_FRAMES, -1);
454 for (int i = -1; i < (int)std::min<size_t>(FRAME_COUNT, IMAGE_COUNT)-1; i++)
456 cv::Mat frame; cap >> frame;
459 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
460 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
461 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
462 ts->printf(ts->LOG, "Error: 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+1 < 10) {s_digit << "0"; s_digit << i+1;}
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 from %s.\n", (dir+"QCIF_"+s_digit.str()+".bmp").c_str());
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, "Frame index: %d\n", i+1);
488 ts->printf(ts->LOG, "Difference between saved and original images: %g\n", psnr);
489 ts->printf(ts->LOG, "Maximum allowed difference: %g\n", thresDbell);
490 ts->printf(ts->LOG, "Error: too big difference between saved and original images.\n");
500 void CV_HighGuiTest::SpecificVideoCameraTest(const string& dir, const char codecchars[4])
502 const string ext[] = {"avi", "mov", "mp4", "mpg", "wmv"};
504 const size_t n = sizeof(ext)/sizeof(ext[0]);
506 const int IMAGE_COUNT = 125;
508 cv::VideoCapture cap(0);
512 ts->printf(ts->LOG, "\nError: cannot start working with device.\n");
513 ts->set_failed_test_info(ts->OK);
517 for (size_t i = 0; i < n; ++i)
518 if ((ext[i]!="mp4")||(string(&codecchars[0], 4)!="IYUV"))
519 #if defined WIN32 || defined _WIN32
520 if (((ext[i]!="mov")||(string(&codecchars[0], 4)=="XVID"))&&(ext[i]!="mp4"))
523 Mat frame; int framecount = 0;
524 cv::VideoWriter writer;
526 std::vector <cv::Mat> tmp_img(IMAGE_COUNT);
528 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);
530 if (!writer.isOpened())
532 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
533 ts->printf(ts->LOG, "Video codec: %s\n", std::string(&codecchars[0], 4).c_str());
534 ts->printf(ts->LOG, "Error: cannot create VideoWriter object for video_%s.%s.\n", string(&codecchars[0]).c_str(), ext[i].c_str());
535 ts->set_failed_test_info(ts->FAIL_EXCEPTION);
545 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
546 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
547 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
548 ts->printf(ts->LOG, "Error: cannot read next frame with index %d from the device.\n", framecount);
552 cv::resize(frame, frame, Size(968, 757), 0, 0, INTER_CUBIC);
553 writer << frame; tmp_img[framecount] = frame;
556 if (framecount == IMAGE_COUNT) break;
560 writer.~VideoWriter();
562 cv::VideoCapture vcap(dir+"video_"+string(&codecchars[0], 4)+"."+ext[i]);
564 if (!vcap.isOpened())
566 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
567 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
568 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
569 ts->printf(ts->LOG, "Error: cannot open video file.\n");
573 int FRAME_COUNT = (int)vcap.get(CV_CAP_PROP_FRAME_COUNT);
575 if (FRAME_COUNT != IMAGE_COUNT)
577 ts->printf(ts->LOG, "\nChecking frame count...\n");
578 ts->printf(ts->LOG, "Video file directory: %s\n", dir.c_str());
579 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
580 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
581 ts->printf(ts->LOG, "Required frame count: %d Returned frame count: %d\n", IMAGE_COUNT, FRAME_COUNT);
582 ts->printf(ts->LOG, "Error: required and returned frame count are not matched.\n");
583 ts->printf(ts->LOG, "Continue checking...\n");
584 ts->set_failed_test_info(ts->FAIL_INVALID_OUTPUT);
587 cv::Mat img; framecount = 0;
588 vcap.set(CV_CAP_PROP_POS_FRAMES, 0);
590 for ( ; framecount < std::min<int>(FRAME_COUNT, IMAGE_COUNT); framecount++ )
596 ts->printf(ts->LOG, "\nVideo file directory: %s\n", dir.c_str());
597 ts->printf(ts->LOG, "File name: video_%s.%s\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
598 ts->printf(ts->LOG, "Video codec: %s\n", string(&codecchars[0], 4).c_str());
599 ts->printf(ts->LOG, "Error: cannot read frame with index %d from the video.\n", framecount);
603 const double thresDbell = 20;
604 double psnr = PSNR(img, tmp_img[framecount]);
606 if (psnr > thresDbell)
608 ts->printf(ts->LOG, "\nReading frame from the file video_%s.%s...\n", string(&codecchars[0], 4).c_str(), ext[i].c_str());
609 ts->printf(ts->LOG, "Frame index: %d\n", framecount);
610 ts->printf(ts->LOG, "Difference between saved and original images: %g\n", psnr);
611 ts->printf(ts->LOG, "Maximum allowed difference: %g\n", thresDbell);
612 ts->printf(ts->LOG, "Error: too big difference between saved and original images.\n");
618 vcap.~VideoCapture();
624 void CV_ImageTest::run(int)
626 ImageTest(ts->get_data_path());
629 void CV_SpecificImageTest::run(int)
631 SpecificImageTest(ts->get_data_path());
634 void CV_VideoTest::run(int)
636 #if defined WIN32 || (defined __linux__ && !defined ANDROID)
637 #if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
639 const char codecs[][4] = { {'I', 'Y', 'U', 'V'},
640 {'X', 'V', 'I', 'D'},
641 {'M', 'P', 'G', '2'},
642 {'M', 'J', 'P', 'G'} };
644 printf("%s", ts->get_data_path().c_str());
646 int count = sizeof(codecs)/(4*sizeof(char));
648 for (int i = 0; i < count; ++i)
650 VideoTest(ts->get_data_path(), CV_FOURCC(codecs[i][0], codecs[i][1], codecs[i][2], codecs[i][3]));
657 void CV_SpecificVideoFileTest::run(int)
659 #if defined WIN32 || (defined __linux__ && !defined ANDROID)
660 #if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
662 const char codecs[][4] = { {'M', 'P', 'G', '2'},
663 {'X', 'V', 'I', 'D'},
664 {'M', 'J', 'P', 'G'},
665 {'I', 'Y', 'U', 'V'} };
667 int count = sizeof(codecs)/(4*sizeof(char));
669 for (int i = 0; i < count; ++i)
671 SpecificVideoFileTest(ts->get_data_path(), codecs[i]);
678 void CV_SpecificVideoCameraTest::run(int)
680 #if defined WIN32 || (defined __linux__ && !defined ANDROID)
681 #if !defined HAVE_GSTREAMER || defined HAVE_GSTREAMER_APP
683 const char codecs[][4] = { {'M', 'P', 'G', '2'},
684 {'X', 'V', 'I', 'D'},
685 {'M', 'J', 'P', 'G'},
686 {'I', 'Y', 'U', 'V'} };
688 int count = sizeof(codecs)/(4*sizeof(char));
690 for (int i = 0; i < count; ++i)
692 SpecificVideoCameraTest(ts->get_data_path(), codecs[i]);
699 TEST(Highgui_Image, regression) { CV_ImageTest test; test.safe_run(); }
700 TEST(Highgui_Video, regression) { CV_VideoTest test; test.safe_run(); }
701 TEST(Highgui_SpecificImage, regression) { CV_SpecificImageTest test; test.safe_run(); }
702 TEST(Highgui_SpecificVideoFile, regression) { CV_SpecificVideoFileTest test; test.safe_run(); }
703 TEST(Highgui_SpecificVideoCamera, regression) { CV_SpecificVideoCameraTest test; test.safe_run(); }