【问题标题】:Catching OpenCV exception捕获 OpenCV 异常
【发布时间】:2017-07-20 17:57:07
【问题描述】:

我正在使用 OpenCV warpAffine 函数。但由于某种原因,它有时会失败(大约 1/10 次尝试)。我想查看引发的实际异常,但由于某种原因我无法捕获它。我的 try-catch 块有问题吗?:

//...
cout << "pos 1" << endl;
try
{
    cout << "pos 2" << endl;

    cv::warpAffine(rawImage, transformed, t, size, INTER_LINEAR, BORDER_CONSTANT);

    cout << "pos 3" << endl;
}
catch (const std::exception& e1)
{
    cerr << e1.what() << endl;
}
catch (const cv::Exception& e2) {
    cerr << e2.what() << endl;

}

即使我只有一个catch 块,它也不会碰到它。控制台总是(在它不工作的情况下)出现以下错误:

所以我并不是真的在关注 warpAffine 的问题,现在更多的是关于 try-catch。

【问题讨论】:

  • 这看起来更像是应用程序崩溃,而不是抛出的 c++ 异常。按调试看看有什么问题。
  • 这可能是一个无法捕获的断言。例如,warpAffine() 中有一个断言来检查图像是否为空。如果您有任何控制台输出,那将会很有帮助。

标签: c++ opencv


【解决方案1】:

这是一个陈旧的话题,但我想我从自己的经验中知道答案。 cv::Exception 继承自 std::exception。您需要将 cv::Exception 捕获移动到 std::exception 之前,如下所示:

try {
...
} catch(const cv::Exception& ex) {
...
} catch(const std::exception& ex) {
...
} catch(...) { //everything else
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-24
    • 2010-09-28
    • 2017-08-18
    • 2014-12-10
    • 2018-03-01
    相关资源
    最近更新 更多