KJB
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
i_filter.h
Go to the documentation of this file.
1 
6 /*
7  * $Id: i_filter.h 18347 2014-12-03 16:58:53Z jguan1 $
8  */
9 
10 #ifndef KJB_CPP_FILTER_H
11 #define KJB_CPP_FILTER_H
12 
13 #include "m_cpp/m_matrix.h"
14 #include "l_cpp/l_exception.h"
15 #include "i_cpp/i_image.h"
16 #include <string>
17 
18 namespace kjb
19 {
20 
36 class Filter
37 {
38 public:
39 
41 
43  Filter(int rows, int cols)
44  : m_kernel(rows % 2 != 0 ? rows : rows + 1, cols % 2 != 0 ? cols : cols + 1)
45  {}
46 
48  Filter(const Matrix& src) :
49  m_kernel(src)
50  {}
51 
53  Filter(const char* fname)
54  : m_kernel(fname)
55  {}
56 
58  Filter(const std::string& fname)
59  : m_kernel(fname.c_str())
60  {}
61 
63  Filter(const Filter& src)
64  : m_kernel(src.m_kernel)
65  {}
66 
67  /* /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ */
68 
71  {}
72 
74  void swap(Filter& other)
75  {
76  using std::swap;
77  swap(m_kernel, other.m_kernel);
78  }
79 
81  int get_num_rows() const
82  {
83  return m_kernel.get_num_rows();
84  }
85 
87  int get_num_cols() const
88  {
89  return m_kernel.get_num_cols();
90  }
91 
94  {
95  if(&m_kernel != &src)
96  {
97  m_kernel = src;
98  }
99 
100  return *this;
101  }
102 
104  Filter& operator=(const Filter& src)
105  {
106  if(this != &src)
107  {
108  m_kernel = src.m_kernel;
109  }
110 
111  return *this;
112  }
113 
115  Value_type& operator()(int row, int col)
116  {
117  // hit KJB(UNTESTED_CODE());
118  return m_kernel(row, col);
119  }
120 
122  Value_type operator()(int row, int col) const
123  {
124  // hit KJB(UNTESTED_CODE());
125  return m_kernel(row, col);
126  }
127 
132  void check_bounds(int row, int col) const
133  {
134  // hit KJB(UNTESTED_CODE());
135  if(row < 0 || get_num_rows() <= row || col < 0 || get_num_cols() <= col)
136  {
137  // hit KJB(UNTESTED_CODE());
139  }
140  }
141 
145  Value_type& at(int row, int col)
146  {
147  // hit KJB(UNTESTED_CODE());
148  check_bounds(row, col);
149  return operator()(row, col);
150  }
151 
155  Value_type at(int row, int col) const
156  {
157  // hit KJB(UNTESTED_CODE());
158  check_bounds(row, col);
159  return operator()(row, col);
160  }
161 
163  void write(std::string fname)
164  {
165  m_kernel.write(fname.c_str());
166  }
167 
168  friend Image operator*(const Image&, const Filter&);
169  friend Matrix operator*(const Matrix&, const Filter&);
170 
171 private:
172  Matrix m_kernel;
173 };
174 
176 Filter gaussian_filter(double sigma, int size);
177 
179 inline Filter gaussian_filter(double sigma)
180 {
181  return gaussian_filter(sigma, 6 * static_cast<int>(sigma) + 1);
182 }
183 
185 Filter laplacian_of_gaussian_filter(int size, double sigma);
186 
188 Image operator*(const Image& image, const Filter& kernel);
189 
191 Image gauss_sample_image(const Image& in, int resolution, double sigma);
192 
194 
195 } // namespace kjb
196 
197 namespace std {
198 
199 template<>
200 inline void swap(kjb::Filter& a, kjb::Filter& b)
201 {
202  a.swap(b);
203 }
204 
205 } // namespace std
206 
207 #endif /*KJB_CPP_FILTER_H */
208 
Filter(const Filter &src)
Copy ctor, performs a deep copy.
Definition: i_filter.h:63
Value_type & at(int row, int col)
Access value lvalue at row, column coordinates.
Definition: i_filter.h:145
~Filter()
Destroy filter's memory.
Definition: i_filter.h:70
Value_type at(int row, int col) const
Access value rvalue at row, column coordinates.
Definition: i_filter.h:155
Definition for the Matrix class, a thin wrapper on the KJB Matrix struct and its related functionalit...
Value_type operator()(int row, int col) const
Rvalue value access at given row & column, no bounds-checking.
Definition: i_filter.h:122
Object thrown when an index argument exceeds the size of a container.
Definition: l_exception.h:399
double Value_type
data type of the elements
Definition: m_matrix.h:108
int get_num_rows() const
Return the number of rows in the matrix.
Definition: m_matrix.h:543
Value_type & operator()(int row, int col)
Lvalue value access at given row & column, no bounds-checking.
Definition: i_filter.h:115
Filter(const std::string &fname)
Read filter from a named file.
Definition: i_filter.h:58
Filter(int rows, int cols)
Construct filter of specified size, defaults to zero by zero.
Definition: i_filter.h:43
Filter & operator=(const kjb::Matrix &src)
Assignment of a matrix to a filter.
Definition: i_filter.h:93
Filter & operator=(const Filter &src)
Deep copy assignment.
Definition: i_filter.h:104
#define KJB_THROW(ex)
Definition: l_exception.h:46
friend Image operator*(const Image &, const Filter &)
Convolve an image with a filter.
Definition: i_filter.cpp:51
Filter(const Matrix &src)
Construct filter from matrix.
Definition: i_filter.h:48
void write(std::string fname)
Write to a file.
Definition: i_filter.h:163
Filter(const char *fname)
Read filter from a named file.
Definition: i_filter.h:53
int get_num_cols() const
Return the number of columns in the matrix.
Definition: m_matrix.h:554
Filter laplacian_of_gaussian_filter(int size, double sigma)
Create a Laplacian of Gaussian filter.
Definition: i_filter.cpp:30
Filter class.
Definition: i_filter.h:36
void swap(kjb::Gsl_Multimin_fdf &m1, kjb::Gsl_Multimin_fdf &m2)
Swap two wrapped multimin objects.
Definition: gsl_multimin.h:693
Image gauss_sample_image(const Image &in, int resolution, double sigma)
this wraps C function kjb_c::gauss_sample_image (q.v.).
Definition: i_filter.cpp:68
int write(const char *filename=0) const
Writes the matrix contents to a file specified by name.
Definition: m_matrix.h:1021
void check_bounds(int row, int col) const
Test whether row, column coordinates are valid.
Definition: i_filter.h:132
int get_num_rows() const
Return the number of rows in the filter.
Definition: i_filter.h:81
Matrix::Value_type Value_type
Definition: i_filter.h:40
int get_num_cols() const
Return the number of columns in the filter.
Definition: i_filter.h:87
This class implements matrices, in the linear-algebra sense, with real-valued elements.
Definition: m_matrix.h:94
Code for a wrapper class around the C struct KJB_Image.
Support for error handling exception classes in libKJB.
Wrapped version of the C struct KJB_image.
Definition: i_image.h:76
Filter gaussian_filter(double sigma, int size)
Create a Gaussian filter with given sigma and size.
Definition: i_filter.cpp:17
Gsl_Vector operator*(double scalar, const Gsl_Vector &vector)
multiply scalar and vector, scalar written on the left side
Definition: gsl_vector.h:661
void swap(Filter &other)
Swap the implementation of two filters.
Definition: i_filter.h:74