1
//cv::BackgroundSubtractorMOG2 bg = cv::BackgroundSubtractorMOG2();
cv::Ptr< BackgroundSubtractorMOG2 >createBackgroundSubtractorMOG2();
bg.set("history", 1000);
bg.set("nmixtures", 3);
bg.set("backgroundRatio", 0.7);
bg.set("detectShadows", false);

//background subtractor for the filterTotalBackground results
//cv::BackgroundSubtractorMOG2 bg2 = cv::BackgroundSubtractorMOG2();
Ptr< BackgroundSubtractorMOG2  >createBackgroundSubtractorMOG2 ();
bg2.set("history", 1000);
bg2.set("nmixtures", 3);
bg2.set("backgroundRatio", 0.7);
bg2.set("detectShadows", false);

need to use this in a single code but I'm in confusion to where to declare bg and bg2 in the above shown code.the earlier commented lines are giving me errors.so if anyone could suggest a feasible solution, then it would be a great help

bg->operator()(total, fore); //error is here

//Computes a background image.
//C++: void BackgroundSubtractor::getBackgroundImage(OutputArray backgroundImage) const¶
bg->getBackgroundImage(back);


//find the moving objects in the frame and cv::erode the image
bg2->operator()(frame, fore2);    //error is here
bg2->getBackgroundImage(back2);
cv::erode(fore2, fore2, cv::Mat());
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
sophie
  • 23
  • 1
  • 7

2 Answers2

3

You must use the -> operator to get the cv::BackgroundSubtractorMOG2 object.

cv::Ptr<cv::BackgroundSubtractorMOG2> bg = cv::createBackgroundSubtractorMOG2();
bg->setHistory(1000);
bg->setNMixtures(3);
bg->setBackgroundRatio(0.7);
bg->setDetectShadows(false);

Another error:

You must change:

bg->operator()(frame, fore);

to

bg->apply(frame, fore);

I see that you are using an old tutorial, you could use this tutorial

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • thanks it worked. but can u help me out with the error which is caused due to the changes u said . the error is like – sophie Apr 26 '17 at 07:42
  • /humans/package_bgs/MovingDetection.cpp:610:22: error: 'struct cv::Ptr' has no member named 'operator()' bg2.operator()(frame, fore2); – sophie Apr 26 '17 at 07:42
  • should i comment my error lines and then add your edited snippet? if i do so then the whole snippet gives an error. and if i dont comment them then it gives no member named opertor – sophie Apr 26 '17 at 08:05
  • THANKYOU FOR YOUR TIME AND HELP. IT WORKED...:) – sophie Apr 26 '17 at 08:11
0

Actually, I got it working changing, in ofxCvMOG2.cpp,

mog2 = new cv::BackgroundSubtractorMOG2();

by:

mog2 = cv::createBackgroundSubtractorMOG2(500,16,true);

That means, setting history, threshold and detect shadows at the beginning, and commenting:

mog2->set("nmixtures", nMixtures);
mog2->set("detectShadows",1);
cigien
  • 57,834
  • 11
  • 73
  • 112
rojele
  • 80
  • 7
  • 1
    Please don't add follow up questions in answers. Feel free to post a new question, and link to this one if it's relevant. – cigien Oct 10 '21 at 17:03
  • Sorry, my fault. I posted it in the wrong post! Thanks for saying it – rojele Oct 10 '21 at 17:10
  • 1
    No worries. Also, I just edited out the follow up part of the answer, on the assumption that the rest of it does answer the OP's question. If it doesn't (i.e. the entire answer was meant to be a separate question), you can go ahead and delete this answer. – cigien Oct 10 '21 at 17:12