Berkeley SfM
Public Types | Public Member Functions | Private Attributes | List of all members
bsfm::Image Class Reference

#include <image.h>

Public Types

typedef std::shared_ptr< ImagePtr
 
typedef std::shared_ptr< const ImageConstPtr
 

Public Member Functions

 Image ()
 
 ~Image ()
 
 Image (const Image &other)
 
 Image (size_t width, size_t height, size_t channels)
 
 Image (const std::string &filename, bool grayscale=false)
 
 Image (const cv::Mat &other)
 
template<typename T >
T & at (size_t u, size_t v)
 
template<typename T >
const T & at (size_t u, size_t v) const
 
void ToCV (cv::Mat &out) const
 
void FromCV (const cv::Mat &in)
 
void ToEigen (MatrixXf &eigen_out)
 
void Read (const std::string &filename, bool grayscale=false)
 
void Write (const std::string &filename) const
 
size_t Width () const
 
size_t Height () const
 
size_t Cols () const
 
size_t Rows () const
 
size_t Channels () const
 
bool IsColor () const
 
void Resize (double scale)
 
void Resize (size_t new_width, size_t new_height)
 
void Transpose ()
 
void RotateClockwise ()
 
void RotateCounterClockwise ()
 
void FlipLR ()
 
void FlipUD ()
 
void ConvertToGrayscale ()
 
void ConvertToRGB ()
 
void ImShow (const std::string &window_name=std::string(), unsigned int wait_time=0)
 

Private Attributes

bool grayscale_
 
std::shared_ptr< cv::Mat > image_
 

Detailed Description

Definition at line 62 of file image.h.

Member Typedef Documentation

typedef std::shared_ptr<const Image> bsfm::Image::ConstPtr

Definition at line 65 of file image.h.

typedef std::shared_ptr<Image> bsfm::Image::Ptr

Definition at line 64 of file image.h.

Constructor & Destructor Documentation

bsfm::Image::Image ( )
inline

Definition at line 67 of file image.h.

67 : grayscale_(false) {}
bool grayscale_
Definition: image.h:128
bsfm::Image::~Image ( )
inline

Definition at line 68 of file image.h.

68 {}
bsfm::Image::Image ( const Image other)

Definition at line 43 of file image.cpp.

