Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
videoReader.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 a video file.
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: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, 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 fprintf(stdout, "\n\
79Read a video file on the disk.\n\
80\n\
81SYNOPSIS\n\
82 %s [-i <input video path>] \n\
83 [-h]\n\
84",
85name);
86
87 fprintf(stdout, "\n\
88OPTIONS: Default\n\
89 -i <input video path> %s\n\
90 Set video input path.\n\
91 From this path read \"video/cube.mpeg\"\n\
92 video.\n\
93 Setting the VISP_INPUT_IMAGE_PATH environment\n\
94 variable produces the same behaviour than using\n\
95 this option.\n\
96\n\
97 -p <personal video path> %s\n\
98 Specify a personal folder containing a video \n\
99 to process.\n\
100 Example : \"/Temp/visp-images/video/video.mpeg\"\n\
101\n\
102 -c\n\
103 Disable the mouse click. Useful to automate the \n\
104 execution of this program without human intervention.\n\
105\n\
106 -d \n\
107 Turn off the display.\n\
108\n\
109 -h\n\
110 Print the help.\n\n",
111 ipath.c_str(), ppath.c_str());
112
113 if (badparam) {
114 fprintf(stderr, "ERROR: \n");
115 fprintf(stderr, "\nBad parameter [%s]\n", badparam);
116 }
117}
131bool getOptions(int argc, const char **argv, std::string &ipath, std::string &ppath, bool &click_allowed, bool &display)
132{
133 const char *optarg_;
134 int c;
135 while ((c = vpParseArgv::parse(argc, argv, GETOPTARGS, &optarg_)) > 1) {
136
137 switch (c) {
138 case 'c':
139 click_allowed = false;
140 break;
141 case 'd':
142 display = false;
143 break;
144 case 'i':
145 ipath = optarg_;
146 break;
147 case 'p':
148 ppath = optarg_;
149 break;
150 case 'h':
151 usage(argv[0], nullptr, ipath, ppath);
152 return false;
153
154 default:
155 usage(argv[0], optarg_, ipath, ppath);
156 return false;
157 }
158 }
159
160 if ((c == 1) || (c == -1)) {
161 // standalone param or error
162 usage(argv[0], nullptr, ipath, ppath);
163 std::cerr << "ERROR: " << std::endl;
164 std::cerr << " Bad argument " << optarg_ << std::endl << std::endl;
165 return false;
166 }
167
168 return true;
169}
170
171int main(int argc, const char **argv)
172{
173#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
174 std::shared_ptr<vpDisplay> display;
175#else
176 vpDisplay *display = nullptr;
177#endif
178 try {
179 std::string env_ipath;
180 std::string opt_ipath;
181 std::string ipath;
182 std::string opt_ppath;
183 std::string filename;
184 bool opt_click_allowed = true;
185 bool opt_display = true;
186
187 std::cout << "-------------------------------------------------------" << std::endl;
188 std::cout << " videoReader.cpp" << std::endl << std::endl;
189
190 std::cout << " reading a video file" << std::endl;
191 std::cout << "-------------------------------------------------------" << std::endl;
192 std::cout << std::endl;
193
194 // Get the visp-images-data package path or VISP_INPUT_IMAGE_PATH
195 // environment variable value
197
198 // Set the default input path
199 if (!env_ipath.empty())
200 ipath = env_ipath;
201
202 // Read the command line options
203 if (getOptions(argc, argv, opt_ipath, opt_ppath, opt_click_allowed, opt_display) == false) {
204 return EXIT_FAILURE;
205 }
206
207 // Get the option values
208 if (!opt_ipath.empty())
209 ipath = opt_ipath;
210
211 // Compare ipath and env_ipath. If they differ, we take into account
212 // the input path coming from the command line option
213 if (!opt_ipath.empty() && !env_ipath.empty() && opt_ppath.empty()) {
214 if (ipath != env_ipath) {
215 std::cout << std::endl << "WARNING: " << std::endl;
216 std::cout << " Since -i <visp image path=" << ipath << "> "
217 << " is different from VISP_IMAGE_PATH=" << env_ipath << std::endl
218 << " we skip the environment variable." << std::endl;
219 }
220 }
221
222 // Test if an input path is set
223 if (opt_ipath.empty() && env_ipath.empty() && opt_ppath.empty()) {
224 usage(argv[0], nullptr, ipath, opt_ppath);
225 std::cerr << std::endl << "ERROR:" << std::endl;
226 std::cerr << " Use -i <visp image path> option or set VISP_INPUT_IMAGE_PATH " << std::endl
227 << " environment variable to specify the location of the " << std::endl
228 << " video path where test images are located." << std::endl
229 << std::endl;
230 return EXIT_FAILURE;
231 }
232
234
235 // vpImage is a template class you can declare vpImage of ...
236 // everything...
238
239 // Create the video Reader
240 vpVideoReader reader;
241
242#if defined(VISP_HAVE_DATASET)
243#if VISP_HAVE_DATASET_VERSION >= 0x030702
244 std::string video("video/cube.mp4");
245#else
246 std::string video("video/cube.mpeg");
247#endif
248#else
249 // We suppose that the user will download a recent dataset
250 std::string video("video/cube.mp4");
251#endif
252
253 if (opt_ppath.empty()) {
254 filename = vpIoTools::createFilePath(ipath, video);
255 }
256 else {
257 filename.assign(opt_ppath);
258 }
259
260 // Initialize the reader and get the first frame.
261 std::cout << "Process video in " << filename << std::endl;
262 reader.setFileName(filename);
263 reader.open(I);
264
265 if (opt_display) {
266 // Display size is automatically defined by the image (I) size
267#if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
268 display = vpDisplayFactory::createDisplay(I, 100, 100, "Display video frame");
269#else
270 display = vpDisplayFactory::allocateDisplay(I, 100, 100, "Display video frame");
271#endif
274 }
275
276 // if (opt_display && opt_click_allowed)
277 // {
278 // std::cout << "Click on the image to read and display the last key
279 // frame" << std::endl; vpDisplay::getClick(I);
280 // }
281 //
282 // reader.getFrame(I,reader.getLastFrameIndex());
283 //
284 // if (opt_display)
285 // {
286 // vpDisplay::display(I) ;
287 // vpDisplay::flush(I);
288 // }
289
290 if (opt_display && opt_click_allowed) {
291 std::cout << "Click to see the video" << std::endl;
293 }
294
295 while (!reader.end()) {
296 reader.acquire(I);
297 std::cout << "Display frame: " << reader.getFrameIndex() << std::endl;
298 if (opt_display) {
300 if (opt_click_allowed) {
301 vpDisplay::displayText(I, 15, 15, "A click to stop...", vpColor::red);
302
303 if (vpDisplay::getClick(I, false)) {
304 break;
305 }
306 }
308 }
309 }
310
311 if (opt_display && opt_click_allowed) {
312 std::cout << "Click to exit this example" << std::endl;
314 }
315 }
316 catch (const vpException &e) {
317 std::cout << "Catch an exception: " << e << std::endl;
318 }
319#if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
320 if (display != nullptr) {
321 delete display;
322 }
323#endif
324 return EXIT_SUCCESS;
325}
326#else
327int main()
328{
329 std::cout << "Sorry, no display is available. We quit this example." << std::endl;
330 return EXIT_SUCCESS;
331}
332#endif
static const vpColor red
Definition vpColor.h:198
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)
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
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...
void open(vpImage< vpRGBa > &I) VP_OVERRIDE
void setFileName(const std::string &filename)
long getFrameIndex() const
void acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
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.