From 08b7855edc382152586919b1662ab73f41f595f5 Mon Sep 17 00:00:00 2001 From: Vadim Pisarevsky Date: Mon, 15 Nov 2010 17:40:57 +0000 Subject: [PATCH] extended out << mat/vec operators; added opencv license --- modules/core/include/opencv2/core/core.hpp | 1 - modules/core/include/opencv2/core/cvout.hpp | 79 ---- .../core/include/opencv2/core/operations.hpp | 101 +++++ modules/core/src/cvout.cpp | 387 +++++++++++++----- modules/features2d/src/brief.cpp | 52 ++- samples/cpp/cvout_sample.cpp | 61 +-- 6 files changed, 455 insertions(+), 226 deletions(-) delete mode 100644 modules/core/include/opencv2/core/cvout.hpp diff --git a/modules/core/include/opencv2/core/core.hpp b/modules/core/include/opencv2/core/core.hpp index fb4c403112..456b46aa0b 100644 --- a/modules/core/include/opencv2/core/core.hpp +++ b/modules/core/include/opencv2/core/core.hpp @@ -4055,6 +4055,5 @@ public: #include "opencv2/core/operations.hpp" #include "opencv2/core/mat.hpp" -#include "opencv2/core/cvout.hpp" #endif /*__OPENCV_CORE_HPP__*/ diff --git a/modules/core/include/opencv2/core/cvout.hpp b/modules/core/include/opencv2/core/cvout.hpp deleted file mode 100644 index 6f869ae32b..0000000000 --- a/modules/core/include/opencv2/core/cvout.hpp +++ /dev/null @@ -1,79 +0,0 @@ -#ifndef __OPENCV_CORE_CVOUT_HPP__ -#define __OPENCV_CORE_CVOUT_HPP__ -#ifdef __cplusplus - -#ifndef SKIP_INCLUDES - #include - #include - #include -#endif - -namespace cv -{ - -/** Writes a point to an output stream in Matlab notation - */ -inline std::ostream & operator<<(std::ostream & out, const Point2f & p) -{ - out << "[ " << p.x << "," << p.y << " ]"; - return out; -} - -/** Writes a point to an output stream in Matlab notation - */ -inline std::ostream & operator<<(std::ostream & out, const Point3f & p) -{ - out << "[ " << p.x << "," << p.y << "," << p.z << " ]"; - return out; -} - -/** \brief write points to and output stream - * \param out typically cout - * \param points the points to be written to the stream - * \return the stream - **/ -CV_EXPORTS std::ostream & operator<<(std::ostream & out, const std::vector & points); - -/** \brief write points to and output stream - * \param out typically cout - * \param points the points to be written to the stream - * \return the stream - **/ -std::ostream & operator<<(std::ostream & out, const std::vector & points); - -/** \brief allows each output of Mat in Matlab for Mat to std::cout - * use like - @verbatim - Mat my_mat = Mat::eye(3,3,CV_32F); - std::cout << my_mat; - @endverbatim - */ -CV_EXPORTS std::ostream & operator<<(std::ostream & out, const Mat & mat); - -/** \brief write a Mat in csv compatible for Matlab. - This means that the rows are seperated by newlines and the - columns by commas .... - 331.413896619595,0,122.365880226491 - 0,249.320451610369,122.146722131871 - 0,0,1 - - * \param out output stream to write to - * \param Mat write a Mat to a csv - */ -CV_EXPORTS std::ostream & writeCSV(std::ostream & out, const Mat & mat); - -/** \brief write a vector of points to an - output stream if possible - **/ -CV_EXPORTS std::ostream & writeCSV(std::ostream & out, const std::vector & points); - -/** \brief write a vector of points to an - output stream if possible - **/ -CV_EXPORTS std::ostream & writeCSV(std::ostream & out, const std::vector & points); - -} //namespace cv - -#endif - -#endif diff --git a/modules/core/include/opencv2/core/operations.hpp b/modules/core/include/opencv2/core/operations.hpp index 1029a3ff02..e31d619a40 100644 --- a/modules/core/include/opencv2/core/operations.hpp +++ b/modules/core/include/opencv2/core/operations.hpp @@ -3495,6 +3495,107 @@ public: return new _ClsName(*(const _ClsName*)ptr); } }; + + +class CV_EXPORTS Formatter +{ +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); +}; + + +struct CV_EXPORTS Formatted +{ + Formatted(const Mat& m, const Formatter* fmt, + const vector& params); + Formatted(const Mat& m, const Formatter* fmt, + const int* params=0); + Mat mtx; + const Formatter* fmt; + vector params; +}; + + +/** Writes a point to an output stream in Matlab notation + */ +template inline std::ostream& operator<<(std::ostream& out, const Point_<_Tp>& p) +{ + out << "[" << p.x << ", " << p.y << "]"; + return out; +} + +/** Writes a point to an output stream in Matlab notation + */ +template inline std::ostream& operator<<(std::ostream& out, const Point3_<_Tp>& p) +{ + out << "[" << p.x << ", " << p.y << ", " << p.z << "]"; + return out; +} + +static inline Formatted format(const Mat& mtx, const char* fmt, + const vector& params=vector()) +{ + return Formatted(mtx, Formatter::get(fmt), params); +} + +template static inline Formatted format(const vector >& vec, + const char* fmt, const vector& params=vector()) +{ + return Formatted(Mat(vec), Formatter::get(fmt), params); +} + +template static inline Formatted format(const vector >& vec, + const char* fmt, const vector& params=vector()) +{ + return Formatted(Mat(vec), Formatter::get(fmt), params); +} + +/** \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) +{ + Formatter::get()->write(out, mtx); + 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) +{ + fmtd.fmt->write(out, fmtd.mtx); + return out; +} + + +template static inline std::ostream& operator << (std::ostream& out, + const vector >& vec) +{ + Formatter::get()->write(out, Mat(vec)); + return out; +} + + +template static inline std::ostream& operator << (std::ostream& out, + const vector >& vec) +{ + Formatter::get()->write(out, Mat(vec)); + return out; +} } diff --git a/modules/core/src/cvout.cpp b/modules/core/src/cvout.cpp index 7f6b60780e..eeca5a1fa2 100644 --- a/modules/core/src/cvout.cpp +++ b/modules/core/src/cvout.cpp @@ -1,138 +1,305 @@ -#include "opencv2/core/core.hpp" +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ + +#include "precomp.hpp" +#include namespace cv { -namespace -{ -template - std::ostream & writevec(std::ostream & out, const std::vector & points) - { - typedef T MT_T; - typedef typename std::vector::const_iterator CIT; - /* Draw Me: - plot2( pts1(:,1),pts1(:,2),'r.') */ - std::streamsize pp = out.precision(); - out.precision(15); - out << "["; - - CIT it = points.begin(); - - for (; it != points.end(); ++it) - { - - out << *it; - CIT next = it; - if (++next != points.end()) - { - out << " "; - } - } - out << "]"; - out.precision(pp); - return out; - } - -std::ostream & writeelem(std::ostream & out, const Mat & mat, int i, int j) +static inline char getCloseBrace(char c) { - if (mat.type() == CV_32F) - out << mat.at (i, j); - else if (mat.type() == CV_64F) - out << mat.at (i, j); - else if (mat.type() == CV_32S) - out << mat.at (i, j); - else if (mat.type() == CV_8U) - out << int(mat.at (i, j)); - else if (mat.type() == CV_16U) - out << int(mat.at (i, j)); - else - out << "?"; - return out; + return c == '[' ? ']' : c == '(' ? ')' : c == '{' ? '}' : '\0'; } -} -std::ostream & operator<<(std::ostream & out, const std::vector & points) +template static void writeElems(std::ostream& out, const _Tp* data, + int nelems, int cn, char obrace, char cbrace) { - return writevec(out, points); + typedef typename DataType<_Tp>::work_type _WTp; + nelems *= cn; + for(int i = 0; i < nelems; i += cn) + { + if(cn == 1) + { + out << (_WTp)data[i] << (i+1 < nelems ? ", " : ""); + continue; + } + out << obrace; + for(int j = 0; j < cn; j++) + out << (_WTp)data[i + j] << (j+1 < cn ? ", " : ""); + out << cbrace << (i+cn < nelems ? ", " : ""); + } } -std::ostream & operator<<(std::ostream & out, const std::vector & points) + + +static void writeElems(std::ostream& out, const void* data, int nelems, int type, char brace) { - return writevec(out, points); + int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); + char cbrace = ' '; + if(!brace || isspace(brace)) + { + nelems *= cn; + cn = 1; + } + else + cbrace = getCloseBrace(brace); + if(depth == CV_8U) + writeElems(out, (const uchar*)data, nelems, cn, brace, cbrace); + else if(depth == CV_8S) + writeElems(out, (const schar*)data, nelems, cn, brace, cbrace); + else if(depth == CV_16U) + writeElems(out, (const ushort*)data, nelems, cn, brace, cbrace); + else if(depth == CV_16S) + writeElems(out, (const short*)data, nelems, cn, brace, cbrace); + else if(depth == CV_32S) + writeElems(out, (const int*)data, nelems, cn, brace, cbrace); + else if(depth == CV_32F) + { + std::streamsize pp = out.precision(); + out.precision(8); + writeElems(out, (const float*)data, nelems, cn, brace, cbrace); + out.precision(pp); + } + else if(depth == CV_64F) + { + std::streamsize pp = out.precision(); + out.precision(16); + writeElems(out, (const double*)data, nelems, cn, brace, cbrace); + out.precision(pp); + } + else + CV_Error(CV_StsUnsupportedFormat, ""); } -std::ostream & operator<<(std::ostream & out, const Mat & _mat) + +static void writeMat(std::ostream& out, const Mat& m, char rowsep, char elembrace, bool singleLine) { - std::streamsize pp = out.precision(); - out.precision(15); - std::vector channels; - split(_mat, channels); - for (int chn = 0; chn < _mat.channels(); chn++) - { - Mat mat = channels[chn]; - out << "["; - for (int i = 0; i < mat.rows; i++) - { - for (int j = 0; j < mat.cols; j++) - { - writeelem(out,mat,i,j); - if (j < mat.cols - 1) - out << " "; - } - if (i < mat.rows - 1) - out << ";\n"; - } - out << "]"; - } - out.precision(pp); - return out; + CV_Assert(m.dims <= 2); + int type = m.type(); + + char crowbrace = getCloseBrace(rowsep); + char orowbrace = crowbrace ? rowsep : '\0'; + + if( orowbrace || isspace(rowsep) ) + rowsep = '\0'; + + for( int i = 0; i < m.rows; i++ ) + { + if(orowbrace) + out << orowbrace; + writeElems(out, m.ptr(i), m.cols, type, elembrace); + if(orowbrace) + out << crowbrace << (i+1 < m.rows ? ", " : ""); + if(i+1 < m.rows) + { + if(rowsep) + out << rowsep << (singleLine ? " " : ""); + if(!singleLine) + out << "\n "; + } + } } -std::ostream & writeCSV(std::ostream & out, const std::vector & points) +class MatlabFormatter : public Formatter { - std::streamsize pp = out.precision(); - out.precision(15); - std::vector::const_iterator it = points.begin(); - - for (; it != points.end(); ++it) - { - out << it->x << "," << it->y << ","<< it->z << "\n"; - } - out.precision(pp); - return out; -} +public: + virtual ~MatlabFormatter() {} + void write(std::ostream& out, const Mat& m, const int*, int) const + { + out << "["; + writeMat(out, m, ';', ' ', m.cols == 1); + out << "]"; + } + + void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const + { + writeElems(out, data, nelems, type, ' '); + } +}; + +class PythonFormatter : public Formatter +{ +public: + virtual ~PythonFormatter() {} + void write(std::ostream& out, const Mat& m, const int*, int) const + { + out << "["; + writeMat(out, m, m.cols > 1 ? '[' : ' ', '[', m.cols*m.channels() == 1); + out << "]"; + } + + void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const + { + writeElems(out, data, nelems, type, '['); + } +}; + -std::ostream & writeCSV(std::ostream & out, const std::vector & points) +class NumpyFormatter : public Formatter { - std::streamsize pp = out.precision(); - out.precision(15); - std::vector::const_iterator it = points.begin(); - - for (; it != points.end(); ++it) - { - out << it->x << "," << it->y << "\n"; - } - out.precision(pp); - return out; +public: + virtual ~NumpyFormatter() {} + void write(std::ostream& out, const Mat& m, const int*, int) const + { + static const char* numpyTypes[] = + { + "uint8", "int8", "uint16", "int16", "int32", "float32", "float64", "uint64" + }; + out << "array(["; + writeMat(out, m, m.cols > 1 ? '[' : ' ', '[', m.cols*m.channels() == 1); + out << "], type='" << numpyTypes[m.depth()] << "')"; + } + + void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const + { + writeElems(out, data, nelems, type, '['); + } +}; + + +class CSVFormatter : public Formatter +{ +public: + virtual ~CSVFormatter() {} + void write(std::ostream& out, const Mat& m, const int*, int) const + { + writeMat(out, m, ' ', ' ', m.cols*m.channels() == 1); + if(m.rows > 1) + out << "\n"; + } + + void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const + { + writeElems(out, data, nelems, type, ' '); + } +}; + + +class CFormatter : public Formatter +{ +public: + virtual ~CFormatter() {} + void write(std::ostream& out, const Mat& m, const int*, int) const + { + out << "{"; + writeMat(out, m, ',', ' ', m.cols==1); + out << "}"; + } + + void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const + { + writeElems(out, data, nelems, type, ' '); + } +}; + + +static MatlabFormatter matlabFormatter; +static PythonFormatter pythonFormatter; +static NumpyFormatter numpyFormatter; +static CSVFormatter csvFormatter; +static CFormatter cFormatter; + +static const Formatter* g_defaultFormatter0 = &matlabFormatter; +static const Formatter* g_defaultFormatter = &matlabFormatter; + +bool my_streq(const char* a, const char* b) +{ + size_t i, alen = strlen(a), blen = strlen(b); + if( alen != blen ) + return false; + for( i = 0; i < alen; i++ ) + if( a[i] != b[i] && a[i] - 32 != b[i] ) + return false; + return true; } -std::ostream & writeCSV(std::ostream & out, const Mat & mat) +const Formatter* Formatter::get(const char* fmt) { - std::streamsize pp = out.precision(); - out.precision(15); - for (int i = 0; i < mat.rows; i++) - { + if(!fmt || my_streq(fmt, "")) + return g_defaultFormatter; + if( my_streq(fmt, "MATLAB")) + return &matlabFormatter; + if( my_streq(fmt, "CSV")) + return &csvFormatter; + if( my_streq(fmt, "PYTHON")) + return &pythonFormatter; + if( my_streq(fmt, "NUMPY")) + return &numpyFormatter; + if( my_streq(fmt, "C")) + return &cFormatter; + CV_Error(CV_StsBadArg, "Unknown formatter"); + return g_defaultFormatter; +} - for (int j = 0; j < mat.cols; j++) +const Formatter* Formatter::setDefault(const Formatter* fmt) +{ + const Formatter* prevFmt = g_defaultFormatter; + if(!fmt) + fmt = g_defaultFormatter0; + g_defaultFormatter = fmt; + return prevFmt; +} + +Formatted::Formatted(const Mat& _m, const Formatter* _fmt, + const vector& _params) +{ + mtx = _m; + fmt = _fmt ? _fmt : Formatter::get(); + std::copy(_params.begin(), _params.end(), back_inserter(params)); +} + +Formatted::Formatted(const Mat& _m, const Formatter* _fmt, const int* _params) +{ + mtx = _m; + fmt = _fmt ? _fmt : Formatter::get(); + + if( _params ) { - writeelem(out,mat,i,j); - if (j < mat.cols - 1) - out << ","; + int i, maxParams = 100; + for(i = 0; i < maxParams && _params[i] != 0; i+=2) + ; + std::copy(_params, _params + i, back_inserter(params)); } - out << "\n"; - } - out.precision(pp); - return out; } } diff --git a/modules/features2d/src/brief.cpp b/modules/features2d/src/brief.cpp index 2a9b53440b..8900780d78 100644 --- a/modules/features2d/src/brief.cpp +++ b/modules/features2d/src/brief.cpp @@ -1,11 +1,49 @@ -#include -#include -#include +/*M/////////////////////////////////////////////////////////////////////////////////////// +// +// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. +// +// By downloading, copying, installing or using the software you agree to this license. +// If you do not agree to this license, do not download, install, +// copy or use the software. +// +// +// License Agreement +// For Open Source Computer Vision Library +// +// Copyright (C) 2000-2008, Intel Corporation, all rights reserved. +// Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved. +// Third party copyrights are property of their respective owners. +// +// Redistribution and use in source and binary forms, with or without modification, +// are permitted provided that the following conditions are met: +// +// * Redistribution's of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// +// * Redistribution's in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// +// * The name of the copyright holders may not be used to endorse or promote products +// derived from this software without specific prior written permission. +// +// This software is provided by the copyright holders and contributors "as is" and +// any express or implied warranties, including, but not limited to, the implied +// warranties of merchantability and fitness for a particular purpose are disclaimed. +// In no event shall the Intel Corporation or contributors be liable for any direct, +// indirect, incidental, special, exemplary, or consequential damages +// (including, but not limited to, procurement of substitute goods or services; +// loss of use, data, or profits; or business interruption) however caused +// and on any theory of liability, whether in contract, strict liability, +// or tort (including negligence or otherwise) arising in any way out of +// the use of this software, even if advised of the possibility of such damage. +// +//M*/ +#include "precomp.hpp" +#include #include -using std::vector; - namespace cv { @@ -362,8 +400,4 @@ unsigned char HammingLUT::byteBitsLookUp(unsigned char b){ return table[b]; } - - - - } // namespace cv diff --git a/samples/cpp/cvout_sample.cpp b/samples/cpp/cvout_sample.cpp index 86e703459b..910c9c6cbb 100644 --- a/samples/cpp/cvout_sample.cpp +++ b/samples/cpp/cvout_sample.cpp @@ -1,34 +1,41 @@ #include "opencv2/core/core.hpp" +#include using namespace std; using namespace cv; -int main() +int main(int,char**) { - Mat i = Mat::eye(4, 4, CV_32F); - cout << "i = " << i << ";" << endl; - - Mat r = Mat(10, 10, CV_8UC1); - randu(r, Scalar(0), Scalar(255)); - - cout << "r = " << r << ";" << endl; - - Point2f p(5, 1); - cout << "p = " << p << ";" << endl; - - Point3f p3f(2, 6, 7); - cout << "p3f = " << p3f << ";" << endl; - - vector points(20); - for (size_t i = 0; i < points.size(); ++i) - { - points[i] = Point2f(i * 5, i % 7); - } - cout << "points = " << points << ";" << endl; - - cout << "#csv" << endl; - - writeCSV(cout, r); - - return 1; + Mat i = Mat::eye(4, 4, CV_64F); + i.at(1,1) = CV_PI; + cout << "i = " << i << ";" << 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; + + Point2f p(5, 1); + cout << "p = " << p << ";" << endl; + + Point3f p3f(2, 6, 7); + cout << "p3f = " << p3f << ";" << endl; + + vector v; + v.push_back(1); + v.push_back(2); + v.push_back(3); + + cout << "shortvec = " << Mat(v) << endl; + + vector points(20); + for (size_t i = 0; i < points.size(); ++i) + points[i] = Point2f(i * 5, i % 7); + + cout << "points = " << points << ";" << endl; + return 0; }