首页 文章

无法保存从网络摄像头捕获的图像(使用OpenCV 2.3 imwrite编译错误)

提问于
浏览
3

我正在使用OpenCV 2.3制作简单的网络摄像头程序,并因编译错误而陷入困境 . 任何想法将不胜感激 .

在编译时,我在imwrite上得到以下错误(在下面的代码中的读取函数中) .

This sample使用imwrite保存图像适用于我的环境,这表明OpenCV 2.3中的imwrite应该可以在我的环境中运行 .

错误:

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是否需要2个参数或3.我的机器上的以下链接和highgui.hpp都说3个args,但我上面引用的示例代码(from ros.org)仅使用2个(这是因为我正在做相同) . http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite#imwrite

PS . 如果您订阅了OpenCV@yahoogroups.com,请原谅我在这里发布的相同问题 . 我这样做的原因是因为这个网站似乎更具互动性和便利性,可用于各种目的 .

1 回答

  • 2

    第三个参数是可选的(格式相关参数数组) . 你得到的错误是因为'sharedImage'是Mat *类型,不能自动转换为'const cv :: _ InputArray&',即imwrite的预期类型 . 如果仔细查看示例,您会看到作为第二个传入的参数类型实际上是“Mat”(不是Mat *) . 希望这可以帮助 .

相关问题