mlpack
load_image_impl.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_CORE_DATA_LOAD_IMAGE_IMPL_HPP
14 #define MLPACK_CORE_DATA_LOAD_IMAGE_IMPL_HPP
15 
16 // In case it hasn't been included yet.
17 #include "load.hpp"
18 
19 namespace mlpack {
20 namespace data {
21 
22 // Image loading API.
23 template<typename eT>
24 bool Load(const std::string& filename,
25  arma::Mat<eT>& matrix,
26  ImageInfo& info,
27  const bool fatal)
28 {
29  Timer::Start("loading_image");
30 
31  // STB loads into unsigned char matrices, so we may have to convert once
32  // loaded.
33  arma::Mat<unsigned char> tempMatrix;
34  const bool result = LoadImage(filename, tempMatrix, info, fatal);
35 
36  // If fatal is true, then the program will have already thrown an exception.
37  if (!result)
38  {
39  Timer::Stop("loading_image");
40  return false;
41  }
42 
43  matrix = arma::conv_to<arma::Mat<eT>>::from(tempMatrix);
44  Timer::Stop("loading_image");
45  return true;
46 }
47 
48 // Image loading API for multiple files.
49 template<typename eT>
50 bool Load(const std::vector<std::string>& files,
51  arma::Mat<eT>& matrix,
52  ImageInfo& info,
53  const bool fatal)
54 {
55  if (files.size() == 0)
56  {
57  std::ostringstream oss;
58  oss << "Load(): vector of image files is empty." << std::endl;
59 
60  if (fatal)
61  Log::Fatal << oss.str();
62  else
63  Log::Warn << oss.str();
64 
65  return false;
66  }
67 
68  arma::Mat<unsigned char> img;
69  bool status = LoadImage(files[0], img, info, fatal);
70 
71  if (!status)
72  return false;
73 
74  // Decide matrix dimension using the image height and width.
75  arma::Mat<unsigned char> tmpMatrix(
76  info.Width() * info.Height() * info.Channels(), files.size());
77  tmpMatrix.col(0) = img;
78 
79  for (size_t i = 1; i < files.size() ; ++i)
80  {
81  arma::Mat<unsigned char> colImg(tmpMatrix.colptr(i), tmpMatrix.n_rows, 1,
82  false, true);
83  status = LoadImage(files[i], colImg, info, fatal);
84 
85  if (!status)
86  return false;
87  }
88 
89  matrix = arma::conv_to<arma::Mat<eT>>::from(tmpMatrix);
90  return true;
91 }
92 
93 } // namespace data
94 } // namespace mlpack
95 
96 #endif
const size_t & Channels() const
Get the image channels.
Definition: image_info.hpp:64
static void Start(const std::string &name)
Start the given timer.
Definition: timers.cpp:28
const size_t & Height() const
Get the image height.
Definition: image_info.hpp:59
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
Implements meta-data of images required by data::Load and data::Save for loading and saving images in...
Definition: image_info.hpp:36
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
static void Stop(const std::string &name)
Stop the given timer.
Definition: timers.cpp:36
bool Load(const std::string &filename, arma::Mat< eT > &matrix, const bool fatal=false, const bool transpose=true, const arma::file_type inputLoadType=arma::auto_detect)
Loads a matrix from file, guessing the filetype from the extension.
Definition: load_impl.hpp:89
const size_t & Width() const
Get the image width.
Definition: image_info.hpp:54