Visual Servoing Platform  version 3.6.1 under development (2024-11-15)
tutorial-mb-generic-tracker-read.cpp
1 #include <memory>
3 #include <visp3/core/vpConfig.h>
4 #include <visp3/core/vpIoTools.h>
5 #include <visp3/gui/vpDisplayFactory.h>
6 #include <visp3/io/vpImageIo.h>
7 #include <visp3/core/vpImageDraw.h>
8 
9 #if defined(VISP_HAVE_DISPLAY) && (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
10 namespace
11 {
12 // https://en.cppreference.com/w/cpp/io/c/fprintf
13 std::string toString(const std::string &name, int val)
14 {
15  auto fmt = name.c_str();
16  int sz = std::snprintf(nullptr, 0, fmt, val);
17  std::vector<char> buf(sz + 1); // note +1 for null terminator
18  std::sprintf(buf.data(), fmt, val);
19  std::string str(buf.begin(), buf.end());
20 
21  return str;
22 }
23 }
24 #endif
25 
26 int main(int argc, char *argv[])
27 {
28 #if defined(VISP_HAVE_DISPLAY) && defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
29 #ifdef ENABLE_VISP_NAMESPACE
30  using namespace VISP_NAMESPACE_NAME;
31 #endif
32 
33  bool opencv_backend = false;
34  std::string npz_filename = "npz_tracking_teabox.npz";
35  bool print_cMo = false;
36  bool dump_infos = false;
37 
38  for (int i = 1; i < argc; i++) {
39  if (std::string(argv[i]) == "--cv-backend") {
40  opencv_backend = true;
41  }
42  else if ((std::string(argv[i]) == "--read" || std::string(argv[i]) == "-i") && (i+1 < argc)) {
43  npz_filename = argv[i+1];
44  ++i;
45  }
46  else if (std::string(argv[i]) == "--print-cMo") {
47  print_cMo = true;
48  }
49  else if (std::string(argv[i]) == "--dump") {
50  dump_infos = true;
51  }
52  else {
53  std::cout << "Options:" << std::endl;
54  std::cout << " --cv-backend use OpenCV if available for in-memory PNG decoding" << std::endl;
55  std::cout << " --read / -i input filename in npz format" << std::endl;
56  std::cout << " --print-cMo print cMo" << std::endl;
57  std::cout << " --dump print all the data name in the file" << std::endl;
58  return EXIT_SUCCESS;
59  }
60  }
61 
62  std::cout << "Read file: " << npz_filename << std::endl;
63  std::cout << "OpenCV backend? " << opencv_backend << std::endl;
64 
65  const vpImageIo::vpImageIoBackendType backend =
67 
68  visp::cnpy::npz_t npz_data = visp::cnpy::npz_load(npz_filename);
69  if (dump_infos) {
70  std::cout << npz_filename << " file contains the following data:" << std::endl;
71  for (visp::cnpy::npz_t::const_iterator it = npz_data.begin(); it != npz_data.end(); ++it) {
72  std::cout << " " << it->first << std::endl;
73  }
74  }
75 
76  visp::cnpy::NpyArray arr_height = npz_data["height"];
77  visp::cnpy::NpyArray arr_width = npz_data["width"];
78  visp::cnpy::NpyArray arr_channel = npz_data["channel"];
79  int height = *arr_height.data<int>();
80  int width = *arr_width.data<int>();
81  int channel = *arr_channel.data<int>();
82  std::cout << "height: " << height << std::endl;
83  std::cout << "width: " << width << std::endl;
84  std::cout << "channel: " << channel << std::endl;
85  std::cout << "Color mode? " << (channel > 1) << std::endl;
86 
87  visp::cnpy::NpyArray arr_camera_name = npz_data["camera_name"];
88  // For null-terminated character handling, see:
89  // https://stackoverflow.com/a/8247804
90  // https://stackoverflow.com/a/45491652
91  std::vector<char> vec_arr_camera_name = arr_camera_name.as_vec<char>();
92  const std::string camera_name = std::string(vec_arr_camera_name.begin(), vec_arr_camera_name.end());
93  std::cout << "Camera name: " << camera_name << std::endl;
94 
95  visp::cnpy::NpyArray arr_px = npz_data["cam_px"];
96  visp::cnpy::NpyArray arr_py = npz_data["cam_py"];
97  visp::cnpy::NpyArray arr_u0 = npz_data["cam_u0"];
98  visp::cnpy::NpyArray arr_v0 = npz_data["cam_v0"];
99  vpCameraParameters cam(*arr_px.data<double>(), *arr_py.data<double>(), *arr_u0.data<double>(), *arr_v0.data<double>());
100  std::cout << "Cam: " << cam << std::endl;
101 
102  vpImage<unsigned char> I(height, width);
103  vpImage<vpRGBa> I_display(height, width);
104 
105 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11)
106  std::shared_ptr<vpDisplay> display = vpDisplayFactory::createDisplay();
107 #else
109 #endif
110  display->init(I_display, 100, 100, "Model-based tracker");
111 
112  visp::cnpy::NpyArray arr_nb_data = npz_data["nb_data"];
113  int nb_data = *arr_nb_data.data<int>();
114  std::cout << "Number of images: " << nb_data << std::endl;
115 
116  // Load all the images data
117  visp::cnpy::NpyArray arr_vec_img_data_size = npz_data["vec_img_data_size"];
118  int *vec_img_data_size_ptr = arr_vec_img_data_size.data<int>();
119  visp::cnpy::NpyArray arr_vec_img = npz_data["vec_img"];
120  unsigned char *vec_img_ptr = arr_vec_img.data<unsigned char>();
121  std::vector<unsigned char> vec_img;
122  size_t img_data_offset = 0;
123 
124  // Load all the poses
125  visp::cnpy::NpyArray arr_vec_poses = npz_data["vec_poses"];
126  double *vec_poses_ptr = arr_vec_poses.data<double>();
127  assert(arr_vec_poses.shape.size() == 2);
128  assert(arr_vec_poses.shape[1] == 6);
129  size_t pose_size = arr_vec_poses.shape[1];
130 
131  std::vector<double> times;
132 
133  for (int iter = 0; iter < nb_data; iter++) {
134  // std::copy(vec_img_ptr + img_data_offset, vec_img_ptr + img_data_offset + vec_img_data_size_ptr[iter],
135  // std::back_inserter(vec_img));
136  vec_img = std::vector<unsigned char>(vec_img_ptr + img_data_offset, vec_img_ptr + img_data_offset + vec_img_data_size_ptr[iter]);
137  double start = vpTime::measureTimeMs(), end = -1;
138  if (channel > 1) {
139  vpImageIo::readPNGfromMem(vec_img, I_display, backend);
140  end = vpTime::measureTimeMs();
141  }
142  else {
143  vpImageIo::readPNGfromMem(vec_img, I, backend);
144  end = vpTime::measureTimeMs();
145  vpImageConvert::convert(I, I_display);
146  }
147  times.push_back(end-start);
148  img_data_offset += vec_img_data_size_ptr[iter];
149 
150  const std::string str_model_iter_sz = toString("model_%06d", iter) + "_sz";
151  visp::cnpy::NpyArray arr_model_iter_sz = npz_data[str_model_iter_sz];
152  size_t model_sz = *arr_model_iter_sz.data<size_t>();
153 
154  for (size_t i = 0; i < model_sz; i++) {
155  char buffer[100];
156  int res = snprintf(buffer, 100, "model_%06d_%06zu", iter, i);
157  if (res > 0 && res < 100) {
158  std::string str_model_iter_data = buffer;
159  visp::cnpy::NpyArray arr_model_iter_data = npz_data[str_model_iter_data];
160 
161  if (arr_model_iter_data.shape[0] >= 5) {
162  if (std::fabs(arr_model_iter_data.data<double>()[0]) <= std::numeric_limits<double>::epsilon()) { // line feature
163  vpImageDraw::drawLine(I_display,
164  vpImagePoint(arr_model_iter_data.data<double>()[1], arr_model_iter_data.data<double>()[2]),
165  vpImagePoint(arr_model_iter_data.data<double>()[3], arr_model_iter_data.data<double>()[4]), vpColor::red, 3);
166  }
167  }
168  }
169  }
170 
171  vpHomogeneousMatrix cMo(vpTranslationVector(vec_poses_ptr[pose_size*iter], vec_poses_ptr[pose_size*iter + 1], vec_poses_ptr[pose_size*iter + 2]),
172  vpThetaUVector(vec_poses_ptr[pose_size*iter + 3], vec_poses_ptr[pose_size*iter + 4], vec_poses_ptr[pose_size*iter + 5])
173  );
174 
175  if (print_cMo) {
176  std::cout << "\ncMo:\n" << cMo << std::endl;
177  }
178 
179  vpDisplay::display(I_display);
180  vpDisplay::displayFrame(I_display, cMo, cam, 0.025, vpColor::none, 3);
181  vpDisplay::flush(I_display);
182 
183  vpTime::wait(30);
184  }
185 
186  std::cout << "Mean time for image decoding: " << vpMath::getMean(times) << " ms ; Median time: "
187  << vpMath::getMedian(times) << " ms ; Std: " << vpMath::getStdev(times) << " ms" << std::endl;
188 
189  vpDisplay::getClick(I_display, true);
190 #if (VISP_CXX_STANDARD < VISP_CXX_STANDARD_11)
191  delete display;
192 #endif
193 #else
194  (void)argc;
195  (void)argv;
196  std::cerr << "Error, a missing display library is needed (X11, GDI or OpenCV built with HighGUI module)." << std::endl;
197 #ifndef VISP_HAVE_MINIZ
198  std::cerr << "You also need to enable npz I/O functions" << std::endl;
199 #endif
200 #endif
201 
202  return EXIT_SUCCESS;
203 }
Generic class defining intrinsic camera parameters.
static const vpColor red
Definition: vpColor.h:217
static const vpColor none
Definition: vpColor.h:229
Class that defines generic functionalities for display.
Definition: vpDisplay.h:178
static bool getClick(const vpImage< unsigned char > &I, bool blocking=true)
static void display(const vpImage< unsigned char > &I)
static void displayFrame(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, double size, const vpColor &color=vpColor::none, unsigned int thickness=1, const vpImagePoint &offset=vpImagePoint(0, 0), const std::string &frameName="", const vpColor &textColor=vpColor::black, const vpImagePoint &textOffset=vpImagePoint(15, 15))
static void flush(const vpImage< unsigned char > &I)
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
static void drawLine(vpImage< unsigned char > &I, const vpImagePoint &ip1, const vpImagePoint &ip2, unsigned char color, unsigned int thickness=1)
vpImageIoBackendType
Image IO backend for only jpeg and png formats image loading and saving.
Definition: vpImageIo.h:128
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
Definition: vpImageIo.h:133
@ IO_OPENCV_BACKEND
Use OpenCV imgcodecs module.
Definition: vpImageIo.h:131
static void readPNGfromMem(const std::vector< unsigned char > &buffer, vpImage< unsigned char > &I, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:1409
Class that defines a 2D point in an image. This class is useful for image processing and stores only ...
Definition: vpImagePoint.h:82
static double getMedian(const std::vector< double > &v)
Definition: vpMath.cpp:322
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition: vpMath.cpp:353
static double getMean(const std::vector< double > &v)
Definition: vpMath.cpp:302
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
VISP_EXPORT npz_t npz_load(std::string fname)
std::map< std::string, NpyArray > npz_t
Definition: vpIoTools.h:130
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.
VISP_EXPORT int wait(double t0, double t)
VISP_EXPORT double measureTimeMs()
std::vector< size_t > shape
Definition: vpIoTools.h:124
std::vector< T > as_vec() const
Definition: vpIoTools.h:112