1 #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>
8 #if defined(VISP_HAVE_MINIZ) && defined(VISP_HAVE_WORKING_REGEX)
9 #ifdef ENABLE_VISP_NAMESPACE
20 std::vector<double> vec { t[0], t[1], t[2], tu[0], tu[1], tu[2] };
26 std::string toString(
const std::string &name,
int val)
28 auto fmt = name.c_str();
29 int sz = std::snprintf(
nullptr, 0, fmt, val);
30 std::vector<char> buf(sz);
31 std::snprintf(&buf[0], sz, fmt, val);
32 std::string str(buf.begin(), buf.end());
38 int main(
int argc,
char **argv)
40 bool opencv_backend =
false;
41 std::string npz_filename =
"npz_tracking_teabox.npz";
42 bool color_mode =
false;
43 bool save_alpha =
false;
44 bool print_cMo =
false;
46 for (
int i = 1; i < argc; i++) {
47 if (std::string(argv[i]) ==
"--cv-backend") {
48 opencv_backend =
true;
50 else if ((std::string(argv[i]) ==
"--save" || std::string(argv[i]) ==
"-o") && (i+1 < argc)) {
51 npz_filename = argv[i+1];
54 else if (std::string(argv[i]) ==
"--color" || std::string(argv[i]) ==
"-c") {
57 else if (std::string(argv[i]) ==
"--alpha" || std::string(argv[i]) ==
"-a") {
60 else if (std::string(argv[i]) ==
"--print-cMo") {
64 std::cout <<
"Options:" << std::endl;
65 std::cout <<
" --cv-backend use OpenCV if available for in-memory PNG encoding" << std::endl;
66 std::cout <<
" --save / -o output filename" << std::endl;
67 std::cout <<
" --color save RGB data" << std::endl;
68 std::cout <<
" --alpha if --color opton, save RGBa data" << std::endl;
69 std::cout <<
" --print-cMo print cMo" << std::endl;
74 std::cout <<
"Save file: " << npz_filename << std::endl;
75 std::cout <<
"OpenCV backend? " << opencv_backend << std::endl;
76 std::cout <<
"Color image? " << color_mode << std::endl;
77 std::cout <<
"Save alpha channel? " << save_alpha << std::endl;
79 std::string opt_videoname =
"model/teabox/teabox.mp4";
80 std::string opt_modelname =
"model/teabox/teabox.cao";
85 if (!parentname.empty())
86 objectname = parentname +
"/" + objectname;
88 std::cout <<
"Video name: " << opt_videoname << std::endl;
106 cMo[0][0] = 0.4889237963; cMo[0][1] = 0.8664706489; cMo[0][2] = 0.1009065709; cMo[0][3] = -0.07010159786;
107 cMo[1][0] = 0.4218451176; cMo[1][1] = -0.1335995053; cMo[1][2] = -0.8967708007; cMo[1][3] = -0.08363026223;
108 cMo[2][0] = -0.7635445096; cMo[2][1] = 0.4810195286; cMo[2][2] = -0.4308363901; cMo[2][3] = 0.4510066725;
111 const int height = I.
getRows();
115 channel = save_alpha ? 4 : 3;
122 std::vector<unsigned char> img_buffer;
124 const std::string camera_name =
"Camera";
125 std::vector<char> vec_camera_name(camera_name.begin(), camera_name.end());
127 visp::cnpy::npz_save(npz_filename,
"camera_name", &vec_camera_name[0], { vec_camera_name.size() },
"w");
138 std::vector<double> vec_poses;
141 std::vector<int> vec_img_data_size;
143 std::vector<unsigned char> vec_img_data;
146 std::vector<double> times;
153 std::cout <<
"\ncMo:\n" << cMo << std::endl;
165 times.push_back(end-start);
166 vec_img_data_size.push_back(
static_cast<int>(img_buffer.size()));
167 vec_img_data.insert(vec_img_data.end(), img_buffer.begin(), img_buffer.end());
169 std::vector<double> vec_pose = poseToVec(cMo);
170 vec_poses.insert(vec_poses.end(), vec_pose.begin(), vec_pose.end());
172 std::map<std::string, std::vector<std::vector<double> > > mapOfModels;
173 std::map<std::string, unsigned int> mapOfW;
175 std::map<std::string, unsigned int> mapOfH;
177 std::map<std::string, vpHomogeneousMatrix> mapOfcMos;
178 mapOfcMos[camera_name] = cMo;
179 std::map<std::string, vpCameraParameters> mapOfCams;
180 mapOfCams[camera_name] = cam;
183 std::vector<std::vector<double>> model = mapOfModels[camera_name];
184 const std::string model_iter = toString(
"model_%06zu",
static_cast<int>(iter));
185 const std::string model_iter_sz = model_iter +
"_sz";
186 const size_t model_size = model.size();
189 for (
size_t i = 0; i < model.size(); i++) {
191 int res = snprintf(buffer, 100,
"model_%06zu_%06zu", iter, i);
192 if (res > 0 && res < 100) {
193 const std::string model_iter_data = buffer;
194 std::vector<double> &vec_line = model[i];
202 std::cout <<
"Mean time for image encoding: " <<
vpMath::getMean(times) <<
" ms ; Median time: "
205 visp::cnpy::npz_save(npz_filename,
"vec_img_data_size", vec_img_data_size.data(), { vec_img_data_size.size() },
"a");
206 visp::cnpy::npz_save(npz_filename,
"vec_img", vec_img_data.data(), { vec_img_data.size() },
"a");
208 assert(iter == vec_poses.size()/6);
209 visp::cnpy::npz_save(npz_filename,
"vec_poses", vec_poses.data(), { static_cast<size_t>(iter), 6 },
"a");
221 std::cout <<
"This tutorial needs c++11 flags" << std::endl;
222 #ifndef VISP_HAVE_MINIZ
223 std::cerr <<
"You also need to enable npz I/O functions" << std::endl;
Generic class defining intrinsic camera parameters.
void initPersProjWithoutDistortion(double px, double py, double u0, double v0)
Implementation of an homogeneous matrix and operations on such kind of matrices.
vpThetaUVector getThetaUVector() const
vpTranslationVector getTranslationVector() const
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.
@ IO_STB_IMAGE_BACKEND
Use embedded stb_image library.
@ IO_OPENCV_BACKEND
Use OpenCV imgcodecs module.
static void writePNGtoMem(const vpImage< unsigned char > &I, std::vector< unsigned char > &buffer, int backend=IO_DEFAULT_BACKEND)
unsigned int getWidth() const
unsigned int getCols() const
unsigned int getHeight() const
unsigned int getRows() const
static double getMedian(const std::vector< double > &v)
static double getStdev(const std::vector< double > &v, bool useBesselCorrection=false)
static double getMean(const std::vector< double > &v)
Real-time 6D object pose tracking using its CAD model.
virtual void setCameraParameters(const vpCameraParameters &camera) VP_OVERRIDE
virtual void initFromPose(const vpImage< unsigned char > &I, const vpHomogeneousMatrix &cMo) VP_OVERRIDE
virtual void getPose(vpHomogeneousMatrix &cMo) const VP_OVERRIDE
virtual void track(const vpImage< unsigned char > &I) VP_OVERRIDE
virtual void loadModel(const std::string &modelFile, bool verbose=false, const vpHomogeneousMatrix &T=vpHomogeneousMatrix()) VP_OVERRIDE
virtual void setTrackerType(int type)
virtual std::vector< std::vector< double > > getModelForDisplay(unsigned int width, unsigned int height, const vpHomogeneousMatrix &cMo, const vpCameraParameters &cam, bool displayFullModel=false) VP_OVERRIDE
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...
void acquire(vpImage< vpRGBa > &I)
void open(vpImage< vpRGBa > &I)
void setFileName(const std::string &filename)
void npz_save(std::string zipname, std::string fname, const T *data, const std::vector< size_t > &shape, std::string mode="w")
VISP_EXPORT double measureTimeMs()