Stream operators (Feature #2430)
Description
Stream operators (<<) are defined for objects of type Mat, Point and Point3, but are missing for many other core types such as Rect and Size. It would be great to have operators defined for such types.
Thanks!
H
Associated revisions
Add stream operators (Requested #2430)
Add output stream operators (<<) for Rect, Size, Matx, Vec.
I can't add operations for cv::KeyPoint.
And putting together all operator<<.
(Matx, Point_, Point3_, Vec, Size_, Rect_)
History
Updated by Stefan R over 12 years ago
I agree. I have done this already in my own code, but you might find the following snippets useful.
Obviously it has to be decided whether operator <<
is meant for seralizing an object into a file or for output on stdout.
IMHO operator <<
is for human-readable output while for serializing object should implement a special write(std::ostream& os)
method or alike.
Note: If operator <
is implemented it should compare the fields in the following order: y before x (for Points) or (size before y before x) for KeyPoints.
This resembles an intuitive ordering of points in the image plane.
cv::KeyPoint¶
// ============================================================================= // Utils for cv::KeyPoint // ============================================================================= namespace cv { inline std::ostream& operator << (std::ostream& out, const cv::KeyPoint& kp){ out << "(x: " << kp.pt.x << " y: " << kp.pt.y << " size: " << kp.size << " angle: " << kp.angle << " class_id: " << kp.class_id << ')'; return out; } inline bool operator < (const cv::KeyPoint& a, const cv::KeyPoint& b) { if( a.size != b.size ) return (a.size < b.size); if( a.pt.y != b.pt.y ) return (a.pt.y < b.pt.y); if( a.pt.x != b.pt.x ) return (a.pt.x < b.pt.x); if( a.angle != b.angle ) return (a.angle < b.angle); if( a.class_id != b.class_id ) return (a.class_id < b.class_id); return false; } inline bool operator == (const cv::KeyPoint& a, const cv::KeyPoint& b) { return( a.pt.y == b.pt.y && a.pt.x == b.pt.x && a.size == b.size && a.angle == b.angle && a.class_id == b.class_id ); } inline bool operator != (const cv::KeyPoint& a, const cv::KeyPoint& b) { return !(a == b); } } //end namespace cv
cv::Point2f¶
// ============================================================================= // Utils for cv::Point2f // ============================================================================= namespace cv { inline bool operator < (const cv::Point2f& a, const cv::Point2f& b) { if( a.y != b.y ) return (a.y < b.y); if( a.x != b.x ) return (a.x < b.x); return false; } inline bool operator == (const cv::Point2f& a, const cv::Point2f& b) { return ( a.y == b.y && a.x == b.x ); } inline bool operator != (const cv::Point2f& a, const cv::Point2f& b) { return !( a.y == b.y && a.x == b.x ); } } //end namespace cv #if _MSC_VER < 1600 namespace std { namespace tr1 { #else namespace std { #endif /// Hash functor template<> struct hash<cv::Point2f> : std::unary_function<cv::Point2f, size_t> { inline size_t operator()(const cv::Point2f& p) const { union { unsigned int u; float f; } uu; size_t _Val = 2166136261U; uu.f = p.x; _Val = 16777619U * _Val ^ uu.u; uu.f = p.y; _Val = 16777619U * _Val ^ uu.u; return _Val; } }; #if _MSC_VER < 1600 }} // end namespace std::tr1 #else } // end namespace std #endif
cv::Point¶
// ============================================================================= // Utils for cv::Point // ============================================================================= namespace cv { inline bool operator < (const cv::Point& a, const cv::Point& b) { if( a.y != b.y ) return (a.y < b.y); return (a.x < b.x); } inline bool operator == (const cv::Point& a, const cv::Point& b) { return ( a.y == b.y && a.x == b.x ); } inline bool operator != (const cv::Point& a, const cv::Point& b) { return !( a.y == b.y && a.x == b.x ); } } //end namespace cv #if _MSC_VER < 1600 namespace std { namespace tr1 { #else namespace std { #endif /// Hash functor template<> struct hash<cv::Point> : std::unary_function<cv::Point, size_t> { inline size_t operator()(const cv::Point& p) const { size_t _Val = 2166136261U; _Val = 16777619U * _Val ^ (unsigned int)(p.x); _Val = 16777619U * _Val ^ (unsigned int)(p.y); return _Val; } }; #if _MSC_VER < 1600 }} // end namespace std::tr1 #else } // end namespace std #endif
cv::Rect¶
// ============================================================================= // Utils for cv::Rect // ============================================================================= namespace cv { std::ostream& operator << (std::ostream& out, const cv::Rect& rect) { out << "cv::Rect(x: "<< rect.x << " y: " << rect.y << " width: " << rect.width << " height: " << rect.height << ')'; return out; } } //end namespace cv
Updated by Andrey Kamaev over 12 years ago
- Status changed from Open to Done