【问题标题】:Can't save an image captured from webcam (imwrite compile error with OpenCV 2.3)无法保存从网络摄像头捕获的图像(使用 OpenCV 2.3 写入编译错误)
【发布时间】:2011-10-10 15:27:11
【问题描述】:

我正在使用 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 个参数,但我上面引用的示例代码 (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 的问题相同的问题。我这样做的原因是因为这个网站对于各种目的来说似乎更具交互性和方便性。

【问题讨论】:

    标签: c++ opencv webcam


    【解决方案1】:

    第三个参数是可选的(格式相关参数的数组)。 您收到的错误是因为“sharedImage”的类型为 Mat*,无法自动转换为 imwrite 的预期类型“const cv::_InputArray&”。如果更仔细地看这个例子,你会发现作为 second 传入的参数的类型实际上是一个“Mat”(不是一个 Mat*)。 希望这会有所帮助。

    【讨论】:

    • 谢谢@crisbia。现在,在看到您的评论后,我刚刚通过在 sharedImg 前面添加“*”来解决编译错误(这使得 *sharedImg)为 imwrite 的第二个参数。不知道它是否像我最初期望的那样工作(我对指针还是有点陌生​​),但希望它现在很好。
    • 很高兴为您提供帮助。祝你的项目好运:)
    • @user577:你根本不应该在那里使用指针。 Mat 是一种引用计数数据类型。您的代码已损坏,您正在返回一个指向局部变量的指针(请参阅en.wikipedia.org/wiki/Dangling_pointer)。
    猜你喜欢
    • 2013-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 2015-08-18
    • 1970-01-01
    • 2013-02-09
    相关资源
    最近更新 更多