|
|
@ -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 <iterator> |
|
|
|
|
|
|
|
|
|
|
|
namespace cv |
|
|
|
namespace cv |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
namespace |
|
|
|
static inline char getCloseBrace(char c) |
|
|
|
{ |
|
|
|
|
|
|
|
template<typename T> |
|
|
|
|
|
|
|
std::ostream & writevec(std::ostream & out, const std::vector<T> & points) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
typedef T MT_T; |
|
|
|
return c == '[' ? ']' : c == '(' ? ')' : c == '{' ? '}' : '\0'; |
|
|
|
typedef typename std::vector<T>::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) |
|
|
|
template<typename _Tp> static void writeElems(std::ostream& out, const _Tp* data, |
|
|
|
|
|
|
|
int nelems, int cn, char obrace, char cbrace) |
|
|
|
{ |
|
|
|
{ |
|
|
|
|
|
|
|
typedef typename DataType<_Tp>::work_type _WTp; |
|
|
|
out << *it; |
|
|
|
nelems *= cn; |
|
|
|
CIT next = it; |
|
|
|
for(int i = 0; i < nelems; i += cn) |
|
|
|
if (++next != points.end()) |
|
|
|
{ |
|
|
|
|
|
|
|
if(cn == 1) |
|
|
|
{ |
|
|
|
{ |
|
|
|
out << " "; |
|
|
|
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 ? ", " : ""); |
|
|
|
} |
|
|
|
} |
|
|
|
out << "]"; |
|
|
|
|
|
|
|
out.precision(pp); |
|
|
|
|
|
|
|
return out; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::ostream & writeelem(std::ostream & out, const Mat & mat, int i, int j) |
|
|
|
|
|
|
|
|
|
|
|
static void writeElems(std::ostream& out, const void* data, int nelems, int type, char brace) |
|
|
|
{ |
|
|
|
{ |
|
|
|
if (mat.type() == CV_32F) |
|
|
|
int depth = CV_MAT_DEPTH(type), cn = CV_MAT_CN(type); |
|
|
|
out << mat.at<float> (i, j); |
|
|
|
char cbrace = ' '; |
|
|
|
else if (mat.type() == CV_64F) |
|
|
|
if(!brace || isspace(brace)) |
|
|
|
out << mat.at<double> (i, j); |
|
|
|
{ |
|
|
|
else if (mat.type() == CV_32S) |
|
|
|
nelems *= cn; |
|
|
|
out << mat.at<int> (i, j); |
|
|
|
cn = 1; |
|
|
|
else if (mat.type() == CV_8U) |
|
|
|
} |
|
|
|
out << int(mat.at<unsigned char> (i, j)); |
|
|
|
else |
|
|
|
else if (mat.type() == CV_16U) |
|
|
|
cbrace = getCloseBrace(brace); |
|
|
|
out << int(mat.at<unsigned short> (i, j)); |
|
|
|
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 |
|
|
|
else |
|
|
|
out << "?"; |
|
|
|
CV_Error(CV_StsUnsupportedFormat, ""); |
|
|
|
return out; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void writeMat(std::ostream& out, const Mat& m, char rowsep, char elembrace, bool singleLine) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
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 & operator<<(std::ostream & out, const std::vector<Point2f> & points) |
|
|
|
class MatlabFormatter : public Formatter |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
public: |
|
|
|
|
|
|
|
virtual ~MatlabFormatter() {} |
|
|
|
|
|
|
|
void write(std::ostream& out, const Mat& m, const int*, int) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
return writevec(out, points); |
|
|
|
out << "["; |
|
|
|
|
|
|
|
writeMat(out, m, ';', ' ', m.cols == 1); |
|
|
|
|
|
|
|
out << "]"; |
|
|
|
} |
|
|
|
} |
|
|
|
std::ostream & operator<<(std::ostream & out, const std::vector<Point3f> & points) |
|
|
|
|
|
|
|
|
|
|
|
void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
return writevec(out, points); |
|
|
|
writeElems(out, data, nelems, type, ' '); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
std::ostream & operator<<(std::ostream & out, const Mat & _mat) |
|
|
|
class PythonFormatter : public Formatter |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::streamsize pp = out.precision(); |
|
|
|
public: |
|
|
|
out.precision(15); |
|
|
|
virtual ~PythonFormatter() {} |
|
|
|
std::vector<Mat> channels; |
|
|
|
void write(std::ostream& out, const Mat& m, const int*, int) const |
|
|
|
split(_mat, channels); |
|
|
|
|
|
|
|
for (int chn = 0; chn < _mat.channels(); chn++) |
|
|
|
|
|
|
|
{ |
|
|
|
{ |
|
|
|
Mat mat = channels[chn]; |
|
|
|
|
|
|
|
out << "["; |
|
|
|
out << "["; |
|
|
|
for (int i = 0; i < mat.rows; i++) |
|
|
|
writeMat(out, m, m.cols > 1 ? '[' : ' ', '[', m.cols*m.channels() == 1); |
|
|
|
{ |
|
|
|
out << "]"; |
|
|
|
for (int j = 0; j < mat.cols; j++) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
writeelem(out,mat,i,j); |
|
|
|
writeElems(out, data, nelems, type, '['); |
|
|
|
if (j < mat.cols - 1) |
|
|
|
|
|
|
|
out << " "; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
if (i < mat.rows - 1) |
|
|
|
}; |
|
|
|
out << ";\n"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NumpyFormatter : public Formatter |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
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()] << "')"; |
|
|
|
} |
|
|
|
} |
|
|
|
out << "]"; |
|
|
|
|
|
|
|
|
|
|
|
void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
writeElems(out, data, nelems, type, '['); |
|
|
|
} |
|
|
|
} |
|
|
|
out.precision(pp); |
|
|
|
}; |
|
|
|
return out; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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"; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::ostream & writeCSV(std::ostream & out, const std::vector<Point3f> & points) |
|
|
|
void write(std::ostream& out, const void* data, int nelems, int type, const int*, int) const |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::streamsize pp = out.precision(); |
|
|
|
writeElems(out, data, nelems, type, ' '); |
|
|
|
out.precision(15); |
|
|
|
} |
|
|
|
std::vector<Point3f>::const_iterator it = points.begin(); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for (; it != points.end(); ++it) |
|
|
|
class CFormatter : public Formatter |
|
|
|
{ |
|
|
|
{ |
|
|
|
out << it->x << "," << it->y << ","<< it->z << "\n"; |
|
|
|
public: |
|
|
|
|
|
|
|
virtual ~CFormatter() {} |
|
|
|
|
|
|
|
void write(std::ostream& out, const Mat& m, const int*, int) const |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
out << "{"; |
|
|
|
|
|
|
|
writeMat(out, m, ',', ' ', m.cols==1); |
|
|
|
|
|
|
|
out << "}"; |
|
|
|
} |
|
|
|
} |
|
|
|
out.precision(pp); |
|
|
|
|
|
|
|
return 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<Point2f> & points) |
|
|
|
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) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::streamsize pp = out.precision(); |
|
|
|
size_t i, alen = strlen(a), blen = strlen(b); |
|
|
|
out.precision(15); |
|
|
|
if( alen != blen ) |
|
|
|
std::vector<Point2f>::const_iterator it = points.begin(); |
|
|
|
return false; |
|
|
|
|
|
|
|
for( i = 0; i < alen; i++ ) |
|
|
|
|
|
|
|
if( a[i] != b[i] && a[i] - 32 != b[i] ) |
|
|
|
|
|
|
|
return false; |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (; it != points.end(); ++it) |
|
|
|
const Formatter* Formatter::get(const char* fmt) |
|
|
|
{ |
|
|
|
{ |
|
|
|
out << it->x << "," << it->y << "\n"; |
|
|
|
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; |
|
|
|
} |
|
|
|
} |
|
|
|
out.precision(pp); |
|
|
|
|
|
|
|
return out; |
|
|
|
const Formatter* Formatter::setDefault(const Formatter* fmt) |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
const Formatter* prevFmt = g_defaultFormatter; |
|
|
|
|
|
|
|
if(!fmt) |
|
|
|
|
|
|
|
fmt = g_defaultFormatter0; |
|
|
|
|
|
|
|
g_defaultFormatter = fmt; |
|
|
|
|
|
|
|
return prevFmt; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::ostream & writeCSV(std::ostream & out, const Mat & mat) |
|
|
|
Formatted::Formatted(const Mat& _m, const Formatter* _fmt, |
|
|
|
|
|
|
|
const vector<int>& _params) |
|
|
|
{ |
|
|
|
{ |
|
|
|
std::streamsize pp = out.precision(); |
|
|
|
mtx = _m; |
|
|
|
out.precision(15); |
|
|
|
fmt = _fmt ? _fmt : Formatter::get(); |
|
|
|
for (int i = 0; i < mat.rows; i++) |
|
|
|
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(); |
|
|
|
|
|
|
|
|
|
|
|
for (int j = 0; j < mat.cols; j++) |
|
|
|
if( _params ) |
|
|
|
{ |
|
|
|
{ |
|
|
|
writeelem(out,mat,i,j); |
|
|
|
int i, maxParams = 100; |
|
|
|
if (j < mat.cols - 1) |
|
|
|
for(i = 0; i < maxParams && _params[i] != 0; i+=2) |
|
|
|
out << ","; |
|
|
|
; |
|
|
|
} |
|
|
|
std::copy(_params, _params + i, back_inserter(params)); |
|
|
|
out << "\n"; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
out.precision(pp); |
|
|
|
|
|
|
|
return out; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|