Wildmeshing Toolkit
mesh_with_tag_from_image.cpp
Go to the documentation of this file.
2 
3 #include <filesystem>
5 
6 #define STB_IMAGE_IMPLEMENTATION
7 #include <stb_image.h>
8 
9 namespace wmtk::components::input {
10 
11 Eigen::Matrix<int64_t, -1, -1> tags_from_image(const std::filesystem::path& file)
12 {
13  int width, height, channels;
14  unsigned char* img = stbi_load(file.string().c_str(), &width, &height, &channels, 0);
15  if (img == NULL) {
16  throw std::runtime_error("Error in loading the image");
17  }
18  const size_t img_size = width * height * channels;
19 
20  Eigen::Matrix<int64_t, -1, -1> pixel_matrix;
21  pixel_matrix.resize(height, width);
22 
23  for (size_t i = 0; i < height; ++i) {
24  for (size_t j = 0; j < width; ++j) {
25  const int pixel = *(img + ((width * i) + j) * channels);
26  pixel_matrix(i, j) = pixel;
27  }
28  }
29 
30  stbi_image_free(img);
31 
32  return pixel_matrix;
33 }
34 
35 std::shared_ptr<wmtk::TriMesh> mesh_with_tag_from_image(
36  const std::filesystem::path& file,
37  const std::string& tag_name)
38 {
39  std::shared_ptr<wmtk::TriMesh> m = std::make_shared<wmtk::TriMesh>();
40 
41  Eigen::Matrix<int64_t, -1, -1> img = tags_from_image(file);
42 
43  std::cout << "img:\n" << img << std::endl;
44 
45  auto lex_index_v = [&img](size_t i, size_t j) { return i * (img.cols() + 1) + j; };
46  auto lex_index_f = [&img](size_t i, size_t j) { return i * img.cols() + j; };
47 
48  // vertices
49  Eigen::MatrixXd V;
50  V.resize((img.rows() + 1) * (img.cols() + 1), 2);
51  for (size_t i = 0; i < img.rows() + 1; ++i) {
52  for (size_t j = 0; j < img.cols() + 1; ++j) {
53  V.row(lex_index_v(i, j)) = Eigen::Matrix<double, 2, 1>(j, img.rows() - i);
54  }
55  }
56 
57  // triangles
58  RowVectors3l F;
59  F.resize(img.rows() * img.cols() * 2, 3);
61  tags.resize(F.rows(), 1);
62  for (size_t i = 0; i < img.rows(); ++i) {
63  for (size_t j = 0; j < img.cols(); ++j) {
64  const int v0 = lex_index_v(i, j);
65  const int v1 = lex_index_v(i, j + 1);
66  const int v2 = lex_index_v(i + 1, j);
67  const int v3 = lex_index_v(i + 1, j + 1);
68  F.row(2 * lex_index_f(i, j) + 0) = Eigen::Matrix<int64_t, 3, 1>(v0, v2, v1);
69  F.row(2 * lex_index_f(i, j) + 1) = Eigen::Matrix<int64_t, 3, 1>(v2, v3, v1);
70  tags(2 * lex_index_f(i, j) + 0) = img(i, j);
71  tags(2 * lex_index_f(i, j) + 1) = img(i, j);
72  }
73  }
74 
75  m->initialize(F);
76 
79 
80  return m;
81 }
82 
83 } // namespace wmtk::components::input
Eigen::Matrix< int64_t, -1, -1 > tags_from_image(const std::filesystem::path &file)
std::shared_ptr< wmtk::TriMesh > mesh_with_tag_from_image(const std::filesystem::path &file, const std::string &tag_name)
Build a tagged triangle mesh from a greyscale image.
attribute::MeshAttributeHandle set_matrix_attribute(const Mat &data, const std::string &name, const PrimitiveType &type, Mesh &mesh)
Definition: mesh_utils.hpp:9
RowVectors< int64_t, 3 > RowVectors3l
Definition: Types.hpp:47
Eigen::Matrix< T, Eigen::Dynamic, C > RowVectors
Definition: Types.hpp:8