LBANN  0.103.0
LivermoreBigArtificialNeuralNetworkToolkit
opencv.hpp
Go to the documentation of this file.
1 // Copyright (c) 2014-2023, Lawrence Livermore National Security, LLC.
3 // Produced at the Lawrence Livermore National Laboratory.
4 // Written by the LBANN Research Team (B. Van Essen, et al.) listed in
5 // the CONTRIBUTORS file. <lbann-dev@llnl.gov>
6 //
7 // LLNL-CODE-697807.
8 // All rights reserved.
9 //
10 // This file is part of LBANN: Livermore Big Artificial Neural Network
11 // Toolkit. For details, see http://software.llnl.gov/LBANN or
12 // https://github.com/LLNL/LBANN.
13 //
14 // Licensed under the Apache License, Version 2.0 (the "Licensee"); you
15 // may not use this file except in compliance with the License. You may
16 // obtain a copy of the License at:
17 //
18 // http://www.apache.org/licenses/LICENSE-2.0
19 //
20 // Unless required by applicable law or agreed to in writing, software
21 // distributed under the License is distributed on an "AS IS" BASIS,
22 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
23 // implied. See the License for the specific language governing
24 // permissions and limitations under the license.
26 
27 #ifndef LBANN_UTILS_OPENCV_HPP_INCLUDED
28 #define LBANN_UTILS_OPENCV_HPP_INCLUDED
29 
32 #include <opencv2/core.hpp>
33 
34 namespace lbann {
35 namespace utils {
36 
46  const std::vector<size_t>& dims)
47 {
48  try {
49  // Check if we can do the conversion.
50  const auto& unused = data.template get<uint8_t>();
51  (void)unused;
52  }
53  catch (const std::bad_any_cast&) {
54  return false;
55  }
56  if (dims.size() != 3 || (dims[0] != 1 && dims[0] != 3)) {
57  return false;
58  }
59  return true;
60 }
61 
72  const std::vector<size_t>& dims)
73 {
74  try {
75  // Check if we can do the conversion.
76  const auto& unused = data.template get<uint8_t>();
77  (void)unused;
78  }
79  catch (const std::bad_any_cast&) {
80  LBANN_ERROR("Data is not an image: not uint8_t.");
81  }
82  if (dims.size() != 3 || (dims[0] != 1 && dims[0] != 3)) {
83  LBANN_ERROR("Data is not an image: bad dims.");
84  }
85 }
86 
94  const std::vector<size_t>& dims)
95 {
96  assert_is_image(data, dims);
97  auto& mat = data.template get<uint8_t>();
98  return cv::Mat(dims[1],
99  dims[2],
100  dims[0] == 1 ? CV_8UC1 : CV_8UC3,
101  mat.Buffer());
102 }
103 
110 inline cv::Mat get_opencv_mat(El::Matrix<uint8_t>& data,
111  const std::vector<size_t>& dims)
112 {
113  if (dims.size() != 3 || (dims[0] != 1 && dims[0] != 3)) {
114  LBANN_ERROR("Data is not an image: bad dims.");
115  }
116  return cv::Mat(dims[1],
117  dims[2],
118  dims[0] == 1 ? CV_8UC1 : CV_8UC3,
119  data.Buffer());
120 }
121 
122 } // namespace utils
123 } // namespace lbann
124 
125 #endif // LBANN_UTILS_OPENCV_HPP_INCLUDED
bool check_is_image(const utils::type_erased_matrix &data, const std::vector< size_t > &dims)
Definition: opencv.hpp:45
#define LBANN_ERROR(...)
Definition: exception.hpp:37
T & data(const cnpy::NpyArray &na, const std::vector< size_t > indices)
Definition: cnpy_utils.hpp:75
void assert_is_image(const utils::type_erased_matrix &data, const std::vector< size_t > &dims)
Definition: opencv.hpp:71
A type-erased wrapper around an El::Matrix<T,Device::CPU>
El::Matrix< DataType, El::Device::CPU > Mat
Definition: base.hpp:185
cv::Mat get_opencv_mat(utils::type_erased_matrix &data, const std::vector< size_t > &dims)
Definition: opencv.hpp:93