From a64d8be233f1bb05cb2e686076252ab07410fc19 Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Wed, 27 Apr 2016 16:59:26 +0200 Subject: [PATCH] FileStorage: add simplified API for bindings at least it is possible to read/ write calibration files. Updates #4282. Also add CPP method for writing comments. --- .../core/include/opencv2/core/persistence.hpp | 34 ++++++++++++++++++- modules/core/src/persistence.cpp | 34 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) diff --git a/modules/core/include/opencv2/core/persistence.hpp b/modules/core/include/opencv2/core/persistence.hpp index 9cf3bca1c72c..875d82cbe562 100644 --- a/modules/core/include/opencv2/core/persistence.hpp +++ b/modules/core/include/opencv2/core/persistence.hpp @@ -398,7 +398,7 @@ class CV_EXPORTS_W FileStorage FileNode operator[](const String& nodename) const; /** @overload */ - CV_WRAP FileNode operator[](const char* nodename) const; + CV_WRAP_AS(getFileNode) FileNode operator[](const char* nodename) const; /** @brief Returns the obsolete C FileStorage structure. @returns Pointer to the underlying C FileStorage structure @@ -425,6 +425,38 @@ class CV_EXPORTS_W FileStorage */ void writeObj( const String& name, const void* obj ); + /** + * @brief Simplified writing API to use with bindings. + * @param name Name of the written object + * @param val Value of the written object + */ + CV_WRAP void write(const String& name, double val); + /// @overload + CV_WRAP void write(const String& name, const String& val); + /// @overload + CV_WRAP void write(const String& name, InputArray val); + + /** + * @brief Simplified reading API to use with bindings. + * @param name Name of the object + * @param val Value of the object + */ + CV_WRAP_AS(readReal) void read(const String& name, CV_OUT double& val); + /// @overload + CV_WRAP_AS(readString) void read(const String& name, CV_OUT String& val); + /// @overload + CV_WRAP_AS(readMat) void read(const String& name, CV_OUT Mat& val); + + /** @brief Writes a comment. + + The function writes a comment into file storage. The comments are skipped when the storage is read. + @param comment The written comment, single-line or multi-line + @param append If true, the function tries to put the comment at the end of current line. + Else if the comment is multi-line, or if it does not fit at the end of the current + line, the comment starts a new line. + */ + CV_WRAP void writeComment(const String& comment, bool append = false); + /** @brief Returns the normalized object name for the specified name of a file. @param filename Name of a file @returns The normalized object name. diff --git a/modules/core/src/persistence.cpp b/modules/core/src/persistence.cpp index 1b1e956f206c..5c0a94e66450 100644 --- a/modules/core/src/persistence.cpp +++ b/modules/core/src/persistence.cpp @@ -5271,6 +5271,40 @@ void FileStorage::writeObj( const String& name, const void* obj ) cvWrite( fs, name.size() > 0 ? name.c_str() : 0, obj ); } +void FileStorage::write( const String& name, double val ) +{ + *this << name << val; +} + +void FileStorage::write( const String& name, const String& val ) +{ + *this << name << val; +} + +void FileStorage::write( const String& name, InputArray val ) +{ + *this << name << val.getMat(); +} + +void FileStorage::read( const String& name, double& val ) +{ + (*this)[name] >> val; +} + +void FileStorage::read( const String& name, String& val ) +{ + (*this)[name] >> val; +} + +void FileStorage::read( const String& name, Mat& val ) +{ + (*this)[name] >> val; +} + +void FileStorage::writeComment( const String& comment, bool append ) +{ + cvWriteComment(fs, comment.c_str(), append ? 1 : 0); +} FileNode FileStorage::operator[](const String& nodename) const {