Background substration MOG2 dramatically decreases frame rate

asked 2020-08-26 09:59:25 -0500

Alco62 gravatar image

Hi There,

I have the following code I am using with Unity,

When using the Background subtraction the frame rate drops dramatically. From 60 FPS to 30 FPS!

Please let me know if you see any other possible source of error, I am really new in OpenCV and I will be happy to receive your feedback.

I am using MOG2 like this:

     Ptr<BackgroundSubtractor> pBackSub ;

pBackSub = createBackgroundSubtractorMOG2();

extern "C" int __declspec(dllexport) __stdcall BackGroundSubs(Color32 **rawImage,  int width, int height, double Learn)

Mat Out(height, width, CV_8UC4, *rawImage);

///Mat frame;
Mat frame(height, width, CV_8UC3);
_capture >> frame;

if (frame.empty())
    return 0;

////Resize Mat to match the array passed to it from C#
Mat resizedMat(height, width, frame.type());
resize(frame, resizedMat, resizedMat.size(), cv::INTER_CUBIC);

///Mat argbImg;
Mat argbImg(height, width, CV_8UC4);
//Need to convert to RGBA
cvtColor(frame, argbImg, COLOR_BGR2RGBA);

//flip image on X axis and Y axis
///Mat argbImgflippedX;
Mat argbImgflippedX(height, width, CV_8UC4);
flip(argbImg, argbImgflippedX, 0);

///Mat argbImgflippedY;
Mat argbImgflippedY(height, width, CV_8UC4);;
flip(argbImgflippedX, argbImgflippedY, 1);

//Copy the final image in the pointer received.

/// --> argbImgflippedY.copyTo(Out);

/////Mat fgMask;
 Mat fgMask(height, width, CV_8UC1);
 pBackSub->apply(argbImgflippedY, fgMask, Learn);


Mat AfterOpening(height, width, CV_8UC1);
Mat element = getStructuringElement(MORPH_RECT, Size(10, 10), Point(1, 1));
morphologyEx(fgMask, AfterOpening, MORPH_OPEN, element);

///Mat andFilter;
Mat andFilter(height, width, CV_8UC1);

Mat GDi(height, width, CV_8UC1);

if (height == 720) {
    andFilter = Mat::zeros(height, width, CV_8UC1);
    bitwise_and(AfterOpening, GDrawMask720, andFilter);

    andFilter= Mat::zeros(height, width, CV_8UC1);
    bitwise_and(AfterOpening, GDrawMask480, andFilter);

argbImgflippedY.copyTo(Out, andFilter);

return height;
edit retag flag offensive close merge delete


try BackgroundSubtractorCNT if you is the fastest one.

sturkmen gravatar imagesturkmen ( 2020-08-26 10:41:40 -0500 )edit