36 #include <visp3/core/vpConfig.h> 38 #if defined(VISP_HAVE_CATCH2) 39 #define CATCH_CONFIG_ENABLE_BENCHMARKING 40 #define CATCH_CONFIG_RUNNER 43 #include <visp3/core/vpIoTools.h> 44 #include <visp3/io/vpImageIo.h> 45 #include <visp3/mbt/vpMbGenericTracker.h> 49 #include <visp3/gui/vpDisplayX.h> 54 bool runBenchmark =
false;
56 template <
typename Type>
57 bool read_data(
const std::string &input_directory,
int cpt,
const vpCameraParameters &cam_depth,
61 static_assert(std::is_same<Type, unsigned char>::value || std::is_same<Type, vpRGBa>::value,
62 "Template function supports only unsigned char and vpRGBa images!");
64 sprintf(buffer, std::string(input_directory +
"/Images/Image_%04d.pgm").c_str(), cpt);
65 std::string image_filename = buffer;
67 sprintf(buffer, std::string(input_directory +
"/Depth/Depth_%04d.bin").c_str(), cpt);
68 std::string depth_filename = buffer;
70 sprintf(buffer, std::string(input_directory +
"/CameraPose/Camera_%03d.txt").c_str(), cpt);
71 std::string pose_filename = buffer;
79 unsigned int depth_width = 0, depth_height = 0;
80 std::ifstream file_depth(depth_filename.c_str(), std::ios::in | std::ios::binary);
81 if (!file_depth.is_open())
86 I_depth.
resize(depth_height, depth_width);
87 pointcloud.resize(depth_height*depth_width);
89 const float depth_scale = 0.000030518f;
90 for (
unsigned int i = 0; i < I_depth.
getHeight(); i++) {
91 for (
unsigned int j = 0; j < I_depth.
getWidth(); j++) {
93 double x = 0.0, y = 0.0, Z = I_depth[i][j] * depth_scale;
99 pointcloud[i*I_depth.
getWidth()+j] = pt3d;
103 std::ifstream file_pose(pose_filename.c_str());
104 if (!file_pose.is_open()) {
108 for (
unsigned int i = 0; i < 4; i++) {
109 for (
unsigned int j = 0; j < 4; j++) {
110 file_pose >> cMo[i][j];
118 TEST_CASE(
"Benchmark generic tracker",
"[benchmark]") {
120 std::vector<int> tracker_type(2);
126 const std::string configFileCam1 = input_directory + std::string(
"/Config/chateau.xml");
127 const std::string configFileCam2 = input_directory + std::string(
"/Config/chateau_depth.xml");
130 tracker.loadConfigFile(configFileCam1, configFileCam2);
132 tracker.loadModel(input_directory +
"/Models/chateau.cao", input_directory +
"/Models/chateau.cao");
143 tracker.loadModel(input_directory +
"/Models/cube.cao",
false, T);
148 std::vector<vpColVector> pointcloud;
151 tracker.getCameraParameters(cam_color, cam_depth);
154 depth_M_color[0][3] = -0.05;
155 tracker.setCameraTransformationMatrix(
"Camera2", depth_M_color);
158 std::vector<vpImage<unsigned char>> images;
159 std::vector<vpImage<uint16_t>> depth_raws;
160 std::vector<std::vector<vpColVector>> pointclouds;
161 std::vector<vpHomogeneousMatrix> cMo_truth_all;
163 for (
int i = 1; i <= 40; i++) {
164 if (read_data(input_directory, i, cam_depth, I, I_depth_raw, pointcloud, cMo_truth)) {
166 depth_raws.push_back(I_depth_raw);
167 pointclouds.push_back(pointcloud);
168 cMo_truth_all.push_back(cMo_truth);
172 for (
int i = 40; i >= 1; i--) {
173 if (read_data(input_directory, i, cam_depth, I, I_depth_raw, pointcloud, cMo_truth)) {
175 depth_raws.push_back(I_depth_raw);
176 pointclouds.push_back(pointcloud);
177 cMo_truth_all.push_back(cMo_truth);
183 std::vector<std::map<std::string, int>> mapOfTrackerTypes;
186 #if defined(VISP_HAVE_OPENCV) 192 std::vector<std::string> benchmarkNames = {
194 "Edge + Depth dense MBT",
195 #if defined(VISP_HAVE_OPENCV) 197 "KLT + depth dense MBT",
198 "Edge + KLT + depth dense MBT" 202 std::vector<bool> monoculars = {
205 #if defined(VISP_HAVE_OPENCV) 212 for (
size_t idx = 0; idx < mapOfTrackerTypes.size(); idx++) {
213 tracker.resetTracker();
214 tracker.setTrackerType(mapOfTrackerTypes[idx]);
216 tracker.loadConfigFile(configFileCam1, configFileCam2);
217 tracker.loadModel(input_directory +
"/Models/chateau.cao", input_directory +
"/Models/chateau.cao");
218 tracker.loadModel(input_directory +
"/Models/cube.cao",
false, T);
219 tracker.initFromPose(images.front(), cMo_truth_all.front());
221 std::map<std::string, unsigned int> mapOfWidths, mapOfHeights;
222 mapOfWidths[
"Camera2"] = monoculars[idx] ? 0 : I_depth_raw.
getWidth();
223 mapOfHeights[
"Camera2"] = monoculars[idx] ? 0 : I_depth_raw.
getHeight();
226 #ifndef DEBUG_DISPLAY 227 BENCHMARK(benchmarkNames[idx].c_str())
234 tracker.setDisplayFeatures(
true);
237 tracker.initFromPose(images.front(), cMo_truth_all.front());
239 for (
size_t i = 0; i < images.size(); i++) {
241 const std::vector<vpColVector>& pointcloud_current = pointclouds[i];
250 std::map<std::string, const vpImage<unsigned char> *> mapOfImages;
251 mapOfImages[
"Camera1"] = &I_current;
253 std::map<std::string, const std::vector<vpColVector> *> mapOfPointclouds;
254 mapOfPointclouds[
"Camera2"] = &pointcloud_current;
256 tracker.track(mapOfImages, mapOfPointclouds, mapOfWidths, mapOfHeights);
257 cMo = tracker.getPose();
260 tracker.display(I, I_depth, cMo, depth_M_color*cMo, cam_color, cam_depth,
vpColor::red, 3);
272 #ifndef DEBUG_DISPLAY 282 for (
unsigned int i = 0; i < 3; i++) {
283 t_err[i] = pose_est[i] - pose_truth[i];
284 tu_err[i] = pose_est[i+3] - pose_truth[i+3];
287 const double max_translation_error = 0.005;
288 const double max_rotation_error = 0.03;
289 CHECK(sqrt(t_err.sumSquare()) < max_translation_error);
290 CHECK(sqrt(tu_err.sumSquare()) < max_rotation_error);
296 int main(
int argc,
char *argv[])
298 Catch::Session session;
301 using namespace Catch::clara;
302 auto cli = session.cli()
305 (
"run benchmark comparing naive code with ViSP implementation");
311 session.applyCommandLine(argc, argv);
313 int numFailed = session.run();
static void read(vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
VISP_EXPORT int wait(double t0, double t)
void resize(unsigned int h, unsigned int w)
resize the image : Image initialization
Implementation of an homogeneous matrix and operations on such kind of matrices.
static void displayText(const vpImage< unsigned char > &I, const vpImagePoint &ip, const std::string &s, const vpColor &color)
Use the X11 console to display images on unix-like OS. Thus to enable this class X11 should be instal...
static const vpColor none
static void convertPoint(const vpCameraParameters &cam, const double &u, const double &v, double &x, double &y)
Real-time 6D object pose tracking using its CAD model.
static void flush(const vpImage< unsigned char > &I)
static void display(const vpImage< unsigned char > &I)
Generic class defining intrinsic camera parameters.
unsigned int getHeight() const
Implementation of column vector and the associated operations.
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))
Implementation of a pose vector and operations on poses.
unsigned int getWidth() const
Definition of the vpImage class member functions.
static void createDepthHistogram(const vpImage< uint16_t > &src_depth, vpImage< vpRGBa > &dest_rgba)