diff --git a/modules/objdetect/include/opencv2/objdetect/aruco_detector.hpp b/modules/objdetect/include/opencv2/objdetect/aruco_detector.hpp index 69c7defd1c..80f84339cd 100644 --- a/modules/objdetect/include/opencv2/objdetect/aruco_detector.hpp +++ b/modules/objdetect/include/opencv2/objdetect/aruco_detector.hpp @@ -85,11 +85,7 @@ struct CV_EXPORTS_W_SIMPLE DetectorParameters { /** @brief Write a set of DetectorParameters to FileStorage */ - bool writeDetectorParameters(FileStorage& fs); - - /** @brief simplified API for language bindings - */ - CV_WRAP bool writeDetectorParameters(const Ptr& fs, const String& name = String()); + CV_WRAP bool writeDetectorParameters(FileStorage& fs, const String& name = String()); /// minimum window size for adaptive thresholding before finding contours (default 3). CV_PROP_RW int adaptiveThreshWinSizeMin; @@ -237,11 +233,7 @@ struct CV_EXPORTS_W_SIMPLE RefineParameters { /** @brief Write a set of RefineParameters to FileStorage */ - bool writeRefineParameters(FileStorage& fs); - - /** @brief simplified API for language bindings - */ - CV_WRAP bool writeRefineParameters(const Ptr& fs, const String& name = String()); + CV_WRAP bool writeRefineParameters(FileStorage& fs, const String& name = String()); /** @brief minRepDistance minimum distance between the corners of the rejected candidate and the reprojected marker in order to consider it as a correspondence. @@ -347,7 +339,7 @@ public: /** @brief simplified API for language bindings */ - CV_WRAP inline void write(const Ptr& fs, const String& name = String()) { Algorithm::write(fs, name); } + CV_WRAP inline void write(FileStorage& fs, const String& name) { Algorithm::write(fs, name); } /** @brief Reads algorithm parameters from a file storage */ diff --git a/modules/objdetect/include/opencv2/objdetect/aruco_dictionary.hpp b/modules/objdetect/include/opencv2/objdetect/aruco_dictionary.hpp index e306c65fe8..498bcf1c33 100644 --- a/modules/objdetect/include/opencv2/objdetect/aruco_dictionary.hpp +++ b/modules/objdetect/include/opencv2/objdetect/aruco_dictionary.hpp @@ -50,11 +50,7 @@ class CV_EXPORTS_W_SIMPLE Dictionary { /** @brief Write a dictionary to FileStorage, format is the same as in readDictionary(). */ - void writeDictionary(FileStorage& fs); - - /** @brief simplified API for language bindings - */ - CV_WRAP void writeDictionary(Ptr& fs, const String& name = String()); + CV_WRAP void writeDictionary(FileStorage& fs, const String& name = String()); /** @brief Given a matrix of bits. Returns whether if marker is identified or not. * diff --git a/modules/objdetect/src/aruco/aruco_detector.cpp b/modules/objdetect/src/aruco/aruco_detector.cpp index c23545c6ad..4ef4710b01 100644 --- a/modules/objdetect/src/aruco/aruco_detector.cpp +++ b/modules/objdetect/src/aruco/aruco_detector.cpp @@ -16,77 +16,73 @@ namespace aruco { using namespace std; -static inline bool readWrite(DetectorParameters ¶ms, const Ptr& readNode, - const Ptr& writeStorage = nullptr) { - CV_Assert(!readNode.empty() || !writeStorage.empty()); +static inline bool readWrite(DetectorParameters ¶ms, const FileNode* readNode, + FileStorage* writeStorage = nullptr) +{ + CV_Assert(readNode || writeStorage); bool check = false; - check |= readWriteParameter("adaptiveThreshWinSizeMin", params.adaptiveThreshWinSizeMin, *readNode, *writeStorage); - check |= readWriteParameter("adaptiveThreshWinSizeMax", params.adaptiveThreshWinSizeMax, *readNode, *writeStorage); - check |= readWriteParameter("adaptiveThreshWinSizeStep", params.adaptiveThreshWinSizeStep, *readNode, *writeStorage); - check |= readWriteParameter("adaptiveThreshConstant", params.adaptiveThreshConstant, *readNode, *writeStorage); - check |= readWriteParameter("minMarkerPerimeterRate", params.minMarkerPerimeterRate, *readNode, *writeStorage); - check |= readWriteParameter("maxMarkerPerimeterRate", params.maxMarkerPerimeterRate, *readNode, *writeStorage); + check |= readWriteParameter("adaptiveThreshWinSizeMin", params.adaptiveThreshWinSizeMin, readNode, writeStorage); + check |= readWriteParameter("adaptiveThreshWinSizeMax", params.adaptiveThreshWinSizeMax, readNode, writeStorage); + check |= readWriteParameter("adaptiveThreshWinSizeStep", params.adaptiveThreshWinSizeStep, readNode, writeStorage); + check |= readWriteParameter("adaptiveThreshConstant", params.adaptiveThreshConstant, readNode, writeStorage); + check |= readWriteParameter("minMarkerPerimeterRate", params.minMarkerPerimeterRate, readNode, writeStorage); + check |= readWriteParameter("maxMarkerPerimeterRate", params.maxMarkerPerimeterRate, readNode, writeStorage); check |= readWriteParameter("polygonalApproxAccuracyRate", params.polygonalApproxAccuracyRate, - *readNode, *writeStorage); - check |= readWriteParameter("minCornerDistanceRate", params.minCornerDistanceRate, *readNode, *writeStorage); - check |= readWriteParameter("minDistanceToBorder", params.minDistanceToBorder, *readNode, *writeStorage); - check |= readWriteParameter("minMarkerDistanceRate", params.minMarkerDistanceRate, *readNode, *writeStorage); - check |= readWriteParameter("cornerRefinementMethod", params.cornerRefinementMethod, *readNode, *writeStorage); - check |= readWriteParameter("cornerRefinementWinSize", params.cornerRefinementWinSize, *readNode, *writeStorage); + readNode, writeStorage); + check |= readWriteParameter("minCornerDistanceRate", params.minCornerDistanceRate, readNode, writeStorage); + check |= readWriteParameter("minDistanceToBorder", params.minDistanceToBorder, readNode, writeStorage); + check |= readWriteParameter("minMarkerDistanceRate", params.minMarkerDistanceRate, readNode, writeStorage); + check |= readWriteParameter("cornerRefinementMethod", params.cornerRefinementMethod, readNode, writeStorage); + check |= readWriteParameter("cornerRefinementWinSize", params.cornerRefinementWinSize, readNode, writeStorage); check |= readWriteParameter("cornerRefinementMaxIterations", params.cornerRefinementMaxIterations, - *readNode, *writeStorage); + readNode, writeStorage); check |= readWriteParameter("cornerRefinementMinAccuracy", params.cornerRefinementMinAccuracy, - *readNode, *writeStorage); - check |= readWriteParameter("markerBorderBits", params.markerBorderBits, *readNode, *writeStorage); + readNode, writeStorage); + check |= readWriteParameter("markerBorderBits", params.markerBorderBits, readNode, writeStorage); check |= readWriteParameter("perspectiveRemovePixelPerCell", params.perspectiveRemovePixelPerCell, - *readNode, *writeStorage); + readNode, writeStorage); check |= readWriteParameter("perspectiveRemoveIgnoredMarginPerCell", params.perspectiveRemoveIgnoredMarginPerCell, - *readNode, *writeStorage); + readNode, writeStorage); check |= readWriteParameter("maxErroneousBitsInBorderRate", params.maxErroneousBitsInBorderRate, - *readNode, *writeStorage); - check |= readWriteParameter("minOtsuStdDev", params.minOtsuStdDev, *readNode, *writeStorage); - check |= readWriteParameter("errorCorrectionRate", params.errorCorrectionRate, *readNode, *writeStorage); + readNode, writeStorage); + check |= readWriteParameter("minOtsuStdDev", params.minOtsuStdDev, readNode, writeStorage); + check |= readWriteParameter("errorCorrectionRate", params.errorCorrectionRate, readNode, writeStorage); // new aruco 3 functionality - check |= readWriteParameter("useAruco3Detection", params.useAruco3Detection, *readNode, *writeStorage); - check |= readWriteParameter("minSideLengthCanonicalImg", params.minSideLengthCanonicalImg, *readNode, *writeStorage); + check |= readWriteParameter("useAruco3Detection", params.useAruco3Detection, readNode, writeStorage); + check |= readWriteParameter("minSideLengthCanonicalImg", params.minSideLengthCanonicalImg, readNode, writeStorage); check |= readWriteParameter("minMarkerLengthRatioOriginalImg", params.minMarkerLengthRatioOriginalImg, - *readNode, *writeStorage); + readNode, writeStorage); return check; } -bool DetectorParameters::readDetectorParameters(const FileNode& fn) { - if(fn.empty()) +bool DetectorParameters::readDetectorParameters(const FileNode& fn) +{ + if (fn.empty()) return false; - Ptr pfn = makePtr(fn); - return readWrite(*this, pfn); + return readWrite(*this, &fn); } -bool DetectorParameters::writeDetectorParameters(const Ptr& fs, const String& name) { - if (fs.empty()) - return false; - if(name.empty()) - return writeDetectorParameters(*fs); - *fs << name << "{"; - bool res = writeDetectorParameters(*fs); - *fs << "}"; +bool DetectorParameters::writeDetectorParameters(FileStorage& fs, const String& name) +{ + CV_Assert(fs.isOpened()); + if (!name.empty()) + fs << name << "{"; + bool res = readWrite(*this, nullptr, &fs); + if (!name.empty()) + fs << "}"; return res; } -bool DetectorParameters::writeDetectorParameters(FileStorage &fs) { - if (!fs.isOpened()) - return false; - return readWrite(*this, nullptr, makePtr(fs)); -} - -static inline bool readWrite(RefineParameters& refineParameters, const Ptr& readNode, - const Ptr& writeStorage = nullptr) { - CV_Assert(!readNode.empty() || !writeStorage.empty()); +static inline bool readWrite(RefineParameters& refineParameters, const FileNode* readNode, + FileStorage* writeStorage = nullptr) +{ + CV_Assert(readNode || writeStorage); bool check = false; - check |= readWriteParameter("minRepDistance", refineParameters.minRepDistance, *readNode, *writeStorage); - check |= readWriteParameter("errorCorrectionRate", refineParameters.errorCorrectionRate, *readNode, *writeStorage); - check |= readWriteParameter("checkAllOrders", refineParameters.checkAllOrders, *readNode, *writeStorage); + check |= readWriteParameter("minRepDistance", refineParameters.minRepDistance, readNode, writeStorage); + check |= readWriteParameter("errorCorrectionRate", refineParameters.errorCorrectionRate, readNode, writeStorage); + check |= readWriteParameter("checkAllOrders", refineParameters.checkAllOrders, readNode, writeStorage); return check; } @@ -94,27 +90,21 @@ RefineParameters::RefineParameters(float _minRepDistance, float _errorCorrection minRepDistance(_minRepDistance), errorCorrectionRate(_errorCorrectionRate), checkAllOrders(_checkAllOrders){} -bool RefineParameters::readRefineParameters(const FileNode &fn) { - if(fn.empty()) +bool RefineParameters::readRefineParameters(const FileNode &fn) +{ + if (fn.empty()) return false; - Ptr pfn = makePtr(fn); - return readWrite(*this, pfn); + return readWrite(*this, &fn); } -bool RefineParameters::writeRefineParameters(FileStorage &fs) { - if(!fs.isOpened()) - return false; - return readWrite(*this, nullptr, makePtr(fs)); -} - -bool RefineParameters::writeRefineParameters(const Ptr& fs, const String& name) { - if(fs.empty()) - return false; - if(name.empty()) - return writeRefineParameters(*fs); - *fs << name << "{"; - bool res = writeRefineParameters(*fs); - *fs << "}"; +bool RefineParameters::writeRefineParameters(FileStorage& fs, const String& name) +{ + CV_Assert(fs.isOpened()); + if (!name.empty()) + fs << name << "{"; + bool res = readWrite(*this, nullptr, &fs); + if (!name.empty()) + fs << "}"; return res; } @@ -1279,9 +1269,9 @@ void ArucoDetector::refineDetectedMarkers(InputArray _image, const Ptr &_ } } -void ArucoDetector::write(FileStorage &fs) const { - Ptr pfs = makePtr(fs); - arucoDetectorImpl->dictionary.writeDictionary(pfs); +void ArucoDetector::write(FileStorage &fs) const +{ + arucoDetectorImpl->dictionary.writeDictionary(fs); arucoDetectorImpl->detectorParams.writeDetectorParameters(fs); arucoDetectorImpl->refineParams.writeRefineParameters(fs); } diff --git a/modules/objdetect/src/aruco/aruco_dictionary.cpp b/modules/objdetect/src/aruco/aruco_dictionary.cpp index a0177acfee..79eac9a649 100644 --- a/modules/objdetect/src/aruco/aruco_dictionary.cpp +++ b/modules/objdetect/src/aruco/aruco_dictionary.cpp @@ -46,7 +46,13 @@ bool Dictionary::readDictionary(const cv::FileNode& fn) { return true; } -void Dictionary::writeDictionary(FileStorage &fs) { +void Dictionary::writeDictionary(FileStorage& fs, const String &name) +{ + CV_Assert(fs.isOpened()); + + if (!name.empty()) + fs << name << "{"; + fs << "nmarkers" << bytesList.rows; fs << "markersize" << markerSize; fs << "maxCorrectionBits" << maxCorrectionBits; @@ -61,14 +67,9 @@ void Dictionary::writeDictionary(FileStorage &fs) { marker.push_back(bitMarker.at(j) + '0'); fs << markerName << marker; } -} -void Dictionary::writeDictionary(Ptr& fs, const String &name) { - if(name.empty()) - return writeDictionary(*fs); - *fs << name << "{"; - writeDictionary(*fs); - *fs << "}"; + if (!name.empty()) + fs << "}"; } diff --git a/modules/objdetect/src/aruco/aruco_utils.hpp b/modules/objdetect/src/aruco/aruco_utils.hpp index e3aa62051a..d7d29a7d18 100644 --- a/modules/objdetect/src/aruco/aruco_utils.hpp +++ b/modules/objdetect/src/aruco/aruco_utils.hpp @@ -31,10 +31,12 @@ inline bool readParameter(const std::string& name, T& parameter, const FileNode& } template -inline bool readWriteParameter(const std::string& name, T& parameter, const FileNode& readNode, FileStorage& writeStorage) { - if (!readNode.empty()) - return readParameter(name, parameter, readNode); - writeStorage << name << parameter; +inline bool readWriteParameter(const std::string& name, T& parameter, const FileNode* readNode, FileStorage* writeStorage) +{ + if (readNode) + return readParameter(name, parameter, *readNode); + CV_Assert(writeStorage); + *writeStorage << name << parameter; return true; }