Visual Servoing Platform version 3.7.0
Loading...
Searching...
No Matches
tutorial-mb-generic-tracker-save.cpp
1
2#include <visp3/core/vpConfig.h>
3#include <visp3/core/vpIoTools.h>
4#include <visp3/io/vpImageIo.h>
5#include <visp3/mbt/vpMbGenericTracker.h>
6#include <visp3/io/vpVideoReader.h>
7
8#if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
9#ifdef ENABLE_VISP_NAMESPACE
10using namespace VISP_NAMESPACE_NAME;
11#endif
12
13namespace
14{
15std::vector<double> poseToVec(const vpHomogeneousMatrix &cMo)
16{
17 vpThetaUVector tu = cMo.getThetaUVector();
18 vpTranslationVector t = cMo.getTranslationVector();
19
20 std::vector<double> vec { t[0], t[1], t[2], tu[0], tu[1], tu[2] };
21
22 return vec;
23}
24}
25
26int main(int argc, char **argv)
27{
28 bool opencv_backend = false;
29 std::string npz_filename = "npz_tracking_teabox.npz";
30 bool color_mode = false;
31 bool save_alpha = false;
32 bool print_cMo = false;
33
34 for (int i = 1; i < argc; i++) {
35 if (std::string(argv[i]) == "--cv-backend") {
36 opencv_backend = true;
37 }
38 else if ((std::string(argv[i]) == "--save" || std::string(argv[i]) == "-o") && (i+1 < argc)) {
39 npz_filename = argv[i+1];
40 ++i;
41 }
42 else if (std::string(argv[i]) == "--color" || std::string(argv[i]) == "-c") {
43 color_mode = true;
44 }
45 else if (std::string(argv[i]) == "--alpha" || std::string(argv[i]) == "-a") {
46 save_alpha = true;
47 }
48 else if (std::string(argv[i]) == "--print-cMo") {
49 print_cMo = true;
50 }
51 else {
52 std::cout << "Options:" << std::endl;
53 std::cout << " --cv-backend use OpenCV if available for in-memory PNG encoding" << std::endl;
54 std::cout << " --save / -o output filename" << std::endl;
55 std::cout << " --color save RGB data" << std::endl;
56 std::cout << " --alpha if --color opton, save RGBa data" << std::endl;
57 std::cout << " --print-cMo print cMo" << std::endl;
58 return EXIT_SUCCESS;
59 }
60 }
61
62 std::cout << "Save file: " << npz_filename << std::endl;
63 std::cout << "OpenCV backend? " << opencv_backend << std::endl;
64 std::cout << "Color image? " << color_mode << std::endl;
65 std::cout << "Save alpha channel? " << save_alpha << std::endl;
66
67 std::string opt_videoname = "model/teabox/teabox.mp4";
68 std::string opt_modelname = "model/teabox/teabox.cao";
69
70 std::string parentname = vpIoTools::getParent(opt_modelname);
71 std::string objectname = vpIoTools::getNameWE(opt_modelname);
72
73 if (!parentname.empty())
74 objectname = parentname + "/" + objectname;
75
76 std::cout << "Video name: " << opt_videoname << std::endl;
77
79 vpImage<vpRGBa> I_color;
80
82 g.setFileName(opt_videoname);
83 g.open(I);
84
87
89 cam.initPersProjWithoutDistortion(839, 839, 325, 243);
90 tracker.setCameraParameters(cam);
91 tracker.loadModel(objectname + ".cao");
92
94 cMo[0][0] = 0.4889237963; cMo[0][1] = 0.8664706489; cMo[0][2] = 0.1009065709; cMo[0][3] = -0.07010159786;
95 cMo[1][0] = 0.4218451176; cMo[1][1] = -0.1335995053; cMo[1][2] = -0.8967708007; cMo[1][3] = -0.08363026223;
96 cMo[2][0] = -0.7635445096; cMo[2][1] = 0.4810195286; cMo[2][2] = -0.4308363901; cMo[2][3] = 0.4510066725;
97 tracker.initFromPose(I, cMo);
98
99 const int height = I.getRows();
100 const int width = I.getCols();
101 int channel = 1;
102 if (color_mode) {
103 channel = save_alpha ? 4 : 3;
104 }
105
106 const vpImageIo::vpImageIoBackendType backend =
108
109 // vector of byte data to store encoded image data using PNG format
110 std::vector<unsigned char> img_buffer;
111
112 const std::string camera_name = "Camera";
113 std::vector<char> vec_camera_name(camera_name.begin(), camera_name.end());
114
115 visp::cnpy::npz_save(npz_filename, "camera_name", &vec_camera_name[0], { vec_camera_name.size() }, "w"); // overwrite
116 visp::cnpy::npz_save(npz_filename, "height", &height, { 1 }, "a"); // append
117 visp::cnpy::npz_save(npz_filename, "width", &width, { 1 }, "a");
118 visp::cnpy::npz_save(npz_filename, "channel", &channel, { 1 }, "a");
119
120 const double cam_px = cam.get_px(), cam_py = cam.get_py(), cam_u0 = cam.get_u0(), cam_v0 = cam.get_v0();
121 visp::cnpy::npz_save(npz_filename, "cam_px", &cam_px, { 1 }, "a");
122 visp::cnpy::npz_save(npz_filename, "cam_py", &cam_py, { 1 }, "a");
123 visp::cnpy::npz_save(npz_filename, "cam_u0", &cam_u0, { 1 }, "a");
124 visp::cnpy::npz_save(npz_filename, "cam_v0", &cam_v0, { 1 }, "a");
125
126 std::vector<double> vec_poses;
127 vec_poses.reserve(g.getLastFrameIndex() * 6);
128
129 std::vector<int> vec_img_data_size;
130 vec_img_data_size.reserve(g.getLastFrameIndex());
131 std::vector<unsigned char> vec_img_data;
132 vec_img_data.reserve(g.getLastFrameIndex() * height * width);
133
134 std::vector<double> times;
135 size_t iter = 0;
136 while (!g.end()) {
137 g.acquire(I);
138 tracker.track(I);
139 tracker.getPose(cMo);
140 if (print_cMo) {
141 std::cout << "\ncMo:\n" << cMo << std::endl;
142 }
143
144 vpImageConvert::convert(I, I_color);
145 double start = vpTime::measureTimeMs();
146 if (color_mode) {
147 vpImageIo::writePNGtoMem(I_color, img_buffer, backend, save_alpha);
148 }
149 else {
150 vpImageIo::writePNGtoMem(I, img_buffer, backend);
151 }
152 double end = vpTime::measureTimeMs();
153 times.push_back(end-start);
154 vec_img_data_size.push_back(static_cast<int>(img_buffer.size()));
155 vec_img_data.insert(vec_img_data.end(), img_buffer.begin(), img_buffer.end());
156
157 std::vector<double> vec_pose = poseToVec(cMo);
158 vec_poses.insert(vec_poses.end(), vec_pose.begin(), vec_pose.end());
159
160 std::map<std::string, std::vector<std::vector<double> > > mapOfModels;
161 std::map<std::string, unsigned int> mapOfW;
162 mapOfW[camera_name] = I.getWidth();
163 std::map<std::string, unsigned int> mapOfH;
164 mapOfH[camera_name] = I.getHeight();
165 std::map<std::string, vpHomogeneousMatrix> mapOfcMos;
166 mapOfcMos[camera_name] = cMo;
167 std::map<std::string, vpCameraParameters> mapOfCams;
168 mapOfCams[camera_name] = cam;
169 tracker.getModelForDisplay(mapOfModels, mapOfW, mapOfH, mapOfcMos, mapOfCams);
170
171 std::vector<std::vector<double>> model = mapOfModels[camera_name];
172 const std::string model_iter = vpIoTools::formatString("model_%06zu", static_cast<int>(iter));
173 const std::string model_iter_sz = model_iter + "_sz";
174
175 const size_t model_size = model.size();
176 visp::cnpy::npz_save(npz_filename, model_iter_sz, &model_size, { 1 }, "a");
177
178 for (size_t i = 0; i < model.size(); i++) {
179 char buffer[100];
180 int res = snprintf(buffer, 100, "model_%06zu_%06zu", iter, i);
181 if (res > 0 && res < 100) {
182 const std::string model_iter_data = buffer;
183 std::vector<double> &vec_line = model[i];
184 visp::cnpy::npz_save(npz_filename, model_iter_data, &vec_line[0], { vec_line.size() }, "a");
185 }
186 }
187
188 iter++;
189 }
190
191 std::cout << "Mean time for image encoding: " << vpMath::getMean(times) << " ms ; Median time: "
192 << vpMath::getMedian(times) << " ms ; Std: " << vpMath::getStdev(times) << " ms" << std::endl;
193
194 visp::cnpy::npz_save(npz_filename, "vec_img_data_size", vec_img_data_size.data(), { vec_img_data_size.size() }, "a");
195 visp::cnpy::npz_save(npz_filename, "vec_img", vec_img_data.data(), { vec_img_data.size() }, "a");
196 // Show how to save a "multidimensional" array
197 assert(iter == vec_poses.size()/6);
198 visp::cnpy::npz_save(npz_filename, "vec_poses", vec_poses.data(), { static_cast<size_t>(iter), 6 }, "a");
199
200 visp::cnpy::npz_save(npz_filename, "nb_data", &iter, { 1 }, "a");
201
202 return EXIT_SUCCESS;
203}
204
205#else
206#include <iostream>
207
208int main()
209{
210 std::cout << "This tutorial needs c++11 flags" << std::endl;
211#ifndef VISP_HAVE_MINIZ
212 std::cerr << "You also need to enable npz I/O functions" << std::endl;
213#endif
214}
215#endif
Generic class defining intrinsic camera parameters.
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
vpImageIoBackendType
Image IO backend for only jpeg and png formats image loading and saving.
Definition vpImageIo.h:129
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
Definition vpImageIo.h:134
@ IO_OPENCV_BACKEND
Use OpenCV imgcodecs module.
Definition vpImageIo.h:132
static void writePNGtoMem(const vpImage< unsigned char > &I, std::vector< unsigned char > &buffer, int backend=IO_DEFAULT_BACKEND)
Definition of the vpImage class member functions.
Definition vpImage.h:131
static std::string formatString(const std::string &name, unsigned int val)
static std::string getNameWE(const std::string &pathname)
static std::string getParent(const std::string &pathname)
static double getMedian(const std::vector< double > &v)
Definition vpMath.cpp:343
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
Definition vpMath.cpp:374
static double getMean(const std::vector< double > &v)
Definition vpMath.cpp:323
Real-time 6D object pose tracking using its CAD model.
Implementation of a rotation vector as axis-angle minimal representation.
Class that consider the case of a translation vector.
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 acquire(vpImage< vpRGBa > &I) VP_OVERRIDE
VISP_EXPORT void npz_save(const std::string &zipname, std::string fname, const std::vector< std::string > &data_vec, const std::vector< size_t > &shape, const std::string &mode="w")
VISP_EXPORT double measureTimeMs()