File: catchVideo.cpp

package info (click to toggle)
visp 3.7.0-7
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 166,380 kB
  • sloc: cpp: 392,705; ansic: 224,448; xml: 23,444; python: 13,701; java: 4,792; sh: 206; objc: 145; makefile: 118
file content (204 lines) | stat: -rw-r--r-- 6,142 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
 * ViSP, open source Visual Servoing Platform software.
 * Copyright (C) 2005 - 2025 by Inria. All rights reserved.
 *
 * This software is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * See the file LICENSE.txt at the root directory of this source
 * distribution for additional information about the GNU GPL.
 *
 * For using ViSP with software that can not be combined with the GNU
 * GPL, please contact Inria about acquiring a ViSP Professional
 * Edition License.
 *
 * See https://visp.inria.fr for more information.
 *
 * This software was developed at:
 * Inria Rennes - Bretagne Atlantique
 * Campus Universitaire de Beaulieu
 * 35042 Rennes Cedex
 * France
 *
 * If you have questions regarding the use of this file, please contact
 * Inria at visp@inria.fr
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Description:
 * Test video i/o.
 */

/*!
  \file catchVideo.cpp
  \brief Test vpVideoReader and vpVideoWriter classes.
 */

#include <visp3/core/vpConfig.h>

#if defined(VISP_HAVE_CATCH2)

#include <catch_amalgamated.hpp>

#include <visp3/core/vpIoTools.h>
#include <visp3/io/vpVideoReader.h>
#include <visp3/io/vpVideoWriter.h>

#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif

static long first_frame = 100;
static int frame_step = 2;
static unsigned int nframes = 3;
static long last_frame = first_frame + static_cast<long>(frame_step) * (nframes - 1);
VP_ATTRIBUTE_NO_DESTROY static std::string tmp;
VP_ATTRIBUTE_NO_DESTROY static std::string videoname_grey;
VP_ATTRIBUTE_NO_DESTROY static std::string videoname_color;

template <class Type>
bool test_createSequence(vpImage<Type> &I, const std::string &videoname, unsigned int first_frame, int frame_step,
                         unsigned int nframes)
{
  try {
    vpVideoWriter writer;
    writer.setFileName(videoname);
    writer.setFirstFrameIndex(static_cast<int>(first_frame));
    writer.setFrameStep(frame_step);
    writer.open(I);

    for (unsigned int i = 0; i < nframes; i++) {
      writer.saveFrame(I);
      std::cout << "Frame saved in: " << writer.getFrameName() << std::endl;
    }
    return true;
  }
  catch (...) {
    return false;
  }
}

template <class Type>
bool test_readSequence(vpImage<Type> &I, const std::string &videoname, long first_frame, int frame_step, int step,
                       long last_frame)
{
  vpVideoReader reader;
  reader.setFileName(videoname);
  reader.setFrameStep(step);
  reader.open(I);

  long frame = reader.getFirstFrameIndex();
  std::cout << "First frame: " << frame << std::endl;
  if (frame != first_frame) {
    std::cout << "Wrong first frame" << std::endl;
    return false;
  }
  frame = reader.getLastFrameIndex();
  std::cout << "Last frame: " << frame << std::endl;
  if (frame != last_frame) {
    std::cout << "Wrong last frame" << std::endl;
    return false;
  }

  long cpt = 0;
  while (!reader.end()) {
    reader.acquire(I);
    long index = reader.getFrameIndex();
    std::cout << "Read frame with index " << index << " from: " << reader.getFrameName() << std::endl;
    if (index != first_frame + cpt * frame_step) {
      std::cout << "Read wrong frame index" << std::endl;
      return false;
    }
    cpt++;
  }
  return true;
}

TEST_CASE("Test saving sequence of uchar images with step 2", "[grey]")
{
  std::cout << "** Create sequence of uchar images with step " << frame_step << std::endl;
  vpImage<unsigned char> I(2, 4, 0);
  CHECK(test_createSequence(I, videoname_grey, first_frame, frame_step, nframes));
}

TEST_CASE("Test reading a sequence of grey images", "[grey]")
{
  SECTION("Read sequence of uchar images with step 1")
  {
    int step = 1;
    vpImage<unsigned char> I;
    CHECK(test_readSequence(I, videoname_grey, first_frame, frame_step, step, last_frame));
  }

  SECTION("Read sequence of uchar images with step 2")
  {
    int step = frame_step;
    vpImage<unsigned char> I;
    CHECK(test_readSequence(I, videoname_grey, first_frame, frame_step, step, last_frame));
  }
}

TEST_CASE("Test saving sequence of color images with step 2", "[color]")
{
  std::cout << "** Create sequence of color images with step " << frame_step << std::endl;
  vpImage<vpRGBa> I(2, 4);
  CHECK(test_createSequence(I, videoname_color, first_frame, frame_step, nframes));
}

TEST_CASE("Test reading a sequence of color images", "[color]")
{
  SECTION("Read sequence of color images with step 1")
  {
    int step = 1;
    vpImage<vpRGBa> I;
    CHECK(test_readSequence(I, videoname_color, first_frame, frame_step, step, last_frame));
  }

  SECTION("Read sequence of color images with step 2")
  {
    int step = frame_step;
    vpImage<vpRGBa> I;
    CHECK(test_readSequence(I, videoname_color, first_frame, frame_step, step, last_frame));
  }
}

int main(int argc, char *argv[])
{
  Catch::Session session; // There must be exactly one instance

  // Let Catch (using Clara) parse the command line
  session.applyCommandLine(argc, argv);

  std::string tmp = vpIoTools::makeTempDirectory("./");

  std::string username = vpIoTools::getUserName();

  // Test if the output path exist. If no try to create it
  if (vpIoTools::checkDirectory(tmp) == false) {
    try {
      // Create the dirname
      vpIoTools::makeDirectory(tmp);
    }
    catch (...) {
      std::cerr << std::endl << "ERROR:" << std::endl;
      std::cerr << "  Cannot create " << tmp << std::endl;
    }
  }

  std::cout << "** Create temp directory: " << tmp << std::endl;

  videoname_grey = tmp + std::string("/I%d.pgm");
  videoname_color = tmp + std::string("/I%d.ppm");

  int numFailed = session.run();

  // numFailed is clamped to 255 as some unices only use the lower 8 bits.
  // This clamping has already been applied, so just return it here
  // You can also do any post run clean-up here
  return numFailed;
}
#else
int main() { return EXIT_SUCCESS; }
#endif