Visual Servoing Platform  version 3.5.0 under development (2022-02-15)
testVideo.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2021 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Test video i/o.
33  *
34  *****************************************************************************/
35 
41 #include <visp3/core/vpConfig.h>
42 
43 #if defined(VISP_HAVE_CATCH2)
44 #define CATCH_CONFIG_RUNNER
45 #include <catch.hpp>
46 
47 #include <visp3/core/vpIoTools.h>
48 #include <visp3/io/vpVideoReader.h>
49 #include <visp3/io/vpVideoWriter.h>
50 
51 static long first_frame = 100;
52 static int frame_step = 2;
53 static unsigned int nframes = 3;
54 static long last_frame = first_frame + static_cast<long>(frame_step) * (nframes - 1);
55 static std::string tmp;
56 static std::string videoname_grey;
57 static std::string videoname_color;
58 
59 template <class Type>
60 bool test_createSequence(vpImage<Type> &I, const std::string &videoname, unsigned int first_frame, int frame_step, unsigned int nframes)
61 {
62  try {
63  vpVideoWriter writer;
64  writer.setFileName(videoname);
65  writer.setFirstFrameIndex(static_cast<int>(first_frame));
66  writer.setFrameStep(frame_step);
67  writer.open(I);
68 
69  for (unsigned int i = 0; i < nframes; i ++) {
70  writer.saveFrame(I);
71  std::cout << "Frame saved in: " << writer.getFrameName() << std::endl;
72  }
73  return true;
74  } catch (...) {
75  return false;
76  }
77 }
78 
79 template <class Type>
80 bool test_readSequence(vpImage<Type> &I, const std::string &videoname, long first_frame, int frame_step, int step, long last_frame)
81 {
82  vpVideoReader reader;
83  reader.setFileName(videoname);
84  reader.setFrameStep(step);
85  reader.open(I);
86 
87  long frame = reader.getFirstFrameIndex();
88  std::cout << "First frame: " << frame << std::endl;
89  if (frame != first_frame) {
90  std::cout << "Wrong first frame" << std::endl;
91  return false;
92  }
93  frame = reader.getLastFrameIndex();
94  std::cout << "Last frame: " << frame << std::endl;
95  if (frame != last_frame) {
96  std::cout << "Wrong last frame" << std::endl;
97  return false;
98  }
99 
100  long cpt = 0;
101  while (! reader.end()) {
102  reader.acquire(I);
103  long index = reader.getFrameIndex();
104  std::cout << "Read frame with index " << index << " from: " << reader.getFrameName() << std::endl;
105  if (index != first_frame + cpt * frame_step) {
106  std::cout << "Read wrong frame index" << std::endl;
107  return false;
108  }
109  cpt ++;
110  }
111  return true;
112 }
113 
114 TEST_CASE("Test saving sequence of uchar images with step 2", "[grey]")
115 {
116  std::cout << "** Create sequence of uchar images with step " << frame_step << std::endl;
117  vpImage<unsigned char> I(2, 4, 0);
118  CHECK(test_createSequence(I, videoname_grey, first_frame, frame_step, nframes));
119 }
120 
121 TEST_CASE("Test reading a sequence of grey images", "[grey]") {
122  SECTION("Read sequence of uchar images with step 1")
123  {
124  int step = 1;
126  CHECK(test_readSequence(I, videoname_grey, first_frame, frame_step, step, last_frame));
127  }
128 
129  SECTION("Read sequence of uchar images with step 2")
130  {
131  int step = frame_step;
133  CHECK(test_readSequence(I, videoname_grey, first_frame, frame_step, step, last_frame));
134  }
135 }
136 
137 TEST_CASE("Test saving sequence of color images with step 2", "[color]")
138 {
139  std::cout << "** Create sequence of color images with step " << frame_step << std::endl;
140  vpImage<vpRGBa> I(2, 4);
141  CHECK(test_createSequence(I, videoname_color, first_frame, frame_step, nframes));
142 }
143 
144 TEST_CASE("Test reading a sequence of color images", "[color]") {
145  SECTION("Read sequence of color images with step 1")
146  {
147  int step = 1;
148  vpImage<vpRGBa> I;
149  CHECK(test_readSequence(I, videoname_color, first_frame, frame_step, step, last_frame));
150  }
151 
152  SECTION("Read sequence of color images with step 2")
153  {
154  int step = frame_step;
155  vpImage<vpRGBa> I;
156  CHECK(test_readSequence(I, videoname_color, first_frame, frame_step, step, last_frame));
157  }
158 }
159 
160 int main(int argc, char *argv[])
161 {
162  Catch::Session session; // There must be exactly one instance
163 
164  // Let Catch (using Clara) parse the command line
165  session.applyCommandLine(argc, argv);
166 
167 #if 0 // Prepare next version using temp folder
168  tmp = vpIoTools::makeTempDirectory("./");
169 #endif
170  std::string username = vpIoTools::getUserName();
171 
172  // Create a temp folder to save velocities...
173 #if defined(_WIN32)
174  std::string tmp = "C:/temp/" + username + "/video";
175 #else
176  std::string tmp = "/tmp/" + username + "/video";
177 #endif
178  // Test if the output path exist. If no try to create it
179  if (vpIoTools::checkDirectory(tmp) == false) {
180  try {
181  // Create the dirname
183  } catch (...) {
184  std::cerr << std::endl << "ERROR:" << std::endl;
185  std::cerr << " Cannot create " << tmp << std::endl;
186  }
187  }
188 
189  std::cout << "** Create temp directory: " << tmp << std::endl;
190 
191  videoname_grey = tmp + std::string("/I%d.pgm");
192  videoname_color = tmp + std::string("/I%d.ppm");
193 
194  int numFailed = session.run();
195 
196  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
197  // This clamping has already been applied, so just return it here
198  // You can also do any post run clean-up here
199  return numFailed;
200 }
201 #else
202 int main()
203 {
204  return 0;
205 }
206 #endif
static void makeDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:570
void setFrameStep(const int frame_step)
void setFirstFrameIndex(int first_frame)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
long getFirstFrameIndex()
static bool checkDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:420
void open(vpImage< vpRGBa > &I)
long getLastFrameIndex()
static std::string getUserName()
Definition: vpIoTools.cpp:316
Class that enables to write easily a video file or a sequence of images.
void acquire(vpImage< vpRGBa > &I)
void saveFrame(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
static std::string makeTempDirectory(const std::string &dirname)
Definition: vpIoTools.cpp:728
long getFrameIndex() const
std::string getFrameName() const
void setFileName(const std::string &filename)
void setFrameStep(const long frame_step)
std::string getFrameName() const
void setFileName(const std::string &filename)
Definition of the vpImage class member functions.
Definition: vpImage.h:126