IMP  2.2.0
The Integrative Modeling Platform
CenteredMat.h
Go to the documentation of this file.
1 /**
2  * \file IMP/em2d/CenteredMat.h
3  * \brief Decorator for OpenCV matrix to use relative coordinates
4  * Copyright 2007-2014 IMP Inventors. All rights reserved.
5 */
6 
7 #ifndef IMPEM2D_CENTERED_MAT_H
8 #define IMPEM2D_CENTERED_MAT_H
9 
10 #include "IMP/em2d/em2d_config.h"
12 #include "IMP/base/exception.h"
13 #include "IMP/macros.h"
15 #include <iostream>
16 
17 IMPEM2D_BEGIN_NAMESPACE
18 
19 //! Decorator for a cv::Mat to use coordinates respect to a point
20 //! Almost always that point is the center
21 class CenteredMat {
22 
23  public:
24  /**
25  * Creates and CenteredMat for the matrx
26  * @param m The matrix
27  * @note The center pixel is not provided. It is assumed
28  * to be the center of the matrix
29  */
30  CenteredMat(cv::Mat &m) {
31  // m is not copied, just a reference is added.
32 
33  IMP_USAGE_CHECK((m.rows != 0 && m.cols != 0),
34  "CenteredMat: Matrix passed is empty");
35  centered_ = m;
36  center_row_ = static_cast<int>(0.5 * m.rows);
37  center_col_ = static_cast<int>(0.5 * m.cols);
38  set_starts_and_ends();
39  }
40 
41  /**
42  * Creates a CenteredMat for the matrx
43  * @param m The matrix
44  * @param center_row - The row for the point used as center of CenteredMat
45  * @param center_col - The column for the point used as center of CenteredMat
46  * @return
47  */
48  CenteredMat(cv::Mat &m, int center_row, int center_col) {
49  centered_ = m;
50  if (center_row >= 0 && center_row < m.rows && center_col >= 0 &&
51  center_col < m.cols) {
52  center_row_ = center_row;
53  center_col_ = center_col;
54  } else {
55  IMP_THROW("CenteredMat: Center index out of range ", ValueException);
56  }
57  set_starts_and_ends();
58  }
59 
60  /**
61  * get the starting value for a dimension.
62  //! For example, in a matrix of 5x5 centered at the origin (2,2), the
63  //! starting point will be (-2,2)
64  * @param i The dimension to use (0 - rows, 1 - columns)
65  * @return The starting point in the given dimension
66  */
67  int get_start(int i) const { return start_[i]; }
68 
69  /**
70  * See get_start() function help. In the example of the 5x5 matrix,
71  * the end values would be (2,2)
72  * @param i The dimension to use (0 - rows, 1 - columns)
73  * @return The end point in the given dimension
74  */
75  int get_end(int i) const { return end_[i]; }
76 
77  /**
78  * Returns true if the indices are in the matrix. Remember that the indices
79  * are those respect to the center of CenteredMat
80  * @param i Row
81  * @param j Column
82  * @return True of False
83  */
84  bool get_is_in_range(int i, int j) const {
85  if (i < get_start(0) || i > get_end(0)) return false;
86  if (j < get_start(1) || j > get_end(1)) return false;
87  return true;
88  }
89 
90  /**
91  * Returns the element (i,j) RELATIVE to the center. Remember then that
92  * the indices can be negative. For performance the indices out of range
93  * are NOT checked
94  * @param i row
95  * @param j column
96  * @return the value of the matrix at row i and column j
97  */
98  double &operator()(int i, int j) {
99  // if (!get_is_in_range(i,j)) {
100  // IMP_THROW("CenteredMat () : Index out of range",ValueException);
101  // }
102  return centered_.at<double>(center_row_ + i, center_col_ + j);
103  }
104 
105  /**
106  * Shows information about the class
107  * @param out Stream used to show the information
108  */
109  void do_show(std::ostream &out) const {
110  out << "Matrix of size: (" << centered_.rows << "," << centered_.cols
111  << ") centered mat at: (" << center_row_ << "," << center_col_
112  << ") start (" << start_[0] << "," << start_[1] << ") end (" << end_[0]
113  << "," << end_[1] << ")" << std::endl;
114  }
115 
116  IMP_SHOWABLE_INLINE(CenteredMat, do_show(out));
117 
118  protected:
119  /**
120  * Sets the starting and ending points for the object. See get_start() and
121  * get_end() for more help
122  */
124  start_[0] = -center_row_;
125  start_[1] = -center_col_;
126  end_[0] = centered_.rows - 1 - center_row_;
127  end_[1] = centered_.cols - 1 - center_col_;
128  }
129 
130  int center_row_, center_col_; // center pixel
131  cv::Mat centered_;
132  // starts and ends
133  int start_[2], end_[2];
134 };
135 
136 IMPEM2D_END_NAMESPACE
137 
138 #endif /* IMPEM2D_CENTERED_MAT_H */
int get_start(int i) const
Definition: CenteredMat.h:67
CenteredMat(cv::Mat &m)
Definition: CenteredMat.h:30
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
double & operator()(int i, int j)
Definition: CenteredMat.h:98
inteface with OpenCV Copyright 2007-2014 IMP Inventors. All rights reserved.
Import IMP/kernel/macros.h in the namespace.
void do_show(std::ostream &out) const
Definition: CenteredMat.h:109
#define IMP_USAGE_CHECK(expr, message)
A runtime test for incorrect usage of a class or method.
CenteredMat(cv::Mat &m, int center_row, int center_col)
Definition: CenteredMat.h:48
int get_end(int i) const
Definition: CenteredMat.h:75
Exception definitions and assertions.
#define IMP_THROW(message, exception_name)
Throw an exception with a message.
Various general useful macros for IMP.
bool get_is_in_range(int i, int j) const
Definition: CenteredMat.h:84