OpenCV support about 3 types subtraction algorithm.
Those are MOG, MOG2, GMG algorithms.
Detailed algorithm explain is, please refer to opencv documnet.
In this post, just introduce source code and result of background subtraction.
firstly, see the results of 3 algorithms.
origin image
MOG result
MOG2 result
CMG result
Especially, in MOG2 result, we can see the gray pixel color, that means shadow.
This is result video of the source code.
#include < stdio.h> #include < iostream> #include < opencv2\opencv.hpp> #include < opencv2/core/core.hpp> #include < opencv2/highgui/highgui.hpp> #include < opencv2/video/background_segm.hpp> #ifdef _DEBUG #pragma comment(lib, "opencv_core247d.lib") #pragma comment(lib, "opencv_imgproc247d.lib") //MAT processing #pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor //#pragma comment(lib, "opencv_gpu247d.lib") //#pragma comment(lib, "opencv_features2d247d.lib") #pragma comment(lib, "opencv_highgui247d.lib") #pragma comment(lib, "opencv_ml247d.lib") //#pragma comment(lib, "opencv_stitching247d.lib"); //#pragma comment(lib, "opencv_nonfree247d.lib"); #pragma comment(lib, "opencv_video247d.lib") #else #pragma comment(lib, "opencv_core247.lib") #pragma comment(lib, "opencv_imgproc247.lib") #pragma comment(lib, "opencv_objdetect247.lib") //#pragma comment(lib, "opencv_gpu247.lib") //#pragma comment(lib, "opencv_features2d247.lib") #pragma comment(lib, "opencv_highgui247.lib") #pragma comment(lib, "opencv_ml247.lib") //#pragma comment(lib, "opencv_stitching247.lib"); //#pragma comment(lib, "opencv_nonfree247.lib"); #pragma comment(lib, "opencv_video247d.lib") #endif using namespace cv; using namespace std; int main() { //global variables Mat frame; //current frame Mat resizeF; Mat fgMaskMOG; //fg mask generated by MOG method Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method Mat fgMaskGMG; //fg mask fg mask generated by MOG2 method Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor pMOG = new BackgroundSubtractorMOG(); pMOG2 = new BackgroundSubtractorMOG2(); pGMG = new BackgroundSubtractorGMG(); char fileName[100] = "C:\\POSCO\\video\\/cctv 2.mov"; //Gate1_175_p1.avi"; //mm2.avi"; //";//_p1.avi"; VideoCapture stream1(fileName); //0 is the id of video device.0 if you have only one camera Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) ); //unconditional loop while (true) { Mat cameraFrame; if(!(stream1.read(frame))) //get one frame form video break; resize(frame, resizeF, Size(frame.size().width/4, frame.size().height/4) ); pMOG->operator()(resizeF, fgMaskMOG); pMOG2->operator()(resizeF, fgMaskMOG2); pGMG->operator()(resizeF, fgMaskGMG); //morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element); imshow("Origin", resizeF); imshow("MOG", fgMaskMOG); imshow("MOG2", fgMaskMOG2); imshow("GMG", fgMaskGMG); if (waitKey(30) >= 0) break; } }
..
refer to this url that is 3.2 version example.
http://study.marearts.com/2017/04/opencv-background-subtraction-32.html
Thanks man a lot
ReplyDeletedude, how to change the default video on your code?
ReplyDeletewhere I should put my own video to substitute the video's name that u have used on the code.
By the Way, AWESOME JOB MAN! :D
May God Bless U.
Please Reply ASAP. Thank You
I'm sorry for asking again.
ReplyDeletehow can i change the video file into real live video recording with my webcam?
which part that I should change?
Kindly Regards,
Hope you can give me your answer ASAP.
thanks
I got this error.. what should I do to avoid this error?
ReplyDeletehttps://www.dropbox.com/s/j0t1x248s7j4xfe/Screenshot%202014-06-30%2020.35.35.png
Hope u can reply soon.
Thanks
change like this
ReplyDeleteVideoCapture stream1(filename); -> VideoCapture stream1(0);
then code will get frame of cam image.
and..
in you error message, I never seen *.dll file, and this source code don't need that dll.
so check the dll what for.
thank you.
hey bro thanks for sharing that its very usefull for me can i get the document for algorithm or kindly send me the link here thanks
ReplyDeleteand kindly let me know how to get fps in text format so i can compile my results
ReplyDeletethanks
ReplyDeleteHi htc onex
ReplyDeleteif you go this page "http://docs.opencv.org/trunk/doc/tutorials/video/background_subtraction/background_subtraction.html", you will find more information. and save text to use FILE * fp; c style, ofstream o; c++ sytyle syntax.
Thank you
thanks bro its relly helpfull could u please tell me how to save fps in text format in opencv 2.3
Delete#include < opencv2\opencv.hpp>
ReplyDelete#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include < opencv2/video/background_segm.hpp>
#include
using namespace cv;
using namespace std;
int main()
{
time_t start, end;
double fps;
int counter = 0;
double sec;
time(&start);
//global variables
Mat frame; //current frame
Mat resize_blur_Img;
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Mat binaryImg;
//Mat TestImg;
Mat ContourImg; //fg mask fg mask generated by MOG2 method
Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
pMOG2 = new BackgroundSubtractorMOG2(56,100,true);//300,0.0);
char fileName[100] = "park.avi"; //video\\mm2.avi"; //mm2.avi"; //cctv 2.mov"; //mm2.avi"; //";//_p1.avi";
VideoCapture stream1(fileName); //0 is the id of video device.0 if you have only one camera
//morphology element
Mat element = getStructuringElement(MORPH_RECT, Size(7,7), Point(3,3) );
//unconditional loop
while (true) {
Mat cameraFrame;
if(!(stream1.read(frame))) //get one frame form video
break;
//Resize
resize(frame, resize_blur_Img, Size(frame.size().width/1, frame.size().height/1) );
//Blur
blur(resize_blur_Img, resize_blur_Img, Size(1,1) );
//Background subtraction
pMOG2->operator()(resize_blur_Img, fgMaskMOG2, -1);//,-0.5);
///////////////////////////////////////////////////////////////////
//pre procesing
//1 point delete
//morphologyEx(fgMaskMOG2, fgMaskMOG2, CV_MOP_ERODE, element);
morphologyEx(fgMaskMOG2, binaryImg, CV_MOP_CLOSE, element);
//morphologyEx(fgMaskMOG2, testImg, CV_MOP_OPEN, element);
//Shadow remove
//Binary
threshold(binaryImg, binaryImg, 128, 255, CV_THRESH_BINARY);
//Find contour
ContourImg = binaryImg.clone();
//less blob delete
vector< vector< Point> > contours;
findContours(ContourImg,
contours, // a vector of contours
CV_RETR_EXTERNAL, // retrieve the external contours
CV_CHAIN_APPROX_NONE); // all pixels of each contours
vector< Rect > output;
vector< vector< Point> >::iterator itc= contours.begin();
while (itc!=contours.end()) {
//Create bounding rect of object
//rect draw on origin image
Rect mr= boundingRect(Mat(*itc));
rectangle(resize_blur_Img, mr, CV_RGB(255,0,0));
++itc;
time(&end);
++counter;
sec = difftime (end, start);
fps = counter / sec;
printf("FPS = %.2f\n", fps);
}
///////////////////////////////////////////////////////////////////
//Display
imshow("Shadow_Removed", binaryImg);
imshow("Blur_Resize", resize_blur_Img);
imshow("MOG2", fgMaskMOG2);
if (waitKey(40) >= 0)
break;
}
}
in the above code kindly tell me where i should put *fp; to save my frames per seconds inb text
ReplyDeleteOK. the method of to save txt is just c, c++ skill.
ReplyDeleteThere is two method for saving the information of fps in the txt file.
First is to create txt file every frames.
The coding style is like that
int index=0
while( .. )
{
index++;
char filename[100];
sprintf(filename, "%d.txt", index);
FILE * fp = fopen(filename,"w")
fprintf(fp, "fps = %lf \n", fps )
fclose(fp)
}
why don't you read basic
Second coding style is ...
ReplyDeleteFILE * fp = fopen("fps_info.txt","w")
while(...)
{
fprintf("fps: %lf \n", fps)
}
fclose(fp)
Thank you.
ReplyDeleteAnd please search about save txt information in c, c++.
If you really cannot know how to do, i will send full source code for you.
Try it and ask me again.
Thank you very much.
thank you so much bro i am not good at it just trying to be i searched alot i coudnt fix my problem thats y i asked u for help thank you so much stay blessed
ReplyDeletesorry man i cant fix my problem help me out to rearrange this code
ReplyDeleteHi htc onex,
ReplyDeleteIs this your asking source code? plz check.
but I didn't run the source code in vs.
#include < stdio.h>
#include < iostream>
#include < opencv2\opencv.hpp>
#include < opencv2/core/core.hpp>
#include < opencv2/highgui/highgui.hpp>
#include < opencv2/video/background_segm.hpp>
#ifdef _DEBUG
#pragma comment(lib, "opencv_core247d.lib")
#pragma comment(lib, "opencv_imgproc247d.lib") //MAT processing
#pragma comment(lib, "opencv_objdetect247d.lib") //HOGDescriptor
//#pragma comment(lib, "opencv_gpu247d.lib")
//#pragma comment(lib, "opencv_features2d247d.lib")
#pragma comment(lib, "opencv_highgui247d.lib")
#pragma comment(lib, "opencv_ml247d.lib")
//#pragma comment(lib, "opencv_stitching247d.lib");
//#pragma comment(lib, "opencv_nonfree247d.lib");
#pragma comment(lib, "opencv_video247d.lib")
#else
#pragma comment(lib, "opencv_core247.lib")
#pragma comment(lib, "opencv_imgproc247.lib")
#pragma comment(lib, "opencv_objdetect247.lib")
//#pragma comment(lib, "opencv_gpu247.lib")
//#pragma comment(lib, "opencv_features2d247.lib")
#pragma comment(lib, "opencv_highgui247.lib")
#pragma comment(lib, "opencv_ml247.lib")
//#pragma comment(lib, "opencv_stitching247.lib");
//#pragma comment(lib, "opencv_nonfree247.lib");
#pragma comment(lib, "opencv_video247d.lib")
#endif
using namespace cv;
using namespace std;
int main()
{
//global variables
Mat frame; //current frame
Mat resizeF;
Mat fgMaskMOG; //fg mask generated by MOG method
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Mat fgMaskGMG; //fg mask fg mask generated by MOG2 method
Ptr< BackgroundSubtractor> pMOG; //MOG Background subtractor
Ptr< BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
Ptr< BackgroundSubtractorGMG> pGMG; //MOG2 Background subtractor
pMOG = new BackgroundSubtractorMOG();
pMOG2 = new BackgroundSubtractorMOG2();
pGMG = new BackgroundSubtractorGMG();
char fileName[100] = "C:\\POSCO\\video\\/cctv 2.mov"; //Gate1_175_p1.avi"; //mm2.avi"; //";//_p1.avi";
VideoCapture stream1(fileName); //0 is the id of video device.0 if you have only one camera
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3), Point(1,1) );
//for check processing time
unsigned long AAtime=0, BBtime=0;
float TakeTime;
int Frame=0;
//unconditional loop
while (true) {
//for check processing time
AAtime = getTickCount();
Mat cameraFrame;
if(!(stream1.read(frame))) //get one frame form video
break;
resize(frame, resizeF, Size(frame.size().width/4, frame.size().height/4) );
pMOG->operator()(resizeF, fgMaskMOG);
pMOG2->operator()(resizeF, fgMaskMOG2);
pGMG->operator()(resizeF, fgMaskGMG);
//morphologyEx(fgMaskGMG, fgMaskGMG, CV_MOP_OPEN, element);
//for check processing time
BBtime = getTickCount();
TakeTime = (BBtime - AAtime)/getTickFrequency();
//save ms i file name and close
FILE *fp;
char str[100];
sprintf_s(str,"%d.txt", Frame);
fopen_s(&fp, str, "w");
fprintf(fp, "%d frame takes %lf ms, %lf fps \n",Frame, TakeTime, 1/TakeTime);
fclose(fp);
Frame++;
imshow("Origin", resizeF);
imshow("MOG", fgMaskMOG);
imshow("MOG2", fgMaskMOG2);
imshow("GMG", fgMaskGMG);
if (waitKey(30) >= 0)
break;
}
}
thank you.
thank you so much brother its working properly but its saving frames in seperate text files each frame seperately is there any way to get frames in one file
ReplyDeletebrother kindly share the description of functions in your code it will be helpfull for my research
ReplyDeleteHi bro i am getting these errors in VS
ReplyDeleteError 1 error C2065: 'BackgroundSubtractorGMG' : undeclared identifier
Error 2 error C2923: 'cv::Ptr' : 'BackgroundSubtractorGMG' is not a valid template type argument for parameter 'T'
Check header file and lib version.
DeleteExample version is 247, other version like 3.x will be change class location and method to usage.
Thank you.
How to make the foreground appear as the original frame pixels, which mean in color?
ReplyDeleteI need a source code for MOG background subtraction using openCV with C and not c++.
ReplyDeleteI need a source code for MOG background subtraction using openCV with C and not c++.
ReplyDeleteSorry, I don't know what different c and c++
DeleteThank you.
Hey bro, how to Get background model from BackgroundSubtractorMOG2 in c++
ReplyDeleteHelp mee, please !! thank you so much !!
This comment has been removed by the author.
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteHi,
ReplyDeletei am getting error "BackgroundSubtractorGMG" is undefined
Error: class "cv::backgroundSubstractor" ha no member ""operator().
Kindly give me sme suggestion
I get "BackgroundSubtractorGMG was not declared in this scope as error message, could you please help
ReplyDeleteI get the error 72:13: error: expected type-specifier before ‘BackgroundSubtractorMOG’
ReplyDeletepMOG = new BackgroundSubtractorMOG();
Refer this link - http://stackoverflow.com/questions/29429863/why-is-createbackgroundsubtractorgmg-considered-obsolete-and-how-can-i-use-it
ReplyDeletehow to execute this program in opencv..is this a c++ program ..suggest commands to compile and execute this program in open cv
ReplyDeletethnx....for this useful information
ReplyDeletehey background_segm.hpp is in-build header file in opencv?
ReplyDeleteif not than tell me algorithm of that file...
Ptr<> - what is it and how can i use it?
ReplyDeleteYou can help yourself effectively retain by making synopsis diagrams of the data,http://collegeinfo.org utilizing streak cards, revising the material, skimming the data and finding the "10,000 foot view," and rehearsing application.
ReplyDeletehow I can detect the red object by using background subtraction method.
ReplyDeleteFor every one of the background checks distinguished in this section, the human resources office at each central command will decide if it or the employing division will play out the background check.background check services
ReplyDelete