22 #ifndef MORPHABLEMODEL_HPP_ 23 #define MORPHABLEMODEL_HPP_ 25 #include "eos/morphablemodel/PcaModel.hpp" 27 #include "eos/render/Mesh.hpp" 29 #include "eos/morphablemodel/io/mat_cerealisation.hpp" 30 #include "cereal/cereal.hpp" 31 #include "cereal/access.hpp" 32 #include "cereal/types/vector.hpp" 33 #include "cereal/archives/binary.hpp" 40 namespace eos {
namespace morphablemodel {
namespace detail {
41 eos::render::Mesh sample_to_mesh(cv::Mat shape, cv::Mat color, std::vector<std::array<int, 3>> tvi, std::vector<std::array<int, 3>> tci, std::vector<cv::Vec2f> texture_coordinates = std::vector<cv::Vec2f>());
45 namespace morphablemodel {
67 MorphableModel(
PcaModel shape_model,
PcaModel color_model, std::vector<cv::Vec2f> texture_coordinates = std::vector<cv::Vec2f>()) : shape_model(shape_model), color_model(color_model), texture_coordinates(texture_coordinates)
101 cv::Mat shape = shape_model.
get_mean();
102 cv::Mat color = color_model.
get_mean();
105 if (has_texture_coordinates()) {
127 cv::Mat shapeSample = shape_model.
draw_sample(shape_sigma);
128 cv::Mat colorSample = color_model.
draw_sample(color_sigma);
131 if (has_texture_coordinates()) {
158 cv::Mat shape_sample;
159 cv::Mat color_sample;
161 if (shape_coefficients.empty()) {
162 shape_sample = shape_model.
get_mean();
165 shape_sample = shape_model.
draw_sample(shape_coefficients);
167 if (color_coefficients.empty()) {
168 color_sample = color_model.
get_mean();
171 color_sample = color_model.
draw_sample(color_coefficients);
175 if (has_texture_coordinates()) {
192 return !color_model.
get_mean().empty();
202 return texture_coordinates;
208 std::vector<cv::Vec2f> texture_coordinates;
216 bool has_texture_coordinates()
const {
217 return texture_coordinates.size() > 0 ?
true :
false;
220 friend class cereal::access;
226 template<
class Archive>
227 void serialize(Archive& archive,
const std::uint32_t version)
229 archive(shape_model, color_model, texture_coordinates);
245 std::ifstream file(filename, std::ios::binary);
247 throw std::runtime_error(
"Error opening given file: " + filename);
249 cereal::BinaryInputArchive input_archive(file);
250 input_archive(model);
264 std::ofstream file(filename, std::ios::binary);
265 cereal::BinaryOutputArchive output_archive(file);
266 output_archive(model);
285 eos::render::Mesh sample_to_mesh(cv::Mat shape, cv::Mat color, std::vector<std::array<int, 3>> tvi, std::vector<std::array<int, 3>> tci, std::vector<cv::Vec2f> texture_coordinates )
287 assert(shape.rows == color.rows || color.empty());
289 auto num_vertices = shape.rows / 3;
295 for (
auto i = 0; i < num_vertices; ++i) {
296 mesh.
vertices[i] = cv::Vec4f(shape.at<
float>(i * 3 + 0), shape.at<
float>(i * 3 + 1), shape.at<
float>(i * 3 + 2), 1.0f);
300 if (!color.empty()) {
301 mesh.
colors.resize(num_vertices);
302 for (
auto i = 0; i < num_vertices; ++i) {
303 mesh.
colors[i] = cv::Vec3f(color.at<
float>(i * 3 + 0), color.at<
float>(i * 3 + 1), color.at<
float>(i * 3 + 2));
312 if (!texture_coordinates.empty()) {
314 for (
auto i = 0; i < num_vertices; ++i) {
315 mesh.
texcoords[i] = texture_coordinates[i];
std::vector< std::array< int, 3 > > tci
Triangle color indices.
Definition: Mesh.hpp:52
This class represents a PCA-model that consists of:
Definition: PcaModel.hpp:55
PcaModel get_color_model() const
Definition: MorphableModel.hpp:87
render::Mesh get_mean() const
Definition: MorphableModel.hpp:97
cv::Mat draw_sample(float sigma=1.0f)
Definition: PcaModel.hpp:143
void save_model(MorphableModel model, std::string filename)
Definition: MorphableModel.hpp:262
int get_data_dimension() const
Definition: PcaModel.hpp:98
std::vector< std::array< int, 3 > > get_triangle_list() const
Definition: PcaModel.hpp:109
std::vector< cv::Vec2f > get_texture_coordinates() const
Definition: MorphableModel.hpp:200
Namespace containing all of eos's 3D model fitting functionality.
MorphableModel load_model(std::string filename)
Definition: MorphableModel.hpp:241
std::vector< cv::Vec4f > vertices
3D vertex positions.
Definition: Mesh.hpp:47
std::vector< cv::Vec3f > colors
Colour information for each vertex. Expected to be in RGB order.
Definition: Mesh.hpp:48
bool has_color_model() const
Definition: MorphableModel.hpp:190
cv::Mat get_mean() const
Definition: PcaModel.hpp:119
This class represents a 3D mesh consisting of vertices, vertex colour information and texture coordin...
Definition: Mesh.hpp:45
MorphableModel(PcaModel shape_model, PcaModel color_model, std::vector< cv::Vec2f > texture_coordinates=std::vector< cv::Vec2f >())
Definition: MorphableModel.hpp:67
PcaModel get_shape_model() const
Definition: MorphableModel.hpp:77
std::vector< std::array< int, 3 > > tvi
Triangle vertex indices.
Definition: Mesh.hpp:51
std::vector< cv::Vec2f > texcoords
Texture coordinates for each vertex.
Definition: Mesh.hpp:49
render::Mesh draw_sample(std::vector< float > shape_coefficients, std::vector< float > color_coefficients) const
Definition: MorphableModel.hpp:154
render::Mesh draw_sample(float shape_sigma=1.0f, float color_sigma=1.0f)
Definition: MorphableModel.hpp:123
A class representing a 3D Morphable Model, consisting of a shape- and colour (albedo) PCA model...
Definition: MorphableModel.hpp:54