Commit c979de1e authored by Andrey Kamaev's avatar Andrey Kamaev

Rewrite Mat formatting without std streams

parent 7193a73c
...@@ -400,96 +400,80 @@ template<typename _Tp> static inline _Tp randu() ...@@ -400,96 +400,80 @@ template<typename _Tp> static inline _Tp randu()
////////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////////
class CV_EXPORTS Formatter class CV_EXPORTS Formatted
{ {
public: public:
virtual ~Formatter() {} virtual const char* next() = 0;
virtual void write(std::ostream& out, const Mat& m, const int* params=0, int nparams=0) const = 0; virtual void reset() = 0;
virtual void write(std::ostream& out, const void* data, int nelems, int type, virtual ~Formatted();
const int* params=0, int nparams=0) const = 0;
static const Formatter* get(const char* fmt="");
static const Formatter* setDefault(const Formatter* fmt);
}; };
struct CV_EXPORTS Formatted class CV_EXPORTS Formatter
{ {
Formatted(const Mat& m, const Formatter* fmt, public:
const std::vector<int>& params); enum { FMT_MATLAB = 0,
Formatted(const Mat& m, const Formatter* fmt, FMT_CSV = 1,
const int* params=0); FMT_PYTHON = 2,
Mat mtx; FMT_NUMPY = 3,
const Formatter* fmt; FMT_C = 4,
std::vector<int> params; FMT_DEFAULT = FMT_MATLAB
}; };
static inline Formatted format(const Mat& mtx, const char* fmt, virtual ~Formatter();
const std::vector<int>& params=std::vector<int>())
{
return Formatted(mtx, Formatter::get(fmt), params);
}
template<typename _Tp> static inline Formatted format(const std::vector<Point_<_Tp> >& vec, virtual Ptr<Formatted> format(const Mat& mtx) const = 0;
const char* fmt, const std::vector<int>& params=std::vector<int>())
{ virtual void set32fPrecision(int p = 8) = 0;
return Formatted(Mat(vec), Formatter::get(fmt), params); virtual void set64fPrecision(int p = 16) = 0;
} virtual void setMultiline(bool ml = true) = 0;
template<typename _Tp> static inline Formatted format(const std::vector<Point3_<_Tp> >& vec, static Ptr<Formatter> get(int fmt = FMT_DEFAULT);
const char* fmt, const std::vector<int>& params=std::vector<int>())
};
static inline
Ptr<Formatted> format(InputArray mtx, int fmt)
{ {
return Formatted(Mat(vec), Formatter::get(fmt), params); return Formatter::get(fmt)->format(mtx.getMat());
} }
/** \brief prints Mat to the output stream in Matlab notation
* use like static inline
@verbatim std::ostream& operator << (std::ostream& out, Ptr<Formatted> fmtd)
Mat my_mat = Mat::eye(3,3,CV_32F);
std::cout << my_mat;
@endverbatim
*/
static inline std::ostream& operator << (std::ostream& out, const Mat& mtx)
{ {
Formatter::get()->write(out, mtx); fmtd->reset();
for(const char* str = fmtd->next(); str; str = fmtd->next())
out << str;
return out; return out;
} }
/** \brief prints Mat to the output stream allows in the specified notation (see format) static inline
* use like std::ostream& operator << (std::ostream& out, const Mat& mtx)
@verbatim
Mat my_mat = Mat::eye(3,3,CV_32F);
std::cout << my_mat;
@endverbatim
*/
static inline std::ostream& operator << (std::ostream& out, const Formatted& fmtd)
{ {
fmtd.fmt->write(out, fmtd.mtx); return out << Formatter::get()->format(mtx);
return out;
} }
template<typename _Tp> static inline
template<typename _Tp> static inline std::ostream& operator << (std::ostream& out, std::ostream& operator << (std::ostream& out, const std::vector<Point_<_Tp> >& vec)
const std::vector<Point_<_Tp> >& vec)
{ {
Formatter::get()->write(out, Mat(vec)); return out << Formatter::get()->format(Mat(vec));
return out;
} }
template<typename _Tp> static inline std::ostream& operator << (std::ostream& out, template<typename _Tp> static inline
const std::vector<Point3_<_Tp> >& vec) std::ostream& operator << (std::ostream& out, const std::vector<Point3_<_Tp> >& vec)
{ {
Formatter::get()->write(out, Mat(vec)); return out << Formatter::get()->format(Mat(vec));
return out;
} }
/** Writes a Matx to an output stream. /** Writes a Matx to an output stream.
*/ */
template<typename _Tp, int m, int n> inline std::ostream& operator<<(std::ostream& out, const Matx<_Tp, m, n>& matx) template<typename _Tp, int m, int n> inline
std::ostream& operator << (std::ostream& out, const Matx<_Tp, m, n>& matx)
{ {
out << cv::Mat(matx); return out << Formatter::get()->format(matx);
return out;
} }
/** Writes a point to an output stream in Matlab notation /** Writes a point to an output stream in Matlab notation
......
...@@ -227,6 +227,51 @@ public: ...@@ -227,6 +227,51 @@ public:
}; };
}; };
template<int depth> class TypeDepth {};
template<> class TypeDepth<CV_8U>
{
enum { depth = CV_8U };
typedef uchar value_type;
};
template<> class TypeDepth<CV_8S>
{
enum { depth = CV_8S };
typedef schar value_type;
};
template<> class TypeDepth<CV_16U>
{
enum { depth = CV_16U };
typedef ushort value_type;
};
template<> class TypeDepth<CV_16S>
{
enum { depth = CV_16S };
typedef short value_type;
};
template<> class TypeDepth<CV_32S>
{
enum { depth = CV_32S };
typedef int value_type;
};
template<> class TypeDepth<CV_32F>
{
enum { depth = CV_32F };
typedef float value_type;
};
template<> class TypeDepth<CV_64F>
{
enum { depth = CV_64F };
typedef double value_type;
};
} // cv } // cv
#endif // __OPENCV_CORE_TRAITS_HPP__ #endif // __OPENCV_CORE_TRAITS_HPP__
This diff is collapsed.
...@@ -378,6 +378,7 @@ protected: ...@@ -378,6 +378,7 @@ protected:
TEST(Core_InputOutput, write_read_consistency) { Core_IOTest test; test.safe_run(); } TEST(Core_InputOutput, write_read_consistency) { Core_IOTest test; test.safe_run(); }
extern void testFormatter();
class CV_MiscIOTest : public cvtest::BaseTest class CV_MiscIOTest : public cvtest::BaseTest
{ {
......
...@@ -1501,8 +1501,8 @@ static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_da ...@@ -1501,8 +1501,8 @@ static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_da
{ {
// TODO move this logic to CvImageWidget // TODO move this logic to CvImageWidget
CvWindow* window = (CvWindow*)user_data; CvWindow* window = (CvWindow*)user_data;
CvPoint2D32f pt32f = {-1., -1.}; CvPoint2D32f pt32f(-1., -1.);
CvPoint pt = {-1,-1}; CvPoint pt(-1,-1);
int cv_event = -1, state = 0; int cv_event = -1, state = 0;
CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget ); CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget );
......
...@@ -30,16 +30,16 @@ int main(int,char**) ...@@ -30,16 +30,16 @@ int main(int,char**)
help(); help();
Mat I = Mat::eye(4, 4, CV_64F); Mat I = Mat::eye(4, 4, CV_64F);
I.at<double>(1,1) = CV_PI; I.at<double>(1,1) = CV_PI;
cout << "I = " << I << ";" << endl; cout << "I = \n" << I << ";" << endl << endl;
Mat r = Mat(10, 3, CV_8UC3); Mat r = Mat(10, 3, CV_8UC3);
randu(r, Scalar::all(0), Scalar::all(255)); randu(r, Scalar::all(0), Scalar::all(255));
cout << "r (default) = " << r << ";" << endl << endl; cout << "r (default) = \n" << r << ";" << endl << endl;
cout << "r (python) = " << format(r,"python") << ";" << endl << endl; cout << "r (python) = \n" << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl;
cout << "r (numpy) = " << format(r,"numpy") << ";" << endl << endl; cout << "r (numpy) = \n" << format(r, Formatter::FMT_NUMPY) << ";" << endl << endl;
cout << "r (csv) = " << format(r,"csv") << ";" << endl << endl; cout << "r (csv) = \n" << format(r, Formatter::FMT_CSV) << ";" << endl << endl;
cout << "r (c) = " << format(r,"C") << ";" << endl << endl; cout << "r (c) = \n" << format(r, Formatter::FMT_C) << ";" << endl << endl;
Point2f p(5, 1); Point2f p(5, 1);
cout << "p = " << p << ";" << endl; cout << "p = " << p << ";" << endl;
......
...@@ -59,10 +59,10 @@ int main(int,char**) ...@@ -59,10 +59,10 @@ int main(int,char**)
// Demonstrate the output formating options // Demonstrate the output formating options
cout << "R (default) = " << endl << R << endl << endl; cout << "R (default) = " << endl << R << endl << endl;
cout << "R (python) = " << endl << format(R,"python") << endl << endl; cout << "R (python) = " << endl << format(R, Formatter::FMT_PYTHON) << endl << endl;
cout << "R (numpy) = " << endl << format(R,"numpy" ) << endl << endl; cout << "R (numpy) = " << endl << format(R, Formatter::FMT_NUMPY ) << endl << endl;
cout << "R (csv) = " << endl << format(R,"csv" ) << endl << endl; cout << "R (csv) = " << endl << format(R, Formatter::FMT_CSV ) << endl << endl;
cout << "R (c) = " << endl << format(R,"C" ) << endl << endl; cout << "R (c) = " << endl << format(R, Formatter::FMT_C ) << endl << endl;
Point2f P(5, 1); Point2f P(5, 1);
cout << "Point (2D) = " << P << endl << endl; cout << "Point (2D) = " << P << endl << endl;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment