Ask Your Question

BackgroundSubtractor is not a member of 'cv' on VS17 C++

asked 2019-01-18 07:33:20 -0500

arvids.p gravatar image

I'm building a background subtractor on Visual Studio 17. I've included the opencv_world.dll. When typing cv::Bac autofinish offers BackgroundSubtractor and moving poiter to it shows correct info about it. But when i build the project, it throws errors.

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/video.hpp>
#include "stdafx.h"
#include <iostream>
#include <stdlib.h>

using namespace std;
using namespace cv;

cv::Ptr<cv::BackgroundSubtractor> backsub = cv::createBackgroundSubtractorMOG2(200, 125, false);

This throws errors like:

'BackgroundSubtractor': is not a member of 'cv'
'BackgroundSubtractor': undeclared identifier

I tried adding opencv_contrib NuGet package, but that did not help. Also tried including all sorts of header files, using bgsegm::createBackgroundSubtractorMOG, cv::createBackgroundSubtractorMOG2.

edit retag flag offensive close merge delete


  • which opencv version do you have ?
  • there is no support for nuget packages from opencv at all (and imho, those are badly maintained)
  • you don't need any contrib modules for the Mog2
berak gravatar imageberak ( 2019-01-18 07:40:43 -0500 )edit

opencv3.4.1 It worked well without the bgs. I used cv::absdiff before.

arvids.p gravatar imagearvids.p ( 2019-01-18 07:44:12 -0500 )edit

sorry, but i cannot reproduce your problem.

and it's still a compiler problem (so, something about headers, not libs)

berak gravatar imageberak ( 2019-01-18 07:49:37 -0500 )edit

Could it be because of some unnecessary headers?

arvids.p gravatar imagearvids.p ( 2019-01-18 07:59:02 -0500 )edit

not really, but for sure you should remove the stdafx.h shit (that's from MFC, 2 decades ago)

also try to disable "precompiled headers" in general.

try with a clean "empty" project. all you should need for the bgMOG2 is "opencv2/video.hpp"

berak gravatar imageberak ( 2019-01-18 08:03:28 -0500 )edit

Removing stdafx created an other error. However, moving it to the top of the file seems to have fixed the error!

arvids.p gravatar imagearvids.p ( 2019-01-18 08:13:42 -0500 )edit

that's interesting, again it hints at a problem with precompiled headers.

and you really should NOT use stdafx.h with opencv projects. see, what's in there, and move anything you need into your main program, and throw anything else (does it have "windows.h ? omg !) away.

berak gravatar imageberak ( 2019-01-18 08:16:08 -0500 )edit

oh, let me guess: problems with ACCESS_READ or such ? really, get rid of windows.h there

berak gravatar imageberak ( 2019-01-18 08:18:12 -0500 )edit

Thank you for the help!

arvids.p gravatar imagearvids.p ( 2019-01-18 08:33:50 -0500 )edit

maybe you could write an answer, describing, what you did, in the end ?

might be quite helpful for others ;)

berak gravatar imageberak ( 2019-01-18 08:39:52 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-01-25 06:55:42 -0500

arvids.p gravatar image

The solution was just moving #include "stdafx.h" to the top of the file.

edit flag offensive delete link more



This is just one more reason why one should make an empty project, and then add files to it. No more stdafx.h

sjhalayka gravatar imagesjhalayka ( 2019-01-25 09:11:48 -0500 )edit

Question Tools

1 follower


Asked: 2019-01-18 07:33:20 -0500

Seen: 183 times

Last updated: Jan 25 '19