gpt4 book ai didi

c++ - cv::FileStorage 在 Qt 中不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 14:10:52 34 4
gpt4 key购买 nike

我有一个用 Xcode 编写的命令行 openCV 程序,它打开两个 XML 文件,分析然后比较它们。但是,我现在正尝试使用 QtCreator 将该程序放入 GUI 中,以便更容易理解输出。

问题是,Qt 似乎不喜欢 cv::FileStorage。程序编译正常,但是当我实际尝试执行 OpenCV 部分时,它崩溃了,并给出了这个错误:

OpenCV Error: Null pointer (NULL or empty buffer) in cvOpenFileStorage, file /tmp/OpenCV-2.4.3/modules/core/src/persistence.cpp, line 2702 Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must reimplement QApplication::notify() and catch all exceptions there.

The program has unexpectedly finished.

有谁知道为什么 Qt 似乎不喜欢运行这个程序?或者是否有一种方法可以将 OpenCV 程序干净地集成到 Qt 程序中?

注意这是 Qt GUI 的样子:

enter image description here

用户使用相关按钮选择两个 XML 文件。这些文件的路径随后被存储并显示在按钮旁边的框中。然后,我使用以下方法将 QString 转换为 std::string:

std::string file1path = file1Name.toUtf8().constData();
std::string file2path = file2Name.toUtf8().constData();

然后将 file1path 和 file2path 传递给 cv::FileStorage 命令,如下所示:

//Create File Storage
FileStorage storage1;
storage1.open(file1path, FileStorage::READ);

FileStorage storage2;
storage2.open(file2path, FileStorage::READ);

最佳答案

它不是专门针对 Qt 的。
OpenCV 在您使用 fileStorage 时抛出错误,文件不存在、不可读,或者您正在尝试读取错误的类型。

Qt 给你这个错误是因为异常已经到达它的通用错误处理程序,而不是 openCv,它会打印一个更有用的错误。

您可以通过向 QApplication 添加方法来向 Qt 应用程序添加全局异常处理程序

bool QApplication::notify ( QObject * receiver, QEvent * event )
{
try{
return QApplication::notify( received, event );
}
catch ( const std::exception &e )
{
QApplication::postEvent( this, new MyEvent( e.what() );
}
catch ( ... )
{

}

另见 http://qt-project.org/doc/qt-4.8/exceptionsafety.html

关于c++ - cv::FileStorage 在 Qt 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14430510/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com