OpenCV createsamples.cpp 调试

OpenCV createsamples.cpp debugging

最新版本的 OpenCV(4) 没有用于创建 HAAR CASCADE 文件的 createsamples 功能。这真是令人难以置信。要获得此功能,有两种解决方案:1) 在另一台机器上下载旧版本或 2) 编译 createsamples.cpp 我选择第二项,因为任何人都可以随时接受 kluge。第二项带有许多错误,如下所示:

 harry@harry-usedmachine:/usr/include/opencv4$ sudo g++ createsamples.cpp 
[sudo] password for harry:        
In file included from createsamples.cpp:48:
/usr/include/opencv4/opencv2/core.hpp:2523:16: error: ‘FileStorage’ has not been declared
 2523 |     void write(FileStorage& fs) const;
      |                ^~~~~~~~~~~
/usr/include/opencv4/opencv2/core.hpp:2573:15: error: ‘FileStorage’ has not been declared
 2573 |     void save(FileStorage& fs) const;
      |               ^~~~~~~~~~~
/usr/include/opencv4/opencv2/core.hpp:2577:21: error: ‘FileStorage’ does not name a type
 2577 |     void load(const FileStorage& node);
      |                     ^~~~~~~~~~~
/usr/include/opencv4/opencv2/core.hpp:3088:24: error: ‘FileStorage’ has not been declared
 3088 |     virtual void write(FileStorage& fs) const { CV_UNUSED(fs); }
      |                        ^~~~~~~~~~~
/usr/include/opencv4/opencv2/core.hpp:3093:34: error: ‘FileStorage’ was not declared in this scope
 3093 |     CV_WRAP void write(const Ptr<FileStorage>& fs, const String& name = String()) const;
      |                                  ^~~~~~~~~~~
/usr/include/opencv4/opencv2/core.hpp:3093:45: error: template argument 1 is invalid
 3093 |     CV_WRAP void write(const Ptr<FileStorage>& fs, const String& name = String()) const;
      |                                             ^
/usr/include/opencv4/opencv2/core.hpp:3172:22: error: ‘FileStorage’ has not been declared
 3172 |     void writeFormat(FileStorage& fs) const;
      |                      ^~~~~~~~~~~
/usr/include/opencv4/opencv2/core.hpp: In static member function ‘static cv::Ptr<_Tp> cv::Algorithm::load(const String&, const String&)’:
/usr/include/opencv4/opencv2/core.hpp:3135:9: error: ‘FileStorage’ was not declared in this scope
 3135 |         FileStorage fs(filename, FileStorage::READ);
      |         ^~~~~~~~~~~
In file included from /usr/include/opencv4/opencv2/core.hpp:55,
                 from createsamples.cpp:48:
/usr/include/opencv4/opencv2/core.hpp:3136:19: error: ‘fs’ was not declared in this scope; did you mean ‘ffs’?
 3136 |         CV_Assert(fs.isOpened());
      |                   ^~
/usr/include/opencv4/opencv2/core/base.hpp:342:38: note: in definition of macro ‘CV_Assert’
  342 | #define CV_Assert( expr ) do { if(!!(expr)) ; else cv::error( cv::Error::StsAssert, #expr, CV_Func, __FILE__, __LINE__ ); } while(0)
      |                                      ^~~~
In file included from createsamples.cpp:48:
/usr/include/opencv4/opencv2/core.hpp:3137:41: error: ‘fs’ was not declared in this scope; did you mean ‘ffs’?
 3137 |         FileNode fn = objname.empty() ? fs.getFirstTopLevelNode() : fs[objname];
      |                                         ^~
      |                                         ffs
/usr/include/opencv4/opencv2/core.hpp:3137:18: error: ‘fn’ has incomplete type
 3137 |         FileNode fn = objname.empty() ? fs.getFirstTopLevelNode() : fs[objname];
      |                  ^~
In file included from /usr/include/opencv4/opencv2/core/base.hpp:58,
                 from /usr/include/opencv4/opencv2/core.hpp:55,
                 from createsamples.cpp:48:
/usr/include/opencv4/opencv2/core/cvstd.hpp:150:18: note: forward declaration of ‘class cv::FileNode’
  150 | class CV_EXPORTS FileNode; //for string constructor from FileNode
      |                  ^~~~~~~~
In file included from createsamples.cpp:48:
/usr/include/opencv4/opencv2/core.hpp: In static member function ‘static cv::Ptr<_Tp> cv::Algorithm::loadFromString(const String&, const String&)’:
/usr/include/opencv4/opencv2/core.hpp:3156:9: error: ‘FileStorage’ was not declared in this scope
 3156 |         FileStorage fs(strModel, FileStorage::READ + FileStorage::MEMORY);
      |         ^~~~~~~~~~~
/usr/include/opencv4/opencv2/core.hpp:3157:41: error: ‘fs’ was not declared in this scope; did you mean ‘ffs’?
 3157 |         FileNode fn = objname.empty() ? fs.getFirstTopLevelNode() : fs[objname];
      |                                         ^~
      |                                         ffs

许多错误来自 core.hpp 和 createsample.cpp。 createsample.cpp 在这里:https://github.com/opencv/opencv/blob/master/apps/createsamples/createsamples.cpp

和core.hpp在这里:https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core.hpp

非常感谢任何有关修复这些错误的见解。

最简单的方法似乎是下载较旧的 opencv 版本源代码,例如 https://github.com/opencv/opencv/archive/2.4.13.6.zip 并将其解压缩到任何位置。在该文件夹中执行

mkdir build
cd build
cmake ..
make
cd bin

在那里你会找到 opencv_createsamplesopencv_traincascade 等的二进制文件

没有必要安装那个旧版本(如果你使用 'make install' 就会发生这种情况),只需使用你当前安装的任何版本的 opencv 构建来完成所有任务,如果你需要创建示例或训练级联,在将其复制到的目录中使用此构建。