00001
00020 #ifndef NTK_OPENCV_UTILS_H
00021 #define NTK_OPENCV_UTILS_H
00022
00023 #include <ntk/core.h>
00024
00025 # define for_all_rc(im) \
00026 for (int r = 0; r < (im).rows; ++r) \
00027 for (int c = 0; c < (im).cols; ++c)
00028
00029
00030 # define for_all_drc(im) \
00031 for (int d = 0; d < (im).size[0]; ++d) \
00032 for (int r = 0; r < (im).size[1]; ++r) \
00033 for (int c = 0; c < (im).size[2]; ++c)
00034
00035 inline bool operator<(const cv::Point2i& p1, const cv::Point2i& p2)
00036 {
00037 if (p1.x == p2.x) return p1.y < p2.y;
00038 return p1.x < p2.x;
00039 }
00040
00041 namespace ntk
00042 {
00043
00044 template <class T>
00045 class Rect3_
00046 {
00047 public:
00048 Rect3_() :
00049 x(0), y(0), z(0),
00050 width(-1), height(-1), depth(-1)
00051 {}
00052
00053 void extendToInclude(const cv::Point3f& p)
00054 {
00055 if (width < T(0) || height < T(0) || depth < T(0))
00056 {
00057 x = p.x;
00058 y = p.y;
00059 z = p.z;
00060 width = 1e-5;
00061 height = 1e-5;
00062 depth = 1e-5;
00063 }
00064 else
00065 {
00066 T min_x = std::min(x, p.x);
00067 T max_x = std::max(x+width, p.x);
00068 T min_y = std::min(y, p.y);
00069 T max_y = std::max(y+height, p.y);
00070 T min_z = std::min(z, p.z);
00071 T max_z = std::max(z+depth, p.z);
00072
00073 x = min_x; width = max_x - min_x;
00074 y = min_y; height = max_y - min_y;
00075 z = min_z; depth = max_z - min_z;
00076 }
00077 }
00078
00079 bool isEmpty() const
00080 {
00081 return width < 0 || height < 0 || depth < 0;
00082 }
00083
00084 cv::Point3_<T> centroid() const
00085 {
00086 return cv::Point3_<T>(x+(width/T(2)), y+(height/T(2)), z + (depth / T(2)));
00087 }
00088
00089 public:
00090 T x,y,z,width,height,depth;
00091 };
00092
00093 typedef Rect3_<float> Rect3f;
00094
00095 ntk::Rect3f bounding_box(const std::vector<cv::Point3f>& points);
00096
00097 }
00098
00099 namespace ntk
00100 {
00101
00102 inline cv::Point2f box_center(const cv::Rect_<float>& bbox)
00103 {
00104 return cv::Point2f(bbox.x + bbox.width/2.0, bbox.y + bbox.height/2.0);
00105 }
00106
00107 template <class ScalarType>
00108 cv::Vec3d toVec3d(const cv::Mat_<ScalarType>& m)
00109 {
00110
00111 return cv::Vec3d(m(0,0), m(1,0), m(2,0));
00112 }
00113
00114 template <class ScalarType>
00115 cv::Vec2f toVec2f(const cv::Mat_<ScalarType>& m)
00116 {
00117
00118 return cv::Vec2f(m(0,0), m(1,0));
00119 }
00120
00121 template <class ScalarType1, class ScalarType2>
00122 void copyMatWithCast(cv::Mat_<ScalarType1>& dest, const cv::Mat_<ScalarType2>& src)
00123 {
00124
00125 for_all_rc(src)
00126 {
00127 dest(r,c) = src(r,c);
00128 }
00129 }
00130
00131 cv::Mat4b toMat4b(const cv::Mat3b& im);
00132 cv::Mat3b toMat3b(const cv::Mat4b& im);
00133
00134 void apply_mask(cv::Mat1b& im, const cv::Mat1b& mask);
00135 void apply_mask(cv::Mat3b& im, const cv::Mat1b& mask);
00136 void apply_mask(cv::Mat1f& im, const cv::Mat1b& mask);
00137
00138 void read_from_yaml(cv::FileNode node, cv::Vec3f& v);
00139 void write_to_yaml(cv::FileStorage& output_file, const std::string& name, const cv::Vec3f& v);
00140
00141 void read_from_yaml(cv::FileNode node, cv::Rect& v);
00142 void write_to_yaml(cv::FileStorage& output_file, const std::string& name, const cv::Rect& v);
00143
00144 void read_from_yaml(cv::FileNode node, bool& b);
00145 void write_to_yaml(cv::FileStorage& output_file, const std::string& name, bool b);
00146
00147 void read_from_yaml(cv::FileNode node, int& b);
00148 void write_to_yaml(cv::FileStorage& output_file, const std::string& name, int b);
00149
00150 void read_from_yaml(cv::FileNode node, double& b);
00151 void write_to_yaml(cv::FileStorage& output_file, const std::string& name, double b);
00152
00153 void read_from_yaml(cv::FileNode node, cv::Mat& matrix);
00154 void write_to_yaml(cv::FileStorage& output_file, const std::string& name, const cv::Mat& matrix);
00155
00159 void writeMatrix(cv::FileStorage& output_file, const std::string& name, const cv::Mat& matrix);
00160 void readMatrix(cv::FileStorage& input_file, const std::string& name, cv::Mat& matrix);
00161
00162 cv::Mat1b normalize_toMat1b(const cv::Mat1f& image);
00163 cv::Mat3b toMat3b(const cv::Mat1b& image);
00164
00165 void imwrite_normalized(const std::string& filename, const cv::Mat1f& m);
00166 void imshow_normalized(const std::string& window_name, const cv::Mat1f& m);
00167
00168 void imwrite_yml(const std::string& filename, const cv::Mat& image);
00169 cv::Mat imread_yml(const std::string& filename);
00170
00172 cv::Mat1f imread_Mat1f_raw(const std::string& filename);
00174 void imwrite_Mat1f_raw(const std::string& filename, const cv::Mat1f& m);
00175
00176 inline cv::Mat getCvByteImage(int width, int height) { return cv::Mat(height, width, CV_8UC1); }
00177 inline cv::Mat getCvFloatImage(int width, int height) { return cv::Mat(height, width, CV_32FC1); }
00178 inline cv::Mat getCvColorByteImage(int width, int height) { return cv::Mat(height, width, CV_8UC3); }
00179 inline cv::Mat getCvColorFloatImage(int width, int height) { return cv::Mat(height, width, CV_32FC3); }
00180
00181 double overlap_ratio(const cv::Rect_<float>& r1, const cv::Rect_<float>& r2);
00182
00183 inline bool is_yx_in_range(const cv::Mat& image, int y, int x)
00184 { return (x >= 0) && (y >= 0) && (x < image.cols) && (y < image.rows); }
00185
00186 inline void normalize(cv::Vec3f& v)
00187 {
00188 v *= float(1.0 / (sqrt(v.dot(v))));
00189 }
00190
00191 inline cv::Vec3b bgr_to_rgb(const cv::Vec3b& v)
00192 { return cv::Vec3b(v[2], v[1], v[0]); }
00193
00194 void adjustRectToImage(cv::Rect& rect, const cv::Size& image_size);
00195
00196 }
00197
00198 #endif // NTK_OPENCV_UTILS_H