Berkeley SfM
image.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015, The Regents of the University of California (Regents).
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above
13  * copyright notice, this list of conditions and the following
14  * disclaimer in the documentation and/or other materials provided
15  * with the distribution.
16  *
17  * 3. Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived
19  * from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  * Please contact the author(s) of this library if you have any questions.
34  * Authors: Erik Nelson ( eanelson@eecs.berkeley.edu )
35  * David Fridovich-Keil ( dfk@eecs.berkeley.edu )
36  */
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 //
40 // This class defines a wrapper around OpenCV's Mat class.
41 //
42 ///////////////////////////////////////////////////////////////////////////////
43 
44 #ifndef BSFM_IMAGE_IMAGE_H
45 #define BSFM_IMAGE_IMAGE_H
46 
47 #include <memory>
48 #include <string>
49 
50 #include <Eigen/Dense>
51 #include <glog/logging.h>
52 #include <opencv2/core/core.hpp>
53 #include <opencv2/core/eigen.hpp>
54 #include <opencv2/highgui/highgui.hpp>
55 #include <opencv2/imgproc/imgproc.hpp>
56 
57 namespace bsfm {
58 
59 using Eigen::MatrixXd;
60 using Eigen::MatrixXf;
61 
62 class Image {
63  public:
64  typedef std::shared_ptr<Image> Ptr;
65  typedef std::shared_ptr<const Image> ConstPtr;
66 
67  Image() : grayscale_(false) {}
68  ~Image() {}
69 
70  // Copy ctor.
71  Image(const Image& other);
72 
73  // Basic ctor.
74  Image(size_t width, size_t height, size_t channels);
75 
76  // Ctor to load from file.
77  explicit Image(const std::string& filename, bool grayscale = false);
78 
79  // Construct from OpenCV mat.
80  explicit Image(const cv::Mat& other);
81 
82  // Access the pixel at (u, v), at a specific channel.
83  template <typename T>
84  inline T& at(size_t u, size_t v);
85 
86  template <typename T>
87  const inline T& at(size_t u, size_t v) const;
88 
89  // Copy to and from OpenCV mats.
90  void ToCV(cv::Mat& out) const;
91  void FromCV(const cv::Mat& in);
92 
93  // Convert to Eigen matrix.
94  void ToEigen(MatrixXf& eigen_out);
95 
96  // Save and load.
97  void Read(const std::string& filename, bool grayscale = false);
98  void Write(const std::string& filename) const;
99 
100  // Basic information.
101  size_t Width() const;
102  size_t Height() const;
103  size_t Cols() const { return Width(); }
104  size_t Rows() const { return Height(); }
105  size_t Channels() const;
106  bool IsColor() const { return !grayscale_; }
107 
108  // Resize to a specific scale or to a specific width and height.
109  void Resize(double scale);
110  void Resize(size_t new_width, size_t new_height);
111 
112  // Transpose, rotate, and flip.
113  void Transpose();
114  void RotateClockwise();
115  void RotateCounterClockwise();
116  void FlipLR();
117  void FlipUD();
118 
119  // Convert between grayscale and 3-channel color.
120  void ConvertToGrayscale();
121  void ConvertToRGB();
122 
123  // Open a window to display the image.
124  void ImShow(const std::string& window_name = std::string(),
125  unsigned int wait_time = 0);
126 
127  private:
129  std::shared_ptr<cv::Mat> image_;
130 }; //\class Image
131 
132 // Non-member conversion from OpenCV to Eigen matrices.
133 template <typename T>
134 inline void OpenCVToEigenMat(
135  const cv::Mat& cv_mat,
136  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& eigen_mat);
137 
138 // Non-member conversions from Eigen to OpenCV matrices.
139 template <typename T>
140 inline void EigenMatToOpenCV(
141  const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& eigen_mat,
142  cv::Mat& cv_mat);
143 
144 // ------------------- Implementation ------------------- //
145 
146 template <typename T>
147 T& Image::at(size_t u, size_t v) {
148  CHECK_NOTNULL(image_.get());
149  return image_->template at<T>(u, v);
150 }
151 
152 template <typename T>
153 const T& Image::at(size_t u, size_t v) const {
154  CHECK_NOTNULL(image_.get());
155  return image_->template at<T>(u, v);
156 }
157 
158 // Non-member conversion from OpenCV to Eigen matrices.
159 template <typename T>
161  const cv::Mat& cv_mat,
162  Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& eigen_mat) {
163  // Make sure the data is grayscale before converting to an eigen matrix.
164  if (cv_mat.channels() != 1) {
165  cv::Mat grayscale_mat;
166  cv::cvtColor(cv_mat, grayscale_mat, CV_RGB2GRAY);
167  cv::cv2eigen(grayscale_mat, eigen_mat);
168  } else {
169  cv::cv2eigen(cv_mat, eigen_mat);
170  }
171 }
172 
173 // Non-member conversion from Eigen to OpenCV matrices.
174 template <typename T>
176  const Eigen::Matrix<T, Eigen::Dynamic, Eigen::Dynamic>& eigen_mat,
177  cv::Mat& cv_mat) {
178  cv::eigen2cv(eigen_mat, cv_mat);
179 }
180 
181 } //\namespace bsfm
182 #endif
size_t Cols() const
Definition: image.h:103
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void RotateClockwise()
Definition: image.cpp:144
void FlipLR()
Definition: image.cpp:156
void FlipUD()
Definition: image.cpp:161
std::shared_ptr< const Image > ConstPtr
Definition: image.h:65
size_t Channels() const
Definition: image.cpp:123
void RotateCounterClockwise()
Definition: image.cpp:150
bool IsColor() const
Definition: image.h:106
size_t Height() const
Definition: image.cpp:118
void ConvertToRGB()
Definition: image.cpp:178
void Write(const std::string &filename) const
Definition: image.cpp:109
std::shared_ptr< Image > Ptr
Definition: image.h:64
~Image()
Definition: image.h:68
void Transpose()
Definition: image.cpp:139
bool grayscale_
Definition: image.h:128
void FromCV(const cv::Mat &in)
Definition: image.cpp:79
void Resize(double scale)
Definition: image.cpp:128
Image()
Definition: image.h:67
void ToEigen(MatrixXf &eigen_out)
Definition: image.cpp:86
Definition: camera.cpp:50
void Read(const std::string &filename, bool grayscale=false)
Definition: image.cpp:91
void ConvertToGrayscale()
Definition: image.cpp:166
size_t Rows() const
Definition: image.h:104
T & at(size_t u, size_t v)
Definition: image.h:147
void EigenMatToOpenCV(const Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &eigen_mat, cv::Mat &cv_mat)
Definition: image.h:175
void OpenCVToEigenMat(const cv::Mat &cv_mat, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &eigen_mat)
Definition: image.h:160
void ImShow(const std::string &window_name=std::string(), unsigned int wait_time=0)
Definition: image.cpp:190
void ToCV(cv::Mat &out) const
Definition: image.cpp:72
size_t Width() const
Definition: image.cpp:113