我正在使用OpenCV 2.3制作简单的网络摄像头程序,并被编译错误卡住。任何想法将不胜感激。无法保存从网络摄像头捕获的图像(使用OpenCV 2.3编辑错误)
编译后,我在imwrite(在下面的代码中的读取函数中)出现以下错误。
This sample使用imwrite保存图像在我的环境中工作,这表明在OpenCV 2.3中的imwrite应该在我的env上工作。
错误:
error: invalid initialization of reference of type ‘const cv::_InputArray&’ from expression of type ‘cv::Mat*’
/usr/local/include/opencv2/highgui/highgui.hpp:110: error: in passing argument 2 of ‘bool cv::imwrite(const std::string&, const cv::_InputArray&, const std::vector<int, std::allocator<int> >&)’
代码摘录:
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace std;
using namespace cv;
//IplImage* SampleClassA::dispImg = NULL;
Mat* SampleClassA::dispImg = NULL;
int read()
{
Mat* sharedImg;
sharedImg = getFrame();
if (sharedImg)
{
if (dispImg == NULL)
{
SampleClassA::dispImg = sharedImg;
}
Mat outMat;
outMat = imwrite("./out/sample.jpg", sharedImg);
}
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>
顺便说一句,我很困惑imwrite是否需要两个参数或3。我的两个机器上的以下链接并highgui.hpp说3参数,但我上面引用的示例代码(from ros.org)仅使用2(这是因为我在做同样的事情)。 http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite#imwrite
ps。请原谅我在这里发布与我发送给[email protected]的问题相同的问题(如果您订阅了该问题)。我这样做的原因是因为这个网站似乎更具互动性并且适合各种用途。
谢谢@crisbia。现在看到您的评论后,我只是通过在第二个imwrite的参数sharedImg(使* sharedImg)前添加'*'来解决编译错误。不知道它是否像我最初所期望的那样工作(我对指针还是比较陌生的),但是希望现在可以做得更好。 – IsaacS
很高兴为您提供帮助。祝你的项目:) – crisbia
@ user577:你不应该在那里使用指针。 Mat是引用计数的数据类型。您的代码已损坏,您正在返回一个指向本地变量的指针(请参阅http://en.wikipedia.org/wiki/Dangling_pointer)。 – etarion