OpenCV 3.1.0 : How to use BackgroundSubtractorMOG class - c++

I have OpenCV 3.1.0 installed.
I want to use BackgroundSubtractorMOG so in my file i have these includes:
//opencv
#include "opencv2/imgcodecs.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include <opencv2/highgui.hpp>
#include <opencv2/video.hpp>
#include <opencv2/video/background_segm.hpp>
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
I declare a pointer to a class of this type:
Ptr<BackgroundSubtractorMOG> pMOG;
And I initialize the pointer like this:
pMOG = createBackgroundSubtractorMOG();
When I try to compile I get this:
/home/edd/Desktop/compvis/FML.cpp:19:5: error: ‘BackgroundSubtractorMOG’ was not declared in this scope
Ptr<BackgroundSubtractorMOG> pMOG; //MOG2 Background subtractor
^
/home/edd/Desktop/compvis/FML.cpp:19:28: error: template argument 1 is invalid
Ptr<BackgroundSubtractorMOG> pMOG; //MOG2 Background subtractor
^
/home/edd/Desktop/compvis/FML.cpp: In function ‘int main(int, char**)’:
/home/edd/Desktop/compvis/FML.cpp:29:42: error: ‘createBackgroundSubtractorMOG’ was not declared in this scope
pMOG = createBackgroundSubtractorMOG(); //MOG approach
^
I tried to look in the documentation. I couldn't figure out what header to include. I couldn't figure out how to instantiate an instance of the class correctly.
What do ?

In the code you've posted, I don't see any statement regarding namespaces. (e.g. using namespace cv in your preamble). Otherwise, you have to preface OpenCV code with cv:: (e.g. cv::Mat frame to declare a Mat object).
EDIT:
#include "opencv2/opencv.hpp"
#include "opencv2/bgsegm.hpp"
...
cv::Ptr<cv::BackgroundSubtractor> mog = cv::bgsegm::createBackgroundSubtractorMOG();
...
And by using the namespace:
#include "opencv2/opencv.hpp"
#include "opencv2/bgsegm.hpp"
using namespace cv
...
Ptr<BackgroundSubtractor> mog = bgsegm::createBackgroundSubtractorMOG();
...
Hope this helps,
MJ

Related

Why Scalar does not work?

Following code results in build success, but no window. Without "m = Scalar(255,0,0);", it creates black window. Why including scalar does not work?
#include <iostream>
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;
using namespace std;
int main() {
Mat m = Mat::zeros(200,200,CV_8UC3);
m = Scalar(255,0,0); //without this, it creates window.
imshow("m", m);
waitKey();
}
It will not give you any compilation error as Mat and Scalar are basically array types of OpenCV.
This is a possible duplicate of How to set all pixels of an OpenCV Mat to a specific value?
Your code have to work!
take a look at the doc:
C++: void imshow(const string& winname, InputArray mat)
and I can confirm you is working fine on my environment(opencv320, VisualStudio2017):
you need to clean the project and build again.

‘BackgroundSubtractorMOG’ is not a member of ‘cv’

I am working in Motion Detector Script but when i run my code i get this error every time when i use this function, but i don't know why it's wrong.
I am using opencv3, below is my code. I tried to run other examples i get it from web to same function, but the error still there. Any idea to fix it ?
This is the Error:
cv.cpp: In function ‘int main()’:
cv.cpp:23:4: error: ‘BackgroundSubtractorMOG’ is not a member of ‘cv’
My code :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>
#include <sstream>
#include <opencv2/video/background_segm.hpp>
using namespace std;
int main()
{
//Openthevideofile
cv::VideoCapture capture("/home/shar/Desktop/op.mp4");
//checkifvideosuccessfullyopened
if (!capture.isOpened())
return 0;
//currentvideoframe
cv::Mat frame;
//foregroundbinaryimage
cv::Mat foreground;
cv::namedWindow("ExtractedForeground");
//TheMixtureofGaussianobject
//used with all default parameters
cv::BackgroundSubtractorMOG mog;
bool stop(false);
//forallframesinvideo
while(!stop){
//readnextframeifany
if(!capture.read(frame))
break;
//updatethebackground
//andreturntheforeground
mog(frame,foreground,0.01)
//learningrate
//Complementtheimage
cv::threshold(foreground,foreground,128,255,cv::THRESH_BINARY_INV);
//showforeground
cv::imshow("ExtractedForeground",foreground);
//introduceadelay
//orpresskeytostop
if(cv::waitKey(10)>=0)
stop=true;
}
}
As #shar said, the answer is in this post. In order to create a smart pointer to the algorithm you need to do:
cv::Ptr<cv::BackgroundSubtractorMOG2> pMOG2 = cv::createBackgroundSubtractorMOG2();
EDIT:
And for use the algorithm:
float learningRate = 0.01; // or whatever
cv::Mat foreground;
pMOG2->apply(frame, foreground, learningRate);

