BackgroundSubtractorMOG2,我可以从中保存图像吗?从 BackgroundSubtractorMOG2 保存图像时出现错误

BackgroundSubtractorMOG2 , can i save image from it. i am getting error while saving images from BackgroundSubtractorMOG2

我正在使用 backgroundMOG2 来保存带有背景的图像,但是程序显示了计数,我需要用背景来保存图像。

#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <opencv2/video/background_segm.hpp>


int main()
{
    cv::Mat frame;
    cv::Mat back;
    cv::Mat fore;

    //cv::Mat img;
    cv::VideoCapture cap("C:\Users\Pavilion\Documents\Visual Studio 2013\Projects\dynamic\sherlock outtakes.avi");

    cv::BackgroundSubtractorMOG2 bg;
    bg.set("nmixtures", 3);

    //bg.bShadowDetecction=false;
    std::vector<std::vector<cv::Point>>countours;

    //cv::imread("C:\Users\Pavilion\Documents\Visual Studio 2013\Projects\video\video\frames\frame122.jpg");
    //cv::VideoCapture Cap;
    //std::vector<std::vector<cv::point> >contours;

    cv::namedWindow("frame");
    cv::namedWindow("background");

    for (;;)
    {
        cap >> frame;
        bg.operator()(frame, fore);
        bg.getBackgroundImage(back);
        cv::erode(fore, fore, cv::Mat());
        cv::dilate(fore, fore, cv::Mat());
        cv::findContours(fore,countours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
        cv::drawContours(frame, countours, -1, cv::Scalar(0, 0, 255), 1);
        cv::imshow("frame", back);
        // cv::imwrite("background",back);

        cv::imshow("background", frame);
        if (cv::waitKey(30) >=0) break;

}
    return 0;

}

这是我在保存图像时出错的代码。

谁能帮我解决这个问题

您需要指定扩展名(jpg、png、...)。

例如:

cv::imwrite("background.jpg",back);