22 #ifndef CONTOURCORRESPONDENCE_HPP_ 23 #define CONTOURCORRESPONDENCE_HPP_ 25 #include "eos/core/Landmark.hpp" 26 #include "eos/morphablemodel/MorphableModel.hpp" 28 #include "cereal/archives/json.hpp" 30 #include "glm/gtc/matrix_transform.hpp" 32 #include "opencv2/core/core.hpp" 34 #include "boost/property_tree/ptree.hpp" 35 #include "boost/property_tree/info_parser.hpp" 47 struct ContourLandmarks;
48 std::pair<std::vector<std::string>, std::vector<int>>
select_contour(
float yaw_angle,
const ContourLandmarks& contour_landmarks,
const ModelContour& model_contour);
49 std::tuple<std::vector<cv::Vec2f>, std::vector<cv::Vec4f>, std::vector<int>>
get_nearest_contour_correspondences(
const eos::core::LandmarkCollection<cv::Vec2f>& landmarks,
const std::vector<std::string>& landmark_contour_identifiers,
const std::vector<int>& model_contour_indices,
const morphablemodel::MorphableModel& morphable_model,
const glm::mat4x4& view_model,
const glm::mat4x4& ortho_projection,
const glm::vec4& viewport);
67 std::vector<int> right_contour;
70 std::vector<int> left_contour;
90 std::ifstream file(filename);
92 throw std::runtime_error(
"Error opening given file: " + filename);
94 cereal::JSONInputArchive input_archive(file);
95 input_archive(contour);
100 friend class cereal::access;
106 template<
class Archive>
109 archive(cereal::make_nvp(
"right_contour", right_contour), cereal::make_nvp(
"left_contour", left_contour));
127 std::vector<std::string> right_contour;
130 std::vector<std::string> left_contour;
147 using boost::property_tree::ptree;
150 boost::property_tree::info_parser::read_info(filename, configtree);
152 catch (
const boost::property_tree::ptree_error& error) {
153 throw std::runtime_error(
string(
"ContourLandmarks: Error reading landmark-mappings file: ") + error.what());
159 ptree right_contour = configtree.get_child(
"contour_landmarks.right");
160 for (
auto&& landmark : right_contour) {
161 contour.right_contour.emplace_back(landmark.first);
163 ptree left_contour = configtree.get_child(
"contour_landmarks.left");
164 for (
auto&& landmark : left_contour) {
165 contour.left_contour.emplace_back(landmark.first);
168 catch (
const boost::property_tree::ptree_error& error) {
169 throw std::runtime_error(
string(
"ContourLandmarks: Error while parsing the mappings file: ") + error.what());
171 catch (
const std::runtime_error& error) {
172 throw std::runtime_error(
string(
"ContourLandmarks: Error while parsing the mappings file: ") + error.what());
205 std::vector<int> model_contour_indices;
206 std::vector<std::string> landmark_contour_identifiers;
207 std::tie(landmark_contour_identifiers, model_contour_indices) =
select_contour(glm::degrees(yaw_angle), contour_landmarks, model_contour);
210 std::vector<cv::Vec4f> model_points_contour;
211 std::vector<int> vertex_indices_contour;
212 std::vector<cv::Vec2f> image_points_contour;
236 std::vector<int> model_contour_indices;
237 std::vector<std::string> contour_landmark_identifiers;
238 if (yaw_angle >= 0.0f) {
239 model_contour_indices = model_contour.right_contour;
240 contour_landmark_identifiers = contour_landmarks.right_contour;
243 model_contour_indices = model_contour.left_contour;
244 contour_landmark_identifiers = contour_landmarks.left_contour;
246 return std::make_pair(contour_landmark_identifiers, model_contour_indices);
276 std::vector<cv::Vec4f> model_points_cnt;
277 std::vector<int> vertex_indices_cnt;
278 std::vector<cv::Vec2f> image_points_cnt;
282 for (
auto&& ibug_idx : landmark_contour_identifiers)
286 auto result = std::find_if(begin(landmarks), end(landmarks), [&ibug_idx](
auto&& e) {
return e.name == ibug_idx; });
288 cv::Vec2f screen_point_2d_contour_landmark = result->coordinates;
290 std::vector<float> distances_2d;
291 for (
auto&& model_contour_vertex_idx : model_contour_indices)
294 glm::vec3 proj = glm::project(glm::vec3{ vertex[0], vertex[1], vertex[2] }, view_model, ortho_projection, viewport);
295 cv::Vec2f screen_point_model_contour(proj.x, proj.y);
297 double dist = cv::norm(screen_point_model_contour, screen_point_2d_contour_landmark, cv::NORM_L2);
298 distances_2d.emplace_back(dist);
300 auto min_ele = std::min_element(begin(distances_2d), end(distances_2d));
302 auto min_ele_idx = std::distance(begin(distances_2d), min_ele);
303 auto the_3dmm_vertex_id_that_is_closest = model_contour_indices[min_ele_idx];
306 model_points_cnt.emplace_back(vertex);
307 vertex_indices_cnt.emplace_back(the_3dmm_vertex_id_that_is_closest);
308 image_points_cnt.emplace_back(screen_point_2d_contour_landmark);
311 return std::make_tuple(image_points_cnt, model_points_cnt, vertex_indices_cnt);
static ContourLandmarks load(std::string filename)
Definition: contour_correspondence.hpp:142
std::tuple< std::vector< cv::Vec2f >, std::vector< cv::Vec4f >, std::vector< int > > get_contour_correspondences(const eos::core::LandmarkCollection< cv::Vec2f > &landmarks, const ContourLandmarks &contour_landmarks, const ModelContour &model_contour, float yaw_angle, const morphablemodel::MorphableModel &morphable_model, const glm::mat4x4 &view_model, const glm::mat4x4 &ortho_projection, const glm::vec4 &viewport)
Definition: contour_correspondence.hpp:202
std::pair< std::vector< std::string >, std::vector< int > > select_contour(float yaw_angle, const ContourLandmarks &contour_landmarks, const ModelContour &model_contour)
Definition: contour_correspondence.hpp:234
std::vector< Landmark< LandmarkType >> LandmarkCollection
A trivial collection of landmarks that belong together.
Definition: Landmark.hpp:46
Definition of the vertex indices that define the right and left model contour.
Definition: contour_correspondence.hpp:64
Namespace containing all of eos's 3D model fitting functionality.
Defines which 2D landmarks comprise the right and left face contour.
Definition: contour_correspondence.hpp:124
void serialize(Archive &archive)
Definition: contour_correspondence.hpp:107
static ModelContour load(std::string filename)
Definition: contour_correspondence.hpp:86
std::tuple< std::vector< cv::Vec2f >, std::vector< cv::Vec4f >, std::vector< int > > get_nearest_contour_correspondences(const eos::core::LandmarkCollection< cv::Vec2f > &landmarks, const std::vector< std::string > &landmark_contour_identifiers, const std::vector< int > &model_contour_indices, const morphablemodel::MorphableModel &morphable_model, const glm::mat4x4 &view_model, const glm::mat4x4 &ortho_projection, const glm::vec4 &viewport)
Definition: contour_correspondence.hpp:273
PcaModel get_shape_model() const
Definition: MorphableModel.hpp:77
cv::Vec4f get_mean_at_point(int vertex_index) const
Definition: PcaModel.hpp:130
A class representing a 3D Morphable Model, consisting of a shape- and colour (albedo) PCA model...
Definition: MorphableModel.hpp:54