SuperimposeMesh
All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
Superimpose an object with the background

This tutorial is based upon the previous one and explain how to add a background image to the rendering process. Our good ol' fiend 👾 from Space Invaders will finally have Space behind him.

space.png
👾's Space

One of the main reason to why this is important, and to why actually this library exists, is that in computer vision and augmented-reality applications you want your mesh to be in a specific pose relative to a camera viewpoint. As a consequence, you usually have an image grabbed from a camera and the pose of an object relative to it.

Here is another commented, ready-to-go, code snippet (using default shaders)!

View it online.

1 #include <cmath>
2 #include <exception>
3 #include <iostream>
4 #include <string>
5 
6 #include <glm/glm.hpp>
7 #include <glm/gtc/matrix_transform.hpp>
8 #include <opencv2/core/core.hpp>
9 #include <opencv2/imgcodecs/imgcodecs.hpp>
10 #include <opencv2/imgproc/imgproc.hpp>
11 #include <SuperimposeMesh/SICAD.h>
12 
13 
14 int main()
15 {
16  const unsigned int cam_width = 320;
17  const unsigned int cam_height = 240;
18  const float cam_fx = 257.34;
19  const float cam_cx = 160;
20  const float cam_fy = 257.34;
21  const float cam_cy = 120;
22 
24  obj.emplace("alien", "./spaceinvader.obj");
25 
26  SICAD si_cad(obj, cam_width, cam_height, cam_fx, cam_fy, cam_cx, cam_cy);
27 
28  Superimpose::ModelPose obj_pose(7);
29  obj_pose[0] = 0;
30  obj_pose[1] = 0;
31  obj_pose[2] = -0.1;
32  obj_pose[3] = 1;
33  obj_pose[4] = 0;
34  obj_pose[5] = 0;
35  obj_pose[6] = 0;
36 
38  objpose_map.emplace("alien", obj_pose);
39 
40  double cam_x[] = { 0, 0, 0};
41  double cam_o[] = {1.0, 0, 0, 0};
42 
43  /**
44  * It's background time!
45  * The steps are the same, but this time the cv::Mat is created from an
46  * existing image and we instruct the SICAD class to use it as a background
47  * with setBackgroundOpt(true).
48  * That's it!
49  **/
50  cv::Mat img = cv::imread("./space.png");
51  si_cad.setBackgroundOpt(true);
52  si_cad.superimpose(objpose_map, cam_x, cam_o, img);
53  cv::imwrite("./spaceinvader.jpg", img);
54 
55  return EXIT_SUCCESS;
56 }
std::unordered_map< std::string, std::string > ModelPathContainer
Definition: SICAD.h:33
std::vector< double > ModelPose
Definition: Superimpose.h:23
int main()
A Superimpose derived class to superimpose mesh models on images.
Definition: SICAD.h:30
std::multimap< std::string, ModelPose > ModelPoseContainer
Definition: Superimpose.h:25

You should get something like this:

alien_space.jpg
👾 in the Space