Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
imageSequenceReader.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 * Reading an image sequence.
32 */
33
38
43
44#include <visp3/core/vpConfig.h>
45#include <visp3/core/vpDebug.h>
46#include <visp3/core/vpImage.h>
47#include <visp3/core/vpIoTools.h>
48#include <visp3/gui/vpDisplayFactory.h>
49#include <visp3/io/vpImageIo.h>
50#include <visp3/io/vpParseArgv.h>
51#include <visp3/io/vpVideoReader.h>
52
53#if defined(VISP_HAVE_DISPLAY)
54
55 // List of allowed command line options
56#define GETOPTARGS "cdi:p:f:h"
57
58#ifdef ENABLE_VISP_NAMESPACE
59using namespace VISP_NAMESPACE_NAME;
60#endif
61
62void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &ppath);
63bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, int &first, bool &click_allowed,
64 bool &display);
65
76void usage(const char *name, const char *badparam, const std::string &ipath, const std::string &ppath)
77{
78#if defined(VISP_HAVE_DATASET)
79#if VISP_HAVE_DATASET_VERSION >= 0x030600
80 std::string ext("png");
81#else
82 std::string ext("pgm");
83#endif
84#else
85 // We suppose that the user will download a recent dataset
86 std::string ext("png");
87#endif
88 fprintf(stdout, "\n\
89Read an image sequence on the disk.\n\
90\n\
91SYNOPSIS\n\
92 %s [-i <input images path>] [-p <personal image sequence path>]\n\
93 [-c][-d][-h]\n\
94",
95name);
96
97 fprintf(stdout, "\n\
98OPTIONS: Default\n\
99 -i <input images path> %s\n\
100 Set ViSP-images input path.\n\
101 From this path read \"cube/image.%%04d.%s\"\n\
102 images.\n\
103 Setting the VISP_INPUT_IMAGE_PATH environment\n\
104 variable produces the same behaviour than using\n\
105 this option.\n\
106\n\
107 -p <personal image sequence path> %s\n\
108 Specify a personal folder containing an image sequence \n\
109 to process.\n\
110 Example : \"/Temp/visp-images/cube/image.%%04d.%s\"\n\
111 %%04d is for the image numbering.\n\
112\n\
113 -f <index of the first frame> \n\
114 Specify the first image index.\n\
115\n\
116 -c\n\
117 Disable the mouse click. Useful to automate the \n\
118 execution of this program without human intervention.\n\
119\n\
120 -d \n\
121 Turn off the display.\n\
122\n\
123 -h\n\
124 Print the help.\n\n",
125 ipath.c_str(), ext.c_str(), ppath.c_str(), ext.c_str());
126
127 if (badparam) {
128 fprintf(stderr, "ERROR: \n");
129 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
130 }
131}
146bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, int &first, bool &click_allowed,
147 bool &display)
148{
149 const char *optarg_;
150 int c;
151 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
152
153 switch (c) {
154 case 'c':
155 click_allowed = false;
156 break;
157 case 'd':
158 display = false;
159 break;
160 case 'i':
161 ipath = optarg_;
162 break;
163 case 'p':
164 ppath = optarg_;
165 break;
166 case 'f':
167 first = atoi(optarg_);
168 break;
169 case 'h':
170 usage(argv[0], nullptr, ipath, ppath);
171 return false;
172
173 default:
174 usage(argv[0], optarg_, ipath, ppath);
175 return false;
176 }
177 }
178
179 if ((c == 1) || (c == -1)) {
180 // standalone param or error
181 usage(argv[0], nullptr, ipath, ppath);
182 std::cerr << "ERROR: " << std::endl;
183 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
184 return false;
185 }
186
187 return true;
188}
189
190int main(int argc, const char **argv)
191{
192#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
193 std::shared_ptr<vpDisplay> display;
194#else
195 vpDisplay *display = nullptr;
196#endif
197 try {
198 std::string env_ipath;
199 std::string opt_ipath;
200 std::string ipath;
201 std::string opt_ppath;
202 std::string filename;
203 int opt_first = 1;
204 bool opt_click_allowed = true;
205 bool opt_display = true;
206
207#if defined(VISP_HAVE_DATASET)
208#if VISP_HAVE_DATASET_VERSION >= 0x030600
209 std::string ext("png");
210#else
211 std::string ext("pgm");
212#endif
213#else
214 // We suppose that the user will download a recent dataset
215 std::string ext("png");
216#endif
217
218 std::cout << "-------------------------------------------------------" << std::endl;
219 std::cout << " videoImageSequenceReader.cpp" << std::endl << std::endl;
220
221 std::cout << " reading an image sequence" << std::endl;
222 std::cout << "-------------------------------------------------------" << std::endl;
223 std::cout << std::endl;
224
225 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
226 // environment variable value
228
229 // Set the default input path
230 if (!env_ipath.empty())
231 ipath = env_ipath;
232
233 // Read the command line options
234 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_first, opt_click_allowed, opt_display) == false) {
235 return EXIT_FAILURE;
236 }
237
238 // Get the option values
239 if (!opt_ipath.empty())
240 ipath = opt_ipath;
241
242 // Compare ipath and env_ipath. If they differ, we take into account
243 // the input path coming from the command line option
244 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
245 if (ipath != env_ipath) {
246 std::cout << std::endl << "WARNING: " << std::endl;
247 std::cout << " Since -i <visp image path=" << ipath << "> "
248 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
249 << " we skip the environment variable." << std::endl;
250 }
251 }
252
253 // Test if an input path is set
254 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
255 usage(argv[0], nullptr, ipath, opt_ppath);
256 std::cerr << std::endl << "ERROR:" << std::endl;
257 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
258 << " environment variable to specify the location of the " << std::endl
259 << " video path where test images are located." << std::endl
260 << std::endl;
261 return EXIT_FAILURE;
262 }
263
265
266 // vpImage is a template class you can declare vpImage of ...
267 // everything...
269
270 // Create the video Reader
271 vpVideoReader reader;
272
273 if (opt_ppath.empty()) {
274 filename = vpIoTools::createFilePath(ipath, "mire-2/image.%04d." + ext);
275 }
276 else {
277 filename.assign(opt_ppath);
278 }
279
280 // Initialize the reader and get the first frame.
281 reader.setFileName(filename);
282 reader.setFirstFrameIndex(opt_first);
283 reader.open(I);
284 std::cout << "Current image number: " << reader.getFrameIndex() << std::endl;
285 if ((opt_first) != reader.getFrameIndex()) {
286 std::cout << "Unable to get requested image number: " << opt_first << std::endl;
287 return EXIT_FAILURE;
288 }
289
290 if (opt_display) {
291 // Display size is automatically defined by the image (I) size
292#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
293 display = vpDisplayFactory::createDisplay(I, 100, 100, "Display video frame");
294#else
295 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Display video frame");
296#endif
299 }
300
301 if (opt_display && opt_click_allowed) {
302 std::cout << "Click in the image to read and display the second frame" << std::endl;
304 }
305
306 reader.getFrame(I, opt_first + 1);
307 std::cout << "Current image number (should be " << opt_first + 1 << "): " << reader.getFrameIndex() << std::endl;
308 if ((opt_first + 1) != reader.getFrameIndex()) {
309 std::cout << "Unable to get requested image number: " << opt_first + 1 << std::endl;
310#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
311 if (display != nullptr) {
312 delete display;
313 }
314#endif
315 return EXIT_FAILURE;
316 }
317
318 if (opt_display) {
321 }
322
323 if (opt_display && opt_click_allowed) {
324 std::cout << "Click on the image to read and display the last frame" << std::endl;
326 }
327
328 reader.getFrame(I, reader.getLastFrameIndex());
329 std::cout << "Current image number (should be " << reader.getLastFrameIndex() << "): " << reader.getFrameIndex()
330 << std::endl;
331
332 if (opt_display) {
335 }
336
337 if (opt_display && opt_click_allowed) {
338 std::cout << "Click to see the video" << std::endl;
340 }
341
342 int lastFrame = reader.getLastFrameIndex();
343
344 for (int i = opt_first; i <= lastFrame; i++) {
345 reader.getFrame(I, i);
346 std::cout << "Current image number: " << reader.getFrameIndex() << std::endl;
347 if (opt_display) {
350 }
351 }
352
353 if (opt_display && opt_click_allowed) {
354 std::cout << "Click to exit the test" << std::endl;
356 }
357
358#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
359 if (display != nullptr) {
360 delete display;
361 }
362#endif
363 return EXIT_SUCCESS;
364 }
365 catch (const vpException &e) {
366 std::cout << "Catch an exception: " << e << std::endl;
367#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
368 if (display != nullptr) {
369 delete display;
370 }
371#endif
372 return EXIT_FAILURE;
373 }
374}
375#else
376int main()
377{
378 std::cout << "Sorry, no display is available. We quit this example." << std::endl;
379 return EXIT_SUCCESS;
380}
381#endif
Class that defines generic functionalities for display.
Definition vpDisplay.h:171
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void flush(const vpImage< unsigned char > &I)
error that can be emitted by ViSP classes.
Definition vpException.h:60
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string getViSPImagesDataPath()
static std::string createFilePath(const std::string &parent, const std::string &child)
static bool parse(int *argcPtr, const char **argv, vpArgvInfo *argTable, int flags)
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)
void setFirstFrameIndex(const long first_frame)
bool getFrame(vpImage< vpRGBa > &I, long frame)
long getFrameIndex() const
std::shared_ptr< vpDisplay > createDisplay()
Return a smart pointer vpDisplay specialization if a GUI library is available or nullptr otherwise.
vpDisplay * allocateDisplay()
Return a newly allocated vpDisplay specialization if a GUI library is available or nullptr otherwise.