在 cvCopyImage/cvResize 运行期间崩溃

发布于 2024-11-25 15:26:34 字数 1678 浏览 3 评论 0原文

我正在使用 OpenCV 2.3 制作简单的网络摄像头程序,但遇到了运行时错误。任何想法将不胜感激。

编译通过,但在运行时,出现以下错误(在下面代码中“read”函数中的 cvCopyImage/cvResize 处)。

错误:

OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /usr/local/src/OpenCV/OpenCV-2.3.0/modules/core/src/matrix.cpp, line 641
terminate called after throwing an instance of 'cv::Exception'
  what():  /usr/local/src/OpenCV/OpenCV-2.3.0/modules/core/src/matrix.cpp:641: error: (-5) Unknown array type in function cvarrToMat

代码摘录:

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

Mat* SampleClassA::dispImg = NULL;

int read()
{
    Mat* sharedImg;
    sharedImg = getFrame();
    if (sharedImg)
    {
        if (dispImg == NULL)
        {
            SampleClassA::dispImg = sharedImg;
        }
    cvCopyImage(sharedImg, SampleClassA::dispImg); // Crashes here.
    cvResize(sharedImg, SampleClassA::classifyImg); // Can crash here too when cvCopyImage is commented out.
    }
    sleep(100);
    return 1;
}

Mat* getFrame()
//IplImage* ReadRealTime::getFrame()
{
    if (!capture.isOpened()) // Actual capturing part is omitted here.
    {
        return NULL;
    }
    Mat frame;
    capture >> frame;
    return &frame;
}
</code>

我的猜测是我使用 Mat 或指针的方式有问题。我对 OpenCV 和 C/C++ 指针还是新手(我在另一个问题中得到了富有洞察力的评论 无法保存从网络摄像头捕获的图像(OpenCV 2.3 的 imwrite 编译错误) 指出可能的情况“悬空指针”,但这一次是一样的吗?)。

I'm making simple webcam program using OpenCV 2.3 and got stuck by the runtime error. Any idea will be appreciated.

Compile passes but upon running, I get the following error (at cvCopyImage/cvResize in 'read' function in the code below).

error:

OpenCV Error: Bad argument (Unknown array type) in cvarrToMat, file /usr/local/src/OpenCV/OpenCV-2.3.0/modules/core/src/matrix.cpp, line 641
terminate called after throwing an instance of 'cv::Exception'
  what():  /usr/local/src/OpenCV/OpenCV-2.3.0/modules/core/src/matrix.cpp:641: error: (-5) Unknown array type in function cvarrToMat

code excerpt:

#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"

using namespace std;
using namespace cv;

Mat* SampleClassA::dispImg = NULL;

int read()
{
    Mat* sharedImg;
    sharedImg = getFrame();
    if (sharedImg)
    {
        if (dispImg == NULL)
        {
            SampleClassA::dispImg = sharedImg;
        }
    cvCopyImage(sharedImg, SampleClassA::dispImg); // Crashes here.
    cvResize(sharedImg, SampleClassA::classifyImg); // Can crash here too when cvCopyImage is commented out.
    }
    sleep(100);
    return 1;
}

Mat* getFrame()
//IplImage* ReadRealTime::getFrame()
{
    if (!capture.isOpened()) // Actual capturing part is omitted here.
    {
        return NULL;
    }
    Mat frame;
    capture >> frame;
    return &frame;
}
</code>

My guess is there's something wrong either/both in the way I use Mat, or pointer. I'm still new both to OpenCV and C/C++ pointer (I've got an insightful comment in another question Can't save an image captured from webcam (imwrite compile error with OpenCV 2.3) to point out possible "dangling pointer", but is this time the same?).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

热情消退 2024-12-02 15:26:34

问题是您正在返回一个指向堆栈分配变量的指针,该变量在函数结束时会自动销毁。

  // ...
  Mat frame;
  capture >> frame;
  return &frame;
} // frame destroyed is at the end of the function yet you return a pointer to it!

您需要在堆上分配帧,以便它能够在函数结束后继续存在:

  // ...
  Mat* frame = new Mat(); // Maybe this needs parameters
  capture >> *frame;
  return frame;
}

请记住,您需要稍后删除帧,否则应用程序将泄漏内存。

The problem is that you are returning a pointer to a stack allocated variable, which is automatically destroyed at the end of the function.

  // ...
  Mat frame;
  capture >> frame;
  return &frame;
} // frame destroyed is at the end of the function yet you return a pointer to it!

You need to allocate the frame on the heap so that it will live on past the end of the function:

  // ...
  Mat* frame = new Mat(); // Maybe this needs parameters
  capture >> *frame;
  return frame;
}

Remember that you need to delete the frame at a later point or your application will leak memory.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文