Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
c979de1e
Commit
c979de1e
authored
Apr 02, 2013
by
Andrey Kamaev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Rewrite Mat formatting without std streams
parent
7193a73c
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
345 additions
and
276 deletions
+345
-276
operations.hpp
modules/core/include/opencv2/core/operations.hpp
+44
-60
traits.hpp
modules/core/include/opencv2/core/traits.hpp
+45
-0
out.cpp
modules/core/src/out.cpp
+243
-204
test_io.cpp
modules/core/test/test_io.cpp
+1
-0
window_gtk.cpp
modules/highgui/src/window_gtk.cpp
+2
-2
cout_mat.cpp
samples/cpp/cout_mat.cpp
+6
-6
mat_the_basic_image_container.cpp
...e_basic_image_container/mat_the_basic_image_container.cpp
+4
-4
No files found.
modules/core/include/opencv2/core/operations.hpp
View file @
c979de1e
...
@@ -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
Formatte
r
class
CV_EXPORTS
Formatte
d
{
{
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
Formatte
d
(
Mat
(
vec
),
Formatter
::
get
(
fmt
),
params
);
return
Formatte
r
::
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
...
...
modules/core/include/opencv2/core/traits.hpp
View file @
c979de1e
...
@@ -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__
modules/core/src/out.cpp
View file @
c979de1e
...
@@ -11,7 +11,8 @@
...
@@ -11,7 +11,8 @@
// For Open Source Computer Vision Library
// For Open Source Computer Vision Library
//
//
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
// Copyright (C) 2009-2010, Willow Garage Inc., all rights reserved.
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Third party copyrights are property of their respective owners.
// Third party copyrights are property of their respective owners.
//
//
// Redistribution and use in source and binary forms, with or without modification,
// Redistribution and use in source and binary forms, with or without modification,
...
@@ -41,267 +42,305 @@
...
@@ -41,267 +42,305 @@
//M*/
//M*/
#include "precomp.hpp"
#include "precomp.hpp"
#include <iterator>
namespace
cv
namespace
{
{
class
FormattedImpl
:
public
cv
::
Formatted
{
enum
{
STATE_PROLOGUE
,
STATE_EPILOGUE
,
STATE_ROW_OPEN
,
STATE_ROW_CLOSE
,
STATE_CN_OPEN
,
STATE_CN_CLOSE
,
STATE_VALUE
,
STATE_FINISHED
,
STATE_LINE_SEPARATOR
,
STATE_CN_SEPARATOR
,
STATE_VALUE_SEPARATOR
};
enum
{
BRACE_ROW_OPEN
=
0
,
BRACE_ROW_CLOSE
=
1
,
BRACE_ROW_SEP
=
2
,
BRACE_CN_OPEN
=
3
,
BRACE_CN_CLOSE
=
4
};
static
inline
char
getCloseBrace
(
char
c
)
char
floatFormat
[
8
];
{
char
buf
[
32
];
// enough for double with precision up to 20
return
c
==
'['
?
']'
:
c
==
'('
?
')'
:
c
==
'{'
?
'}'
:
'\0'
;
}
cv
::
Mat
mtx
;
int
mcn
;
bool
singleLine
;
template
<
typename
_Tp
>
static
void
writeElems
(
std
::
ostream
&
out
,
const
_Tp
*
data
,
int
state
;
int
nelems
,
int
cn
,
char
obrace
,
char
cbrace
)
int
row
;
{
int
col
;
typedef
typename
DataType
<
_Tp
>::
work_type
_WTp
;
int
cn
;
nelems
*=
cn
;
for
(
int
i
=
0
;
i
<
nelems
;
i
+=
cn
)
cv
::
String
prologue
;
cv
::
String
epilogue
;
char
braces
[
5
];
void
(
FormattedImpl
::*
valueToStr
)();
void
valueToStr8u
()
{
sprintf
(
buf
,
"%3d"
,
(
int
)
mtx
.
ptr
<
uchar
>
(
row
,
col
)[
cn
]);
}
void
valueToStr8s
()
{
sprintf
(
buf
,
"%3d"
,
(
int
)
mtx
.
ptr
<
schar
>
(
row
,
col
)[
cn
]);
}
void
valueToStr16u
()
{
sprintf
(
buf
,
"%d"
,
(
int
)
mtx
.
ptr
<
ushort
>
(
row
,
col
)[
cn
]);
}
void
valueToStr16s
()
{
sprintf
(
buf
,
"%d"
,
(
int
)
mtx
.
ptr
<
short
>
(
row
,
col
)[
cn
]);
}
void
valueToStr32s
()
{
sprintf
(
buf
,
"%d"
,
mtx
.
ptr
<
int
>
(
row
,
col
)[
cn
]);
}
void
valueToStr32f
()
{
sprintf
(
buf
,
floatFormat
,
mtx
.
ptr
<
float
>
(
row
,
col
)[
cn
]);
}
void
valueToStr64f
()
{
sprintf
(
buf
,
floatFormat
,
mtx
.
ptr
<
double
>
(
row
,
col
)[
cn
]);
}
void
valueToStrOther
()
{
buf
[
0
]
=
0
;
}
public
:
FormattedImpl
(
cv
::
String
pl
,
cv
::
String
el
,
cv
::
Mat
m
,
char
br
[
5
],
bool
sLine
,
int
precision
)
{
{
if
(
cn
==
1
)
prologue
=
pl
;
epilogue
=
el
;
mtx
=
m
;
mcn
=
m
.
channels
();
memcpy
(
braces
,
br
,
5
);
state
=
STATE_PROLOGUE
;
singleLine
=
sLine
;
if
(
precision
<
0
)
{
{
out
<<
(
_WTp
)
data
[
i
]
<<
(
i
+
1
<
nelems
?
", "
:
""
);
floatFormat
[
0
]
=
'%'
;
continue
;
floatFormat
[
1
]
=
'a'
;
floatFormat
[
2
]
=
0
;
}
}
out
<<
obrace
;
else
for
(
int
j
=
0
;
j
<
cn
;
j
++
)
{
out
<<
(
_WTp
)
data
[
i
+
j
]
<<
(
j
+
1
<
cn
?
", "
:
""
);
sprintf
(
floatFormat
,
"%%.%dg"
,
std
::
min
(
precision
,
20
));
out
<<
cbrace
<<
(
i
+
cn
<
nelems
?
", "
:
""
);
}
}
}
switch
(
mtx
.
depth
())
{
case
CV_8U
:
valueToStr
=
&
FormattedImpl
::
valueToStr8u
;
break
;
case
CV_8S
:
valueToStr
=
&
FormattedImpl
::
valueToStr8s
;
break
;
case
CV_16U
:
valueToStr
=
&
FormattedImpl
::
valueToStr16u
;
break
;
case
CV_16S
:
valueToStr
=
&
FormattedImpl
::
valueToStr16s
;
break
;
case
CV_32S
:
valueToStr
=
&
FormattedImpl
::
valueToStr32s
;
break
;
case
CV_32F
:
valueToStr
=
&
FormattedImpl
::
valueToStr32f
;
break
;
case
CV_64F
:
valueToStr
=
&
FormattedImpl
::
valueToStr64f
;
break
;
default
:
valueToStr
=
&
FormattedImpl
::
valueToStrOther
;
break
;
}
}
static
void
writeElems
(
std
::
ostream
&
out
,
const
void
*
data
,
int
nelems
,
int
type
,
char
brace
)
void
reset
()
{
int
depth
=
CV_MAT_DEPTH
(
type
),
cn
=
CV_MAT_CN
(
type
);
char
cbrace
=
' '
;
if
(
!
brace
||
isspace
(
brace
))
{
{
nelems
*=
cn
;
state
=
STATE_PROLOGUE
;
cn
=
1
;
}
}
const
char
*
next
()
{
switch
(
state
)
{
case
STATE_PROLOGUE
:
row
=
0
;
if
(
mtx
.
empty
())
state
=
STATE_EPILOGUE
;
else
else
cbrace
=
getCloseBrace
(
brace
);
state
=
STATE_ROW_OPEN
;
if
(
depth
==
CV_8U
)
return
prologue
.
c_str
();
writeElems
(
out
,
(
const
uchar
*
)
data
,
nelems
,
cn
,
brace
,
cbrace
);
case
STATE_EPILOGUE
:
else
if
(
depth
==
CV_8S
)
state
=
STATE_FINISHED
;
writeElems
(
out
,
(
const
schar
*
)
data
,
nelems
,
cn
,
brace
,
cbrace
);
return
epilogue
.
c_str
();
else
if
(
depth
==
CV_16U
)
case
STATE_ROW_OPEN
:
writeElems
(
out
,
(
const
ushort
*
)
data
,
nelems
,
cn
,
brace
,
cbrace
);
col
=
0
;
else
if
(
depth
==
CV_16S
)
state
=
STATE_CN_OPEN
;
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
();
size_t
pos
=
0
;
out
.
precision
(
8
);
if
(
row
>
0
)
writeElems
(
out
,
(
const
float
*
)
data
,
nelems
,
cn
,
brace
,
cbrace
);
while
(
pos
<
prologue
.
size
()
&&
pos
<
sizeof
(
buf
)
-
2
)
out
.
precision
(
pp
);
buf
[
pos
++
]
=
' '
;
if
(
braces
[
BRACE_ROW_OPEN
])
buf
[
pos
++
]
=
braces
[
BRACE_ROW_OPEN
];
if
(
!
pos
)
return
next
();
buf
[
pos
]
=
0
;
}
}
else
if
(
depth
==
CV_64F
)
return
buf
;
case
STATE_ROW_CLOSE
:
state
=
STATE_LINE_SEPARATOR
;
++
row
;
if
(
braces
[
BRACE_ROW_CLOSE
])
{
{
std
::
streamsize
pp
=
out
.
precision
()
;
buf
[
0
]
=
braces
[
BRACE_ROW_CLOSE
]
;
out
.
precision
(
16
)
;
buf
[
1
]
=
row
<
mtx
.
rows
?
','
:
'\0'
;
writeElems
(
out
,
(
const
double
*
)
data
,
nelems
,
cn
,
brace
,
cbrace
)
;
buf
[
2
]
=
0
;
out
.
precision
(
pp
)
;
return
buf
;
}
}
else
if
(
braces
[
BRACE_ROW_SEP
]
&&
row
<
mtx
.
rows
)
{
buf
[
0
]
=
braces
[
BRACE_ROW_SEP
];
buf
[
1
]
=
0
;
return
buf
;
}
return
next
();
case
STATE_CN_OPEN
:
cn
=
0
;
state
=
STATE_VALUE
;
if
(
mcn
>
1
&&
braces
[
BRACE_CN_OPEN
])
{
buf
[
0
]
=
braces
[
BRACE_CN_OPEN
];
buf
[
1
]
=
0
;
return
buf
;
}
return
next
();
case
STATE_CN_CLOSE
:
++
col
;
if
(
col
>=
mtx
.
cols
)
state
=
STATE_ROW_CLOSE
;
else
else
CV_Error
(
CV_StsUnsupportedFormat
,
""
);
state
=
STATE_CN_SEPARATOR
;
}
if
(
mcn
>
1
&&
braces
[
BRACE_CN_CLOSE
])
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
)
buf
[
0
]
=
braces
[
BRACE_CN_CLOSE
];
out
<<
orowbrace
;
buf
[
1
]
=
0
;
if
(
m
.
data
)
return
buf
;
writeElems
(
out
,
m
.
ptr
(
i
),
m
.
cols
,
type
,
elembrace
);
}
if
(
orowbrace
)
return
next
();
out
<<
crowbrace
<<
(
i
+
1
<
m
.
rows
?
", "
:
""
);
case
STATE_VALUE
:
if
(
i
+
1
<
m
.
rows
)
(
this
->*
valueToStr
)();
if
(
++
cn
>=
mcn
)
state
=
STATE_CN_CLOSE
;
else
state
=
STATE_VALUE_SEPARATOR
;
return
buf
;
case
STATE_FINISHED
:
return
0
;
case
STATE_LINE_SEPARATOR
:
if
(
row
>=
mtx
.
rows
)
{
{
if
(
rowsep
)
state
=
STATE_EPILOGUE
;
out
<<
rowsep
<<
(
singleLine
?
" "
:
""
);
return
next
();
if
(
!
singleLine
)
}
out
<<
"
\n
"
;
state
=
STATE_ROW_OPEN
;
buf
[
0
]
=
singleLine
?
' '
:
'\n'
;
buf
[
1
]
=
0
;
return
buf
;
case
STATE_CN_SEPARATOR
:
state
=
STATE_CN_OPEN
;
buf
[
0
]
=
','
;
buf
[
1
]
=
' '
;
buf
[
2
]
=
0
;
return
buf
;
case
STATE_VALUE_SEPARATOR
:
state
=
STATE_VALUE
;
buf
[
0
]
=
','
;
buf
[
1
]
=
' '
;
buf
[
2
]
=
0
;
return
buf
;
}
}
return
0
;
}
}
}
};
class
MatlabFormatter
:
public
Formatter
class
FormatterBase
:
public
cv
::
Formatter
{
public
:
virtual
~
MatlabFormatter
()
{}
void
write
(
std
::
ostream
&
out
,
const
Mat
&
m
,
const
int
*
,
int
)
const
{
{
out
<<
"["
;
public
:
writeMat
(
out
,
m
,
';'
,
' '
,
m
.
cols
==
1
);
FormatterBase
()
:
prec32f
(
8
),
prec64f
(
16
),
multiline
(
true
)
{}
out
<<
"]"
;
}
void
write
(
std
::
ostream
&
out
,
const
void
*
data
,
int
nelems
,
int
type
,
const
int
*
,
int
)
const
void
set32fPrecision
(
int
p
)
{
{
writeElems
(
out
,
data
,
nelems
,
type
,
' '
)
;
prec32f
=
p
;
}
}
};
class
PythonFormatter
:
public
Formatter
void
set64fPrecision
(
int
p
)
{
public
:
virtual
~
PythonFormatter
()
{}
void
write
(
std
::
ostream
&
out
,
const
Mat
&
m
,
const
int
*
,
int
)
const
{
{
out
<<
"["
;
prec64f
=
p
;
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
void
setMultiline
(
bool
ml
)
{
{
writeElems
(
out
,
data
,
nelems
,
type
,
'['
)
;
multiline
=
ml
;
}
}
};
protected
:
int
prec32f
;
int
prec64f
;
int
multiline
;
};
class
NumpyFormatter
:
public
Formatter
class
MatlabFormatter
:
public
FormatterBase
{
public
:
virtual
~
NumpyFormatter
()
{}
void
write
(
std
::
ostream
&
out
,
const
Mat
&
m
,
const
int
*
,
int
)
const
{
{
static
const
char
*
numpyTypes
[]
=
public
:
{
"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
cv
::
Ptr
<
cv
::
Formatted
>
format
(
const
cv
::
Mat
&
mtx
)
const
{
{
writeElems
(
out
,
data
,
nelems
,
type
,
'['
);
char
braces
[
5
]
=
{
'\0'
,
'\0'
,
';'
,
'\0'
,
'\0'
};
return
new
FormattedImpl
(
"["
,
"]"
,
mtx
,
braces
,
mtx
.
cols
==
1
||
!
multiline
,
mtx
.
depth
()
==
CV_64F
?
prec64f
:
prec32f
);
}
}
};
};
class
CSVFormatter
:
public
Formatter
class
PythonFormatter
:
public
FormatterBase
{
public
:
virtual
~
CSVFormatter
()
{}
void
write
(
std
::
ostream
&
out
,
const
Mat
&
m
,
const
int
*
,
int
)
const
{
{
writeMat
(
out
,
m
,
' '
,
' '
,
m
.
cols
*
m
.
channels
()
==
1
);
public
:
if
(
m
.
rows
>
1
)
out
<<
"
\n
"
;
}
void
write
(
std
::
ostream
&
out
,
const
void
*
data
,
int
nelems
,
int
type
,
const
int
*
,
int
)
const
cv
::
Ptr
<
cv
::
Formatted
>
format
(
const
cv
::
Mat
&
mtx
)
const
{
{
writeElems
(
out
,
data
,
nelems
,
type
,
' '
);
char
braces
[
5
]
=
{
'['
,
']'
,
'\0'
,
'['
,
']'
};
if
(
mtx
.
cols
==
1
)
braces
[
0
]
=
braces
[
1
]
=
'\0'
;
return
new
FormattedImpl
(
"["
,
"]"
,
mtx
,
braces
,
mtx
.
cols
*
mtx
.
channels
()
==
1
||
!
multiline
,
mtx
.
depth
()
==
CV_64F
?
prec64f
:
prec32f
);
}
}
};
};
class
NumpyFormatter
:
public
FormatterBase
{
public
:
class
CFormatter
:
public
Formatter
cv
::
Ptr
<
cv
::
Formatted
>
format
(
const
cv
::
Mat
&
mtx
)
const
{
public
:
virtual
~
CFormatter
()
{}
void
write
(
std
::
ostream
&
out
,
const
Mat
&
m
,
const
int
*
,
int
)
const
{
{
out
<<
"{"
;
static
const
char
*
numpyTypes
[]
=
writeMat
(
out
,
m
,
','
,
' '
,
m
.
cols
==
1
);
{
out
<<
"}"
;
"uint8"
,
"int8"
,
"uint16"
,
"int16"
,
"int32"
,
"float32"
,
"float64"
,
"uint64"
};
char
braces
[
5
]
=
{
'['
,
']'
,
'\0'
,
'['
,
']'
};
if
(
mtx
.
cols
==
1
)
braces
[
0
]
=
braces
[
1
]
=
'\0'
;
return
new
FormattedImpl
(
"array(["
,
cv
::
format
(
"], type='%s')"
,
numpyTypes
[
mtx
.
depth
()]),
mtx
,
braces
,
mtx
.
cols
*
mtx
.
channels
()
==
1
||
!
multiline
,
mtx
.
depth
()
==
CV_64F
?
prec64f
:
prec32f
);
}
}
};
class
CSVFormatter
:
public
FormatterBase
{
public
:
void
write
(
std
::
ostream
&
out
,
const
void
*
data
,
int
nelems
,
int
type
,
const
int
*
,
int
)
const
cv
::
Ptr
<
cv
::
Formatted
>
format
(
const
cv
::
Mat
&
mtx
)
const
{
{
writeElems
(
out
,
data
,
nelems
,
type
,
' '
);
char
braces
[
5
]
=
{
'\0'
,
'\0'
,
'\0'
,
'\0'
,
'\0'
};
return
new
FormattedImpl
(
cv
::
String
(),
mtx
.
rows
>
1
?
cv
::
String
(
"
\n
"
)
:
cv
::
String
(),
mtx
,
braces
,
mtx
.
cols
*
mtx
.
channels
()
==
1
||
!
multiline
,
mtx
.
depth
()
==
CV_64F
?
prec64f
:
prec32f
);
}
}
};
};
class
CFormatter
:
public
FormatterBase
{
public
:
static
MatlabFormatter
matlabFormatter
;
cv
::
Ptr
<
cv
::
Formatted
>
format
(
const
cv
::
Mat
&
mtx
)
const
static
PythonFormatter
pythonFormatter
;
{
static
NumpyFormatter
numpyFormatter
;
char
braces
[
5
]
=
{
'\0'
,
'\0'
,
','
,
'\0'
,
'\0'
};
static
CSVFormatter
csvFormatter
;
return
new
FormattedImpl
(
"{"
,
"}"
,
mtx
,
braces
,
static
CFormatter
cFormatter
;
mtx
.
cols
==
1
||
!
multiline
,
mtx
.
depth
()
==
CV_64F
?
prec64f
:
prec32f
);
}
};
static
const
Formatter
*
g_defaultFormatter0
=
&
matlabFormatter
;
}
// namespace
static
const
Formatter
*
g_defaultFormatter
=
&
matlabFormatter
;
static
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
;
}
const
Formatter
*
Formatter
::
get
(
const
char
*
fmt
)
{
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
;
}
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
std
::
vector
<
int
>&
_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
)
namespace
cv
{
{
mtx
=
_m
;
Formatted
::~
Formatted
()
{}
fmt
=
_fmt
?
_fmt
:
Formatter
::
get
();
Formatter
::~
Formatter
()
{}
if
(
_params
)
Ptr
<
Formatter
>
Formatter
::
get
(
int
fmt
)
{
{
int
i
,
maxParams
=
100
;
switch
(
fmt
)
for
(
i
=
0
;
i
<
maxParams
&&
_params
[
i
]
!=
0
;
i
+=
2
)
{
;
case
FMT_MATLAB
:
std
::
copy
(
_params
,
_params
+
i
,
back_inserter
(
params
));
return
new
MatlabFormatter
();
case
FMT_CSV
:
return
new
CSVFormatter
();
case
FMT_PYTHON
:
return
new
PythonFormatter
();
case
FMT_NUMPY
:
return
new
NumpyFormatter
();
case
FMT_C
:
return
new
CFormatter
();
}
}
}
return
new
MatlabFormatter
();
}
}
}
// cv
modules/core/test/test_io.cpp
View file @
c979de1e
...
@@ -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
{
{
...
...
modules/highgui/src/window_gtk.cpp
View file @
c979de1e
...
@@ -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
);
...
...
samples/cpp/cout_mat.cpp
View file @
c979de1e
...
@@ -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
;
...
...
samples/cpp/tutorial_code/core/mat_the_basic_image_container/mat_the_basic_image_container.cpp
View file @
c979de1e
...
@@ -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
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment