Visual Servoing Platform version 3.6.0
Loading...
Searching...
No Matches
testVideo.cpp
1/****************************************************************************
2 *
3 * ViSP, open source Visual Servoing Platform software.
4 * Copyright (C) 2005 - 2023 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 https://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
51static long first_frame = 100;
52static int frame_step = 2;
53static unsigned int nframes = 3;
54static long last_frame = first_frame + static_cast<long>(frame_step) * (nframes - 1);
55static std::string tmp;
56static std::string videoname_grey;
57static std::string videoname_color;
58
59template <class Type>
60bool test_createSequence(vpImage<Type> &I, const std::string &videoname, unsigned int first_frame, int frame_step,
61 unsigned int nframes)
62{
63 try {
64 vpVideoWriter writer;
65 writer.setFileName(videoname);
66 writer.setFirstFrameIndex(static_cast<int>(first_frame));
67 writer.setFrameStep(frame_step);
68 writer.open(I);
69
70 for (unsigned int i = 0; i < nframes; i++) {
71 writer.saveFrame(I);
72 std::cout << "Frame saved in: " << writer.getFrameName() << std::endl;
73 }
74 return true;
75 } catch (...) {
76 return false;
77 }
78}
79
80template <class Type>
81bool test_readSequence(vpImage<Type> &I, const std::string &videoname, long first_frame, int frame_step, int step,
82 long last_frame)
83{
84 vpVideoReader reader;
85 reader.setFileName(videoname);
86 reader.setFrameStep(step);
87 reader.open(I);
88
89 long frame = reader.getFirstFrameIndex();
90 std::cout << "First frame: " << frame << std::endl;
91 if (frame != first_frame) {
92 std::cout << "Wrong first frame" << std::endl;
93 return false;
94 }
95 frame = reader.getLastFrameIndex();
96 std::cout << "Last frame: " << frame << std::endl;
97 if (frame != last_frame) {
98 std::cout << "Wrong last frame" << std::endl;
99 return false;
100 }
101
102 long cpt = 0;
103 while (!reader.end()) {
104 reader.acquire(I);
105 long index = reader.getFrameIndex();
106 std::cout << "Read frame with index " << index << " from: " << reader.getFrameName() << std::endl;
107 if (index != first_frame + cpt * frame_step) {
108 std::cout << "Read wrong frame index" << std::endl;
109 return false;
110 }
111 cpt++;
112 }
113 return true;
114}
115
116TEST_CASE("Test saving sequence of uchar images with step 2", "[grey]")
117{
118 std::cout << "** Create sequence of uchar images with step " << frame_step << std::endl;
119 vpImage<unsigned char> I(2, 4, 0);
120 CHECK(test_createSequence(I, videoname_grey, first_frame, frame_step, nframes));
121}
122
123TEST_CASE("Test reading a sequence of grey images", "[grey]")
124{
125 SECTION("Read sequence of uchar images with step 1")
126 {
127 int step = 1;
129 CHECK(test_readSequence(I, videoname_grey, first_frame, frame_step, step, last_frame));
130 }
131
132 SECTION("Read sequence of uchar images with step 2")
133 {
134 int step = frame_step;
136 CHECK(test_readSequence(I, videoname_grey, first_frame, frame_step, step, last_frame));
137 }
138}
139
140TEST_CASE("Test saving sequence of color images with step 2", "[color]")
141{
142 std::cout << "** Create sequence of color images with step " << frame_step << std::endl;
143 vpImage<vpRGBa> I(2, 4);
144 CHECK(test_createSequence(I, videoname_color, first_frame, frame_step, nframes));
145}
146
147TEST_CASE("Test reading a sequence of color images", "[color]")
148{
149 SECTION("Read sequence of color images with step 1")
150 {
151 int step = 1;
153 CHECK(test_readSequence(I, videoname_color, first_frame, frame_step, step, last_frame));
154 }
155
156 SECTION("Read sequence of color images with step 2")
157 {
158 int step = frame_step;
160 CHECK(test_readSequence(I, videoname_color, first_frame, frame_step, step, last_frame));
161 }
162}
163
164int main(int argc, char *argv[])
165{
166 Catch::Session session; // There must be exactly one instance
167
168 // Let Catch (using Clara) parse the command line
169 session.applyCommandLine(argc, argv);
170
171 std::string tmp = vpIoTools::makeTempDirectory("./");
172
173 std::string username = vpIoTools::getUserName();
174
175 // Test if the output path exist. If no try to create it
176 if (vpIoTools::checkDirectory(tmp) == false) {
177 try {
178 // Create the dirname
180 } catch (...) {
181 std::cerr << std::endl << "ERROR:" << std::endl;
182 std::cerr << " Cannot create " << tmp << std::endl;
183 }
184 }
185
186 std::cout << "** Create temp directory: " << tmp << std::endl;
187
188 videoname_grey = tmp + std::string("/I%d.pgm");
189 videoname_color = tmp + std::string("/I%d.ppm");
190
191 int numFailed = session.run();
192
193 // numFailed is clamped to 255 as some unices only use the lower 8 bits.
194 // This clamping has already been applied, so just return it here
195 // You can also do any post run clean-up here
196 return numFailed;
197}
198#else
199int main() { return EXIT_SUCCESS; }
200#endif
Definition of the vpImage class member functions.
Definition vpImage.h:135
static bool checkDirectory(const std::string &dirname)
static std::string getUserName()
static void makeDirectory(const std::string &dirname)
static std::string makeTempDirectory(const std::string &dirname)
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void acquire(vpImage< vpRGBa > &I)
long getLastFrameIndex()
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
long getFirstFrameIndex()
void setFrameStep(const long frame_step)
std::string getFrameName() const
long getFrameIndex() const
Class that enables to write easily a video file or a sequence of images.
void saveFrame(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void setFrameStep(const int frame_step)
void open(vpImage< vpRGBa > &I)
void setFirstFrameIndex(int first_frame)
std::string getFrameName() const