From 6fbf0758bc9b7108f22642899ffbf9c8c230bf7e Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Thu, 27 Jul 2017 14:34:32 +0200 Subject: [PATCH 1/2] Python: wrap Algorithm::read and Algorithm::write --- modules/core/include/opencv2/core.hpp | 9 +++++++-- modules/core/src/algorithm.cpp | 13 +++++++++++++ modules/features2d/include/opencv2/features2d.hpp | 13 +++++++++++-- 3 files changed, 31 insertions(+), 4 deletions(-) diff --git a/modules/core/include/opencv2/core.hpp b/modules/core/include/opencv2/core.hpp index 8054d31b28..a1d196d677 100644 --- a/modules/core/include/opencv2/core.hpp +++ b/modules/core/include/opencv2/core.hpp @@ -3093,13 +3093,18 @@ public: */ virtual void write(FileStorage& fs) const { (void)fs; } + /** @brief simplified API for language bindings + * @overload + */ + CV_WRAP void write(const Ptr& fs, const String& name = String()) const; + /** @brief Reads algorithm parameters from a file storage */ - virtual void read(const FileNode& fn) { (void)fn; } + CV_WRAP virtual void read(const FileNode& fn) { (void)fn; } /** @brief Returns true if the Algorithm is empty (e.g. in the very beginning or after unsuccessful read */ - virtual bool empty() const { return false; } + CV_WRAP virtual bool empty() const { return false; } /** @brief Reads algorithm from the file node diff --git a/modules/core/src/algorithm.cpp b/modules/core/src/algorithm.cpp index 24f4dfb8b8..556f5a7328 100644 --- a/modules/core/src/algorithm.cpp +++ b/modules/core/src/algorithm.cpp @@ -55,6 +55,19 @@ Algorithm::~Algorithm() CV_TRACE_FUNCTION(); } +void Algorithm::write(const Ptr& fs, const String& name) const +{ + CV_TRACE_FUNCTION(); + if(name.empty()) + { + write(*fs); + return; + } + *fs << name << "{"; + write(*fs); + *fs << "}"; +} + void Algorithm::save(const String& filename) const { CV_TRACE_FUNCTION(); diff --git a/modules/features2d/include/opencv2/features2d.hpp b/modules/features2d/include/opencv2/features2d.hpp index 0ab11b63a1..119782bf1e 100644 --- a/modules/features2d/include/opencv2/features2d.hpp +++ b/modules/features2d/include/opencv2/features2d.hpp @@ -210,11 +210,15 @@ public: virtual void write( FileStorage&) const; - virtual void read( const FileNode&); + // see corresponding cv::Algorithm method + CV_WRAP virtual void read( const FileNode&); //! Return true if detector object is empty CV_WRAP virtual bool empty() const; CV_WRAP virtual String getDefaultName() const; + + // see corresponding cv::Algorithm method + CV_WRAP inline void write(const Ptr& fs, const String& name = String()) const { Algorithm::write(fs, name); } }; /** Feature detectors in OpenCV have wrappers with a common interface that enables you to easily switch @@ -985,7 +989,8 @@ public: read(fs.root()); } // Reads matcher object from a file node - virtual void read( const FileNode& ); + // see corresponding cv::Algorithm method + CV_WRAP virtual void read( const FileNode& ); // Writes matcher object to a file storage virtual void write( FileStorage& ) const; @@ -1012,6 +1017,10 @@ public: CV_WRAP static Ptr create( int matcherType ); + + // see corresponding cv::Algorithm method + CV_WRAP inline void write(const Ptr& fs, const String& name = String()) const { Algorithm::write(fs, name); } + protected: /** * Class to work with descriptors from several images as with one merged matrix. From 3c795a0dabb1c6bea6ad44c40937bdebaf523bb0 Mon Sep 17 00:00:00 2001 From: Pavel Rojtberg Date: Fri, 4 Aug 2017 17:12:20 +0200 Subject: [PATCH 2/2] add test_algorithm_rw using AKAZE --- modules/python/test/test_algorithm_rw.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 modules/python/test/test_algorithm_rw.py diff --git a/modules/python/test/test_algorithm_rw.py b/modules/python/test/test_algorithm_rw.py new file mode 100644 index 0000000000..788fa220bc --- /dev/null +++ b/modules/python/test/test_algorithm_rw.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +''' +Algorithm serializaion test +''' +import cv2 + +from tests_common import NewOpenCVTests + +class algorithm_rw_test(NewOpenCVTests): + def test_algorithm_rw(self): + # some arbitrary non-default parameters + gold = cv2.AKAZE_create(descriptor_size=1, descriptor_channels=2, nOctaves=3, threshold=4.0) + gold.write(cv2.FileStorage("params.yml", 1), "AKAZE") + + fs = cv2.FileStorage("params.yml", 0) + algorithm = cv2.AKAZE_create() + algorithm.read(fs.getNode("AKAZE")) + + self.assertEqual(algorithm.getDescriptorSize(), 1) + self.assertEqual(algorithm.getDescriptorChannels(), 2) + self.assertEqual(algorithm.getNOctaves(), 3) + self.assertEqual(algorithm.getThreshold(), 4.0)