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

Can't save an image captured from webcam (imwrite compile error with OpenCV 2.3)

本文关键字:OpenCV imwrite 编译 错误 图像 网络 保存 摄像头      更新时间:2023-10-16

我正在使用OpenCV 2.3制作简单的网络摄像头程序,并被编译错误卡住。如有任何意见,我将不胜感激。

在编译后,我在imwrite(在下面的代码中的read函数中)得到以下错误:

这个使用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个参数,但我上面引用的示例代码(来自ros.org)只使用了2个(这是因为我也在做同样的事情)。http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite imwrite

p。如果你订阅了OpenCV@yahoogroups.com,请原谅我在这里发布了同样的问题。我这样做的原因是,这个网站似乎更具互动性,方便各种用途。

第三个参数是可选的(格式相关参数数组)。你得到的错误是因为'sharedImage'的类型是Mat*,不能自动转换为'const cv::_InputArray&',这是imwrite的预期类型。如果更仔细地查看示例,您将看到作为second传入的参数类型实际上是一个'Mat'(而不是Mat*)。