opencv3 knearest object definition error

I'm trying to implement a digi recognition program with c++ and opencv3, when i define a varible using KNearest i get this error:
main.cpp:19:18: error: variable or field 'RunSelfTest' declared void
void RunSelfTest(KNearest& knn2);
^
main.cpp:19:18: error: 'KNearest' was not declared in this scope
main.cpp:19:18: note: suggested alternative:
In file included from c:/OpenCV/build/include/opencv2/ml/ml.hpp:48:0,
from main.cpp:1:
c:/OpenCV/build/include/opencv2/ml.hpp:397:20: note: 'cv::ml::KNearest'
class CV_EXPORTS_W KNearest : public StatModel
this is my code:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/ml/ml.hpp"
#include <iostream>
#include <stdio.h>
using namespace cv;
using namespace std;
void RunSelfTest(KNearest& knn2);
void AnalyseImage(KNearest knearest);
i totally can't find where is the problem
You want to use cv::ml::KNearest but you're trying to refer to it as KNearest while using namespace cv.
But KNearest is also inside the ml namespace inside cv::. Try this:
ml::KNearest
(Or remove using namespace …; from your code, since it's bad practice anyway, and simply refer to it as cv::ml::KNearest.)

NormalBayesClassifier is giving undeclared identifier in opencv code

Im trying to use NormalBayesClassifier in my code to apply bag of words. The pre-training matrix is ready and given to the trainme matrix. I am using it as follows:
NormalBayesClassifier classifier;
classifier.train(trainme, labels);
And I am getting the following error:
error C2065: 'NormalBayesClassifier' : undeclared identifier
I have added all the correct libraries and all other opencv functions work, including the features2d functions.
Here are my libraries:
#include <stdafx.h>
#include <stdlib.h>
#include <cv.hpp>
#include <cxcore.hpp>
#include <highgui.h>
#include <iostream>
#include "opencv2/nonfree/features2d.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/nonfree/nonfree.hpp"
#include <fstream>
#include <windows.h>
using namespace cv;
using namespace std;
That should cover the NormalBayesClassifier function, then why am I getting this error?
According to the documentation, the class for the Normal Bayes Classifier is actually called CvNormalBayesClassifier.
The corresponding header file is:
#include "opencv2/ml/ml.hpp"

How to use FeatureDetector in OpenCV C++?

I am using VS 2008 with OpenCV 2.1 installed as per the installation guide. FeatureDetector/SurfFeatureDetector are listed as classes in the documentation, but they are considered "syntax error : identifier 'SurfFeatureDetector"
This is pretty much the entirety of my code.
#include "cv.h"
#include "highgui.h"
Ptr<FeatureDetector> *detect = new SurfFeatureDetector();
I've tried a bunch of random combinations to get this to work. How can I initialize a featuredetector?
You're declaring a pointer to a cv::Ptr -- you really should just have the cv::Ptr. Change your code to
#include "cv.h"
#include "highgui.h"
using namespace cv;
Ptr<FeatureDetector> detect = new SurfFeatureDetector();
and it should work.
I think you have installation problem, try resinstalling from here: sourceforge.net/projects/opencvlibrary/files/opencv-win/2.2
anther other option is that your precompiler already has __OPENCV_OLD_CV_H__ defined.
Try undefining it before #include "cv.h"
When you type #include "cv.h"
It automatically should include featurs2d. in fact cv.h includes the following:
#include "opencv2/core/core_c.h"
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc_c.h"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/tracking.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/flann/flann.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/objdetect/objdetect.hpp"
#include "opencv2/legacy/compat.hpp"
You need the OpenCV 2.x style C++ include. See below
#include "opencv2/features2d/features2d.hpp"
#include "cv.h"
#include "highgui.h"
using namespace cv;
Ptr<FeatureDetector> detect = new SurfFeatureDetector();
You need to:
#include <opencv2/nonfree/nonfree.hpp>
(from here: http://answers.opencv.org/question/411/feature-detector-crash/)