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 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);
//THIS PART SLOW DOWN THE FRAME RATE
/////Mat fgMask;
Mat fgMask(height, width, CV_8UC1);
pBackSub->apply(argbImgflippedY, fgMask, Learn);
//argbImgflippedY.copyTo(Out);
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);
GDrawMask720.copyTo(GDi);
}
else
{
andFilter= Mat::zeros(height, width, CV_8UC1);
bitwise_and(AfterOpening, GDrawMask480, andFilter);
GDrawMask480.copyTo(GDi);
}
argbImgflippedY.copyTo(Out, andFilter);
return height;
}
↧