简体   繁体   English

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

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

I'm making simple webcam program using OpenCV 2.3 and got stuck by the compile error.我正在使用 OpenCV 2.3 制作简单的网络摄像头程序,但被编译错误卡住了。 Any idea will be appreciated.任何想法将不胜感激。

Upon compile, I get the following error at imwrite (in read function in the code below).编译后,我在 imwrite 收到以下错误(在下面的代码中读取 function)。

This sample that uses imwrite to save an image works on my environment, which indicates imwrite in OpenCV 2.3 should work on my env. 这个使用 imwrite 保存图像的示例适用于我的环境,这表明 OpenCV 2.3 中的 imwrite 应该适用于我的环境。

error:错误:

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> >&)’

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;

//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>

Btw, I'm confused whether imwrite takes 2 arguments or 3. Both the following link and highgui.hpp on my machine say 3 args, but the sample code I cited above ( from ros.org ) uses only 2 (which is because I'm doing the same).顺便说一句,我很困惑 imwrite 是需要 2 arguments 还是 3。以下链接和我机器上的 highgui.hpp 都说 3 args,但我上面引用的示例代码( 来自 ros.org )只使用 2(这是因为我我也在做同样的事情)。 http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite#imwrite http://opencv.itseez.com/modules/highgui/doc/reading_and_writing_images_and_video.html?highlight=imwrite#imwrite

ps.附言。 Forgive my posting the same question here with the one I sent to OpenCV@yahoogroups.com if you are subscribing to it.如果您订阅它,请原谅我在这里发布与我发送到 OpenCV@yahoogroups.com 的问题相同的问题。 The reason I did this is because this website seems more interactive and convenient for various purposes.我这样做的原因是因为这个网站对于各种目的来说似乎更具交互性和方便性。

The third param is optional (array of format dependent parameters).第三个参数是可选的(格式相关参数的数组)。 The error you are getting is because 'sharedImage' is of type Mat* that can't cast automatically to 'const cv::_InputArray&', the expected type for imwrite.您收到的错误是因为“sharedImage”的类型为 Mat*,无法自动转换为 imwrite 的预期类型“const cv::_InputArray&”。 If look at the example more carefully, you'll see that the type of the parameter passed in as second is actually a 'Mat' (not a Mat*).如果更仔细地看这个例子,你会发现作为 second 传入的参数的类型实际上是一个“Mat”(不是一个 Mat*)。 Hope this helps.希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM