Utilities
Utilities header only library
CvMatToTexture.h
1 #include <glm/gtc/type_ptr.hpp>
2 #include <opencv2/opencv.hpp>
3 #include <GLW/Texture.h>
4 #include <Utils/Ulog.h>
5 
6 namespace utils
7 {
8 
9  const std::unordered_map<int, std::string> CV_MAT_TYPE_TO_STRING =
10  {
11  {CV_8UC1, "CV_8UC1"},
12  {CV_8SC1, "CV_8SC1"},
13  {CV_16UC1, "CV_16UC1"},
14  {CV_16SC1, "CV_16SC1"},
15  {CV_32SC1, "CV_32SC1"},
16  {CV_32FC1, "CV_32FC1"},
17  {CV_64FC1, "CV_64FC1"},
18  {CV_8UC2, "CV_8UC2"},
19  {CV_8SC2, "CV_8SC2"},
20  {CV_16UC2, "CV_16UC2"},
21  {CV_16SC2, "CV_16SC2"},
22  {CV_32SC2, "CV_32SC2"},
23  {CV_32FC2, "CV_32FC2"},
24  {CV_64FC2, "CV_64FC2"},
25  {CV_8UC3, "CV_8UC3"},
26  {CV_8SC3, "CV_8SC3"},
27  {CV_16UC3, "CV_16UC3"},
28  {CV_16SC3, "CV_16SC3"},
29  {CV_32SC3, "CV_32SC3"},
30  {CV_32FC3, "CV_32FC3"},
31  {CV_64FC3, "CV_64FC3"},
32  {CV_8UC4, "CV_8UC4"},
33  {CV_8SC4, "CV_8SC4"},
34  {CV_16UC4, "CV_16UC4"},
35  {CV_16SC4, "CV_16SC4"},
36  {CV_32SC4, "CV_32SC4"},
37  {CV_32FC4, "CV_32FC4"},
38  {CV_64FC4, "CV_64FC4"},
39  };
40 
41  gl::GLenum CvMatTypeToGlType(const int mat_type)
42  {
43  if (!std_ext::Contains(mat_type, CV_MAT_TYPE_TO_STRING))
44  {
45  return gl::GLenum(0);
46  }
47  const auto &mat_type_str = CV_MAT_TYPE_TO_STRING.at(mat_type);
48  if (std_ext::Contains("8U", mat_type_str))
49  {
50  return gl::GL_UNSIGNED_BYTE;
51  }
52  if (std_ext::Contains("8S", mat_type_str))
53  {
54  return gl::GL_BYTE;
55  }
56  if (std_ext::Contains("16U", mat_type_str))
57  {
58  return gl::GL_UNSIGNED_SHORT;
59  }
60  if (std_ext::Contains("16S", mat_type_str))
61  {
62  return gl::GL_SHORT;
63  }
64  if (std_ext::Contains("32S", mat_type_str))
65  {
66  return gl::GL_INT;
67  }
68  if (std_ext::Contains("32F", mat_type_str))
69  {
70  return gl::GL_FLOAT;
71  }
72  return gl::GLenum(0);
73  }
74 
75  unsigned int GetNumOfChannels(const int mat_type)
76  {
77  if (!std_ext::Contains(mat_type, CV_MAT_TYPE_TO_STRING))
78  {
79  return 0;
80  }
81  const auto &mat_type_str = CV_MAT_TYPE_TO_STRING.at(mat_type);
82  if (std_ext::Contains("C1", mat_type_str))
83  {
84  return 1;
85  }
86  if (std_ext::Contains("C2", mat_type_str))
87  {
88  return 2;
89  }
90  if (std_ext::Contains("C3", mat_type_str))
91  {
92  return 3;
93  }
94  if (std_ext::Contains("C4", mat_type_str))
95  {
96  return 4;
97  }
98  return 0;
99  }
100 
101  glw::PTexture2D MatToTexture(const cv::Mat &image, bool flip_vertically)
102  {
103  cv::Mat img = image.clone();
104  if (flip_vertically)
105  {
106  cv::flip(img, img, 0);
107  }
108 
109  const int mat_type = image.type();
110 
111  gl::GLenum input_format;
112  gl::GLenum internal_format;
113  if (img.channels() == 1)
114  {
115  input_format = gl::GL_LUMINANCE;
116  switch (mat_type)
117  {
118  case CV_8UC1:
119  internal_format = gl::GL_R8;
120  break;
121  case CV_8SC1:
122  internal_format = gl::GL_R8I;
123  break;
124  case CV_16UC1:
125  internal_format = gl::GL_R16UI;
126  break;
127  case CV_16SC1:
128  internal_format = gl::GL_R16I;
129  break;
130  case CV_32SC1:
131  internal_format = gl::GL_R32I;
132  break;
133  case CV_32FC1:
134  internal_format = gl::GL_R32F;
135  break;
136  default:
137  internal_format = gl::GL_R8;
138  break;
139  }
140 
141  }
142  else if (img.channels() == 3)
143  {
144  input_format = gl::GL_BGR;
145  switch (mat_type)
146  {
147  case CV_8UC3:
148  internal_format = gl::GL_RGB8;
149  break;
150  case CV_8SC3:
151  internal_format = gl::GL_RGB8I;
152  break;
153  case CV_16UC3:
154  internal_format = gl::GL_RGB16UI;
155  break;
156  case CV_16SC3:
157  internal_format = gl::GL_RGB16I;
158  break;
159  case CV_32SC3:
160  internal_format = gl::GL_RGB32I;
161  break;
162  case CV_32FC3:
163  internal_format = gl::GL_RGB32F;
164  break;
165  default:
166  internal_format = gl::GL_RGB8;
167  break;
168  }
169  }
170  else if (img.channels() == 4)
171  {
172  input_format = gl::GL_BGRA;
173  switch (mat_type)
174  {
175  case CV_8UC4:
176  internal_format = gl::GL_RGBA8;
177  break;
178  case CV_8SC4:
179  internal_format = gl::GL_RGBA8I;
180  break;
181  case CV_16UC4:
182  internal_format = gl::GL_RGBA16UI;
183  break;
184  case CV_16SC4:
185  internal_format = gl::GL_RGBA16I;
186  break;
187  case CV_32SC4:
188  internal_format = gl::GL_RGBA32I;
189  break;
190  case CV_32FC4:
191  internal_format = gl::GL_RGBA32F;
192  break;
193  default:
194  internal_format = gl::GL_RGBA8;
195  break;
196  }
197  }
198  else
199  {
200  ulog::Fail("Unrecognized OpenCV image format. Invalid number of dimensions (" + std::to_string(img.channels()) + ").", "utils::MatToTexture");
201  return nullptr;
202  }
203 
204  glw::PTexture2D texture = glw::Texture2D::Create(img.cols, img.rows, internal_format);
205  texture->Bind();
206 
207  int *param = new int[1];
208  gl::glGetIntegerv(gl::GL_UNPACK_ALIGNMENT, param);
209 
210  gl::glPixelStorei(gl::GL_UNPACK_ALIGNMENT, 1);
211 
212  gl::GLenum gl_type = CvMatTypeToGlType(mat_type);
213 
214  if (gl_type == 0)
215  {
216  ulog::Fail("Unsupported cv::Mat data type.", "utils::MatToTexture");
217  return texture;
218  }
219 
220  texture->SetData(input_format, gl_type, img.ptr());
221 
222  gl::glPixelStorei(gl::GL_UNPACK_ALIGNMENT, *param);
223 
224  texture->Unbind();
225  return texture;
226  }
227 
228  cv::Mat TextureToMat(const glw::PTexture2D &texture, bool flip_vertically /*= false*/, const int mat_type /*= CV_8UC3*/)
229  {
230  if (texture == nullptr)
231  {
232  return cv::Mat();
233  }
234 
235  texture->Bind();
236  int gl_texture_width, gl_texture_height;
237  const gl::GLenum gl_type = CvMatTypeToGlType(mat_type);
238  const unsigned int num_of_channels = GetNumOfChannels(mat_type);
239 
240  if (num_of_channels == 0 || num_of_channels == 2 || num_of_channels > 4)
241  {
242  ulog::Fail("Unrecognized OpenCV image format. Invalid number of dimensions.", "utils::TextureToMat");
243  return cv::Mat();
244  }
245 
246  gl::glGetTexLevelParameteriv(gl::GL_TEXTURE_2D, 0, gl::GL_TEXTURE_WIDTH, &gl_texture_width);
247  gl::glGetTexLevelParameteriv(gl::GL_TEXTURE_2D, 0, gl::GL_TEXTURE_HEIGHT, &gl_texture_height);
248 
249  auto gl_texture_floats = (float *)malloc(sizeof(float) * gl_texture_width * gl_texture_height * num_of_channels);
250 
251  if (num_of_channels == 1)
252  {
253  gl::glGetTexImage(gl::GL_TEXTURE_2D, 0, gl::GL_R, gl_type, gl_texture_floats);
254  }
255  else if (num_of_channels == 3)
256  {
257  gl::glGetTexImage(gl::GL_TEXTURE_2D, 0, gl::GL_BGR, gl_type, gl_texture_floats);
258  }
259  else
260  {
261  gl::glGetTexImage(gl::GL_TEXTURE_2D, 0, gl::GL_BGRA, gl_type, gl_texture_floats);
262  }
263 
264  texture->Unbind();
265 
266  cv::Mat result = cv::Mat(gl_texture_height, gl_texture_width, mat_type, gl_texture_floats);
267  result = result.clone();
268 
269  delete gl_texture_floats;
270 
271  if (flip_vertically)
272  {
273  cv::Mat flipped_mat;
274  cv::flip(result, flipped_mat, 0);
275  return flipped_mat;
276  }
277 
278  return result;
279  }
280 
281 }
bool Contains(const std::string &substring, const std::string &text)
Checks if the text contains substring.
Definition: ExtSTD.h:327
General namespace for utilities.
Definition: BulletConversions.h:9