43  : grayscale_(false) {
44  image_ = other.image_;
45  grayscale_ = other.grayscale_;
46 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
bool grayscale_
Definition: image.h:128
bsfm::Image::Image ( size_t  width,
size_t  height,
size_t  channels 
)

Definition at line 49 of file image.cpp.

49  {
50  if (channels == 1) {
51  grayscale_ = true;
52  image_ = std::shared_ptr<cv::Mat>(new cv::Mat(width, height, CV_32F, 0.f));
53  } else {
54  grayscale_ = false;
55  image_ = std::shared_ptr<cv::Mat>(
56  new cv::Mat(width, height, CV_32FC3, CV_RGB(0.f, 0.f, 0.f)));
57  }
58 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
bool grayscale_
Definition: image.h:128
bsfm::Image::Image ( const std::string &  filename,
bool  grayscale = false 
)
explicit

Definition at line 61 of file image.cpp.

61  : grayscale_(false) {
62  image_ = std::shared_ptr<cv::Mat>(new cv::Mat());
63  Read(filename, grayscale);
64 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
bool grayscale_
Definition: image.h:128
void Read(const std::string &filename, bool grayscale=false)
Definition: image.cpp:91
bsfm::Image::Image ( const cv::Mat &  other)
explicit

Definition at line 67 of file image.cpp.

67  {
68  image_ = std::shared_ptr<cv::Mat>(new cv::Mat(other));
69  grayscale_ = (this->Channels() == 1);
70 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
size_t Channels() const
Definition: image.cpp:123
bool grayscale_
Definition: image.h:128

Member Function Documentation

template<typename T >
T & bsfm::Image::at ( size_t  u,
size_t  v 
)
inline

Definition at line 147 of file image.h.

147  {
148  CHECK_NOTNULL(image_.get());
149  return image_->template at<T>(u, v);
150 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
template<typename T >
const T & bsfm::Image::at ( size_t  u,
size_t  v 
) const
inline

Definition at line 153 of file image.h.

153  {
154  CHECK_NOTNULL(image_.get());
155  return image_->template at<T>(u, v);
156 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
size_t bsfm::Image::Channels ( ) const

Definition at line 123 of file image.cpp.

123  {
124  CHECK(image_.get()) << "Image data is not allocated.";
125  return image_->channels();
126 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
size_t bsfm::Image::Cols ( ) const
inline

Definition at line 103 of file image.h.

103 { return Width(); }
size_t Width() const
Definition: image.cpp:113
void bsfm::Image::ConvertToGrayscale ( )

Definition at line 166 of file image.cpp.

166  {
167  CHECK(image_.get()) << "Image data is not allocated.";
168 
169  if (grayscale_) {
170  VLOG(1) << "Cannot convert image to grayscale, image is already grayscale.";
171  return;
172  }
173 
174  cv::cvtColor(*image_, *image_, CV_RGB2GRAY);
175  grayscale_ = true;
176 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
bool grayscale_
Definition: image.h:128
void bsfm::Image::ConvertToRGB ( )

Definition at line 178 of file image.cpp.

178  {
179  CHECK(image_.get()) << "Image data is not allocated.";
180 
181  if (!grayscale_) {
182  VLOG(1) << "Cannot convert image to RGB, image is already RGB.";
183  return;
184  }
185 
186  cv::cvtColor(*image_, *image_, CV_GRAY2RGB);
187  grayscale_ = false;
188 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
bool grayscale_
Definition: image.h:128
void bsfm::Image::FlipLR ( )

Definition at line 156 of file image.cpp.

156  {
157  CHECK(image_.get()) << "Image data is not allocated.";
158  cv::flip(*image_, *image_, 1 /*about vertical axis*/);
159 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void bsfm::Image::FlipUD ( )

Definition at line 161 of file image.cpp.

161  {
162  CHECK(image_.get()) << "Image data is not allocated.";
163  cv::flip(*image_, *image_, 0 /*about horizontal axis*/);
164 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void bsfm::Image::FromCV ( const cv::Mat &  in)

Definition at line 79 of file image.cpp.

79  {
80  CHECK(image_.get()) << "Image data is not allocated.";
81 
82  // Convert from BGR (default in OpenCV) to RGB (internal representation).
83  cv::cvtColor(in, *image_, CV_BGR2RGB);
84 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
size_t bsfm::Image::Height ( ) const

Definition at line 118 of file image.cpp.

118  {
119  CHECK(image_.get()) << "Image data is not allocated.";
120  return image_->rows;
121 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void bsfm::Image::ImShow ( const std::string &  window_name = std::string(),
unsigned int  wait_time = 0 
)

Definition at line 190 of file image.cpp.

190  {
191  CHECK(image_.get()) << "Image data is not allocated.";
192 
193  cv::namedWindow(window_name.c_str(), CV_WINDOW_AUTOSIZE);
194 
195  // If the image is not grayscale, convert it from RGB to BGR.
196  if (!grayscale_) {
197  cv::Mat bgr_image;
198  ToCV(bgr_image);
199  cv::imshow(window_name.c_str(), bgr_image);
200  } else {
201  cv::imshow(window_name.c_str(), *image_);
202  }
203 
204  cv::waitKey(wait_time);
205 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
bool grayscale_
Definition: image.h:128
void ToCV(cv::Mat &out) const
Definition: image.cpp:72
bool bsfm::Image::IsColor ( ) const
inline

Definition at line 106 of file image.h.

106 { return !grayscale_; }
bool grayscale_
Definition: image.h:128
void bsfm::Image::Read ( const std::string &  filename,
bool  grayscale = false 
)

Definition at line 91 of file image.cpp.

91  {
92  if (grayscale) {
93  *image_ = cv::imread(filename.c_str(), CV_LOAD_IMAGE_GRAYSCALE);
94 
95  // Convert from unsigned to floating point.
96  image_->convertTo(*image_, CV_32F, 1.f / 255.f);
97  } else {
98  *image_ = cv::imread(filename.c_str(), CV_LOAD_IMAGE_COLOR);
99 
100  // Convert from unsigned to floating point.
101  image_->convertTo(*image_, CV_32FC3, 1.f / 255.f);
102  }
103  CHECK(image_->data) << "Unable to read image file.";
104 
105  // Convert from BGR (default in OpenCV) to RGB.
106  cv::cvtColor(*image_, *image_, CV_BGR2RGB);
107 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void bsfm::Image::Resize ( double  scale)

Definition at line 128 of file image.cpp.

128  {
129  CHECK(image_.get()) << "Image data is not allocated.";
130  cv::resize(*image_, *image_, cv::Size(), scale, scale, CV_INTER_LANCZOS4);
131 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void bsfm::Image::Resize ( size_t  new_width,
size_t  new_height 
)

Definition at line 133 of file image.cpp.

133  {
134  CHECK(image_.get()) << "Image data is not allocated.";
135  cv::resize(*image_, *image_, cv::Size(new_width, new_height),
136  CV_INTER_LANCZOS4);
137 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void bsfm::Image::RotateClockwise ( )

Definition at line 144 of file image.cpp.

144  {
145  CHECK(image_.get()) << "Image data is not allocated.";
146  Transpose();
147  FlipLR();
148 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void FlipLR()
Definition: image.cpp:156
void Transpose()
Definition: image.cpp:139
void bsfm::Image::RotateCounterClockwise ( )

Definition at line 150 of file image.cpp.

150  {
151  CHECK(image_.get()) << "Image data is not allocated.";
152  Transpose();
153  FlipUD();
154 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void FlipUD()
Definition: image.cpp:161
void Transpose()
Definition: image.cpp:139
size_t bsfm::Image::Rows ( ) const
inline

Definition at line 104 of file image.h.

104 { return Height(); }
size_t Height() const
Definition: image.cpp:118
void bsfm::Image::ToCV ( cv::Mat &  out) const

Definition at line 72 of file image.cpp.

72  {
73  CHECK(image_.get()) << "Image data is not allocated.";
74 
75  // Convert from RGB (internal representation) to BGR (default in OpenCV).
76  cv::cvtColor(*image_, out, CV_RGB2BGR);
77 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void bsfm::Image::ToEigen ( MatrixXf &  eigen_out)

Definition at line 86 of file image.cpp.

86  {
87  CHECK(image_.get()) << "Image data is not allocated.";
88  OpenCVToEigenMat(*image_, eigen_out);
89 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void OpenCVToEigenMat(const cv::Mat &cv_mat, Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > &eigen_mat)
Definition: image.h:160
void bsfm::Image::Transpose ( )

Definition at line 139 of file image.cpp.

139  {
140  CHECK(image_.get()) << "Image data is not allocated.";
141  cv::transpose(*image_, *image_);
142 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
size_t bsfm::Image::Width ( ) const

Definition at line 113 of file image.cpp.

113  {
114  CHECK(image_.get()) << "Image data is not allocated.";
115  return image_->cols;
116 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129
void bsfm::Image::Write ( const std::string &  filename) const

Definition at line 109 of file image.cpp.

109  {
110  cv::imwrite(filename.c_str(), *image_);
111 }
std::shared_ptr< cv::Mat > image_
Definition: image.h:129

Member Data Documentation

bool bsfm::Image::grayscale_
private

Definition at line 128 of file image.h.

std::shared_ptr<cv::Mat> bsfm::Image::image_
private

Definition at line 129 of file image.h.


The documentation for this class was generated from the following files: