Ask Your Question
0

questions in GMM...

asked 2016-12-03 18:27:36 -0600

Ruby_Sheng gravatar image

Can anybody figure out what's going on with the code? I am using opencv310

 #include <opencv2\opencv.hpp>
#include <iostream>  
#include <string>  
using namespace cv;


int main(int argc, char** argv)
{
    std::string videoFile = "1.avi";

    VideoCapture capture;
    capture.open(videoFile);

    if (!capture.isOpened())
    {
        std::cout << "read video failure" << std::endl;
        return -1;
    }


   Ptr<BackgroundSubtractorMOG2> subtractor;

    cv::Mat foreground;
    cv::Mat background;

    cv::Mat frame;
    long frameNo = 0;
    while (capture.read(frame))
    {
        ++frameNo;

        std::cout << frameNo << std::endl;

        // 运动前景检测,并更新背景  
        subtractor->apply(frame, foreground, 0.001);

        // 腐蚀  
        cv::erode(foreground, foreground, cv::Mat());

        // 膨胀  
        cv::dilate(foreground, foreground, cv::Mat());



        cv::imshow("video", foreground);
        cv::imshow("background", background);


        if (cv::waitKey(25) > 0)
        {
            break;
        }
    }



    return 0;
}
edit retag flag offensive close merge delete

Comments

1

Well, you're never setting background to anything at all, ever. More importantly, what's not working?

Tetragramm gravatar imageTetragramm ( 2016-12-03 18:55:26 -0600 )edit

-the background is in cv::Mat background;

  • the error is:
    • An unhandled exception (in GMM2.exe) at 0x00007FF7CACE282F: 0xC0000005: An access violation occurs when the location 0x0000000000000000 is read.
  • thanks
Ruby_Sheng gravatar imageRuby_Sheng ( 2016-12-04 06:36:19 -0600 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2016-12-04 08:46:43 -0600

Tetragramm gravatar image

First, you never actually create the subtractor object. You need to call the create function like so:

Ptr<BackgroundSubtractorMOG2> subtractor = createBackgroundSubtractorMOG2();

Secondly, you're never actually filling background. You need to get the background image by calling

subtractor->getBackgroundImage(background);
edit flag offensive delete link more

Comments

Yeah~~~the problem is solved thanks a lot

Ruby_Sheng gravatar imageRuby_Sheng ( 2016-12-05 01:33:43 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-12-03 18:27:36 -0600

Seen: 187 times

Last updated: Dec 04 '16