在 cvCopyImage/cvResize 运行期间崩溃
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在返回一个指向堆栈分配变量的指针,该变量在函数结束时会自动销毁。
您需要在堆上分配帧,以便它能够在函数结束后继续存在:
请记住,您需要稍后
删除
帧,否则应用程序将泄漏内存。The problem is that you are returning a pointer to a stack allocated variable, which is automatically destroyed at the end of the function.
You need to allocate the frame on the heap so that it will live on past the end of the function:
Remember that you need to
delete
the frame at a later point or your application will leak memory.