00001
00024 #ifndef __D_CV_TYPES__
00025 #define __D_CV_TYPES__
00026
00027 #include <opencv/cv.h>
00028 #include <string>
00029
00030 namespace DUtilsCV
00031 {
00032
00034 class Types
00035 {
00036 public:
00037
00044 template<class T>
00045 static void vectorize(const cv::Mat &V, T* r);
00046
00051 static std::string type(const cv::Mat &m);
00052
00053 protected:
00054
00061 template<class TM, class T>
00062 static void _vectorize(const cv::Mat &V, T* r);
00063
00064 };
00065
00066
00067
00068 template<class T>
00069 void Types::vectorize(const cv::Mat &V, T* r)
00070 {
00071 switch(V.type())
00072 {
00073 case CV_8U: Types::_vectorize<unsigned char, T>(V, r); break;
00074 case CV_8S: Types::_vectorize<char, T>(V, r); break;
00075 case CV_16U: Types::_vectorize<unsigned short, T>(V, r); break;
00076 case CV_16S: Types::_vectorize<short, T>(V, r); break;
00077 case CV_32S: Types::_vectorize<int, T>(V, r); break;
00078 case CV_32F: Types::_vectorize<float, T>(V, r); break;
00079 case CV_64F: Types::_vectorize<double, T>(V, r); break;
00080 }
00081 }
00082
00083
00084
00085 template<class TM, class T>
00086 void Types::_vectorize(const cv::Mat &V, T* r)
00087 {
00088 if(V.empty() || V.cols == 0 || V.rows == 0) return;
00089
00090 if(V.rows > 1 && V.cols > 1)
00091 {
00092 int i = 0;
00093 for(int row = 0; row < V.rows; ++row)
00094 for(int col = 0; col < V.cols; ++col, ++i)
00095 r[i] = V.at<TM>(row, col);
00096 }
00097 else
00098 {
00099 if(V.rows > 1)
00100 {
00101 for(int i = 0; i < V.rows; ++i) r[i] = (T)V.at<TM>(i, 0);
00102 }
00103 else
00104 {
00105 for(int i = 0; i < V.cols; ++i) r[i] = (T)V.at<TM>(0, i);
00106 }
00107 }
00108
00109 }
00110
00111
00112
00113 }
00114
00115 #endif
00116