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