This tutorial introduces the basic steps to superimpose an object, i.e. a mesh, on top of an image by using the SICAD class and its methods.
Just have a look at the following commented, ready-to-go, code snippets!
Default easy-peasy SICAD
View it online.
7 #include <glm/gtc/matrix_transform.hpp> 9 #include <opencv2/core/core.hpp> 10 #include <opencv2/imgcodecs/imgcodecs.hpp> 11 #include <opencv2/imgproc/imgproc.hpp> 28 const unsigned int cam_width = 320;
29 const unsigned int cam_height = 240;
30 const float cam_fx = 257.34;
31 const float cam_cx = 160;
32 const float cam_fy = 257.34;
33 const float cam_cy = 120;
51 obj.emplace(
"alien",
"./spaceinvader.obj");
57 SICAD si_cad(obj, cam_width, cam_height, cam_fx, cam_fy, cam_cx, cam_cy);
89 objpose_map.emplace(
"alien", obj_pose);
98 double cam_x[] = { 0, 0, 0};
99 double cam_o[] = {1.0, 0, 0, 0};
109 si_cad.superimpose(objpose_map, cam_x, cam_o, img);
110 cv::imwrite(
"./spaceinvader.jpg", img);
std::unordered_map< std::string, std::string > ModelPathContainer
std::vector< double > ModelPose
A Superimpose derived class to superimpose mesh models on images.
std::multimap< std::string, ModelPose > ModelPoseContainer
If instead you want to provide your own OpenGL shader files the code changes just a tiny bit: you just have to provide two more paramenters to the the SICAD constructor.
Use your own OpenGL shaders in SICAD
View it online.
7 #include <glm/gtc/matrix_transform.hpp> 9 #include <opencv2/core/core.hpp> 10 #include <opencv2/imgcodecs/imgcodecs.hpp> 11 #include <opencv2/imgproc/imgproc.hpp> 28 const unsigned int cam_width = 320;
29 const unsigned int cam_height = 240;
30 const float cam_fx = 257.34;
31 const float cam_cx = 160;
32 const float cam_fy = 257.34;
33 const float cam_cy = 120;
51 obj.emplace(
"alien",
"./spaceinvader.obj");
65 SICAD si_cad(obj, cam_width, cam_height, cam_fx, cam_fy, cam_cx, cam_cy, 1,
".");
97 objpose_map.emplace(
"alien", obj_pose);
106 double cam_x[] = { 0, 0, 0};
107 double cam_o[] = {1.0, 0, 0, 0};
117 si_cad.superimpose(objpose_map, cam_x, cam_o, img);
118 cv::imwrite(
"./spaceinvader.jpg", img);
std::unordered_map< std::string, std::string > ModelPathContainer
std::vector< double > ModelPose
A Superimpose derived class to superimpose mesh models on images.
std::multimap< std::string, ModelPose > ModelPoseContainer
Here are the shaders and the Space Invaders mesh model:
⚠️ The four shaders must have the following exact names:
- shader_model.vert for the model vertex shader
- shader_model_texture.vert for the textured model vertex shader
- shader_model.frag for the model fragment shader
- shader_background.vert for the background vertex shader
- shader_background.frag for the background fragment shader
Result + Next
You should get something like this:
👾
You can now proceed to the next tutorial: how to add a background!