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()
//////////////////////////////////////////////////////////////////////////////
class CV_EXPORTS Formatter
class CV_EXPORTS Formatted
{
public:
virtual ~Formatter() {}
virtual void write(std::ostream& out, const Mat& m, const int* params=0, int nparams=0) const = 0;
virtual void write(std::ostream& out, const void* data, int nelems, int type,
const int* params=0, int nparams=0) const = 0;
static const Formatter* get(const char* fmt="");
static const Formatter* setDefault(const Formatter* fmt);
virtual const char* next() = 0;
virtual void reset() = 0;
virtual ~Formatted();
};
struct CV_EXPORTS Formatted
class CV_EXPORTS Formatter
{
Formatted(const Mat& m, const Formatter* fmt,
const std::vector<int>& params);
Formatted(const Mat& m, const Formatter* fmt,
const int* params=0);
Mat mtx;
const Formatter* fmt;
std::vector<int> params;
};
public:
enum { FMT_MATLAB = 0,
FMT_CSV = 1,
FMT_PYTHON = 2,
FMT_NUMPY = 3,
FMT_C = 4,
FMT_DEFAULT = FMT_MATLAB
};
static inline Formatted format(const Mat& mtx, const char* fmt,
const std::vector<int>& params=std::vector<int>())
{
return Formatted(mtx, Formatter::get(fmt), params);
}
virtual ~Formatter();
template<typename _Tp> static inline Formatted format(const std::vector<Point_<_Tp> >& vec,
const char* fmt, const std::vector<int>& params=std::vector<int>())
{
return Formatted(Mat(vec), Formatter::get(fmt), params);
}
virtual Ptr<Formatted> format(const Mat& mtx) const = 0;
virtual void set32fPrecision(int p = 8) = 0;
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,
const char* fmt, const std::vector<int>& params=std::vector<int>())
static Ptr<Formatter> get(int fmt = FMT_DEFAULT);
};
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
@verbatim
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)
static inline
std::ostream& operator << (std::ostream& out, Ptr<Formatted> fmtd)
{
Formatter::get()->write(out, mtx);
fmtd->reset();
for(const char* str = fmtd->next(); str; str = fmtd->next())
out << str;
return out;
}
/** \brief prints Mat to the output stream allows in the specified notation (see format)
* use like
@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)
static inline
std::ostream& operator << (std::ostream& out, const Mat& mtx)
{
fmtd.fmt->write(out, fmtd.mtx);
return out;
return out << Formatter::get()->format(mtx);
}
template<typename _Tp> static inline std::ostream& operator << (std::ostream& out,
const std::vector<Point_<_Tp> >& vec)
template<typename _Tp> static inline
std::ostream& operator << (std::ostream& out, const std::vector<Point_<_Tp> >& vec)
{
Formatter::get()->write(out, Mat(vec));
return out;
return out << Formatter::get()->format(Mat(vec));
}
template<typename _Tp> static inline std::ostream& operator << (std::ostream& out,
const std::vector<Point3_<_Tp> >& vec)
template<typename _Tp> static inline
std::ostream& operator << (std::ostream& out, const std::vector<Point3_<_Tp> >& vec)
{
Formatter::get()->write(out, Mat(vec));
return out;
return out << Formatter::get()->format(Mat(vec));
}
/** 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;
return out << Formatter::get()->format(matx);
}
/** Writes a point to an output stream in Matlab notation
......
......@@ -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
#endif // __OPENCV_CORE_TRAITS_HPP__
This diff is collapsed.
......@@ -378,6 +378,7 @@ protected:
TEST(Core_InputOutput, write_read_consistency) { Core_IOTest test; test.safe_run(); }
extern void testFormatter();
class CV_MiscIOTest : public cvtest::BaseTest
{
......
......@@ -1501,8 +1501,8 @@ static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_da
{
// TODO move this logic to CvImageWidget
CvWindow* window = (CvWindow*)user_data;
CvPoint2D32f pt32f = {-1., -1.};
CvPoint pt = {-1,-1};
CvPoint2D32f pt32f(-1., -1.);
CvPoint pt(-1,-1);
int cv_event = -1, state = 0;
CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget );
......
......@@ -30,16 +30,16 @@ int main(int,char**)
help();
Mat I = Mat::eye(4, 4, CV_64F);
I.at<double>(1,1) = CV_PI;
cout << "I = " << I << ";" << endl;
cout << "I = \n" << I << ";" << endl << endl;
Mat r = Mat(10, 3, CV_8UC3);
randu(r, Scalar::all(0), Scalar::all(255));
cout << "r (default) = " << r << ";" << endl << endl;
cout << "r (python) = " << format(r,"python") << ";" << endl << endl;
cout << "r (numpy) = " << format(r,"numpy") << ";" << endl << endl;
cout << "r (csv) = " << format(r,"csv") << ";" << endl << endl;
cout << "r (c) = " << format(r,"C") << ";" << endl << endl;
cout << "r (default) = \n" << r << ";" << endl << endl;
cout << "r (python) = \n" << format(r, Formatter::FMT_PYTHON) << ";" << endl << endl;
cout << "r (numpy) = \n" << format(r, Formatter::FMT_NUMPY) << ";" << endl << endl;
cout << "r (csv) = \n" << format(r, Formatter::FMT_CSV) << ";" << endl << endl;
cout << "r (c) = \n" << format(r, Formatter::FMT_C) << ";" << endl << endl;
Point2f p(5, 1);
cout << "p = " << p << ";" << endl;
......
......@@ -59,10 +59,10 @@ int main(int,char**)
// Demonstrate the output formating options
cout << "R (default) = " << endl << R << endl << endl;
cout << "R (python) = " << endl << format(R,"python") << endl << endl;
cout << "R (numpy) = " << endl << format(R,"numpy" ) << endl << endl;
cout << "R (csv) = " << endl << format(R,"csv" ) << endl << endl;
cout << "R (c) = " << endl << format(R,"C" ) << endl << endl;
cout << "R (python) = " << endl << format(R, Formatter::FMT_PYTHON) << endl << endl;
cout << "R (numpy) = " << endl << format(R, Formatter::FMT_NUMPY ) << endl << endl;
cout << "R (csv) = " << endl << format(R, Formatter::FMT_CSV ) << endl << endl;
cout << "R (c) = " << endl << format(R, Formatter::FMT_C ) << endl << endl;
Point2f P(5, 1);
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