I am facing performance issue in BeagleBone Black + Opencv Object Counter. I am using BackgroundSubtractorMOG2 for background subtraction and Contours Detection. Here is the code below:
cv::Mat frame;
cv::Mat resizedFrame;
cv::Mat back;
cv::Mat fore;
bool objStart = false;
bool objEnd = false;
bool start = true;
cv::Point startLine(0, 50); // this is the start of the line where I take decision
cv::Point endLine(1000, 50); // this is the end of the line
cv::VideoCapture cap("/home/moonzai/Videos/test.avi");
cv::BackgroundSubtractorMOG2 bg;
bg.set("nmixtures", 3);
vector<vector<cv::Point> > contours;
for(;;)
{
cap >> resizedFrame;
cv::resize(resizedFrame, frame, cv::Size(320, 240), 0, 0, cv::INTER_LINEAR); // I wrote this line when there were only 1 frame per second processing, I resized the frame to 320 X 240
if(start)
{
bg.operator ()(frame,fore);
bg.getBackgroundImage(back);
cv::erode(fore,fore,cv::Mat());
cv::dilate(fore,fore,cv::Mat());
cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
vector<cv::Rect> boundRect( contours.size() );
cv::Rect mainRect;
for( unsigned int i = 0; i < contours.size(); i++ )
{
boundRect[i] = boundingRect( cv::Mat(contours[i]) );
if(mainRect.area() < boundRect[i].area())
{
mainRect = boundRect[i];
}
}
if(LineIntersectsRect(startLine, endLine, mainRect)) // this function actually returns boolean, if rectangle is touching the line
{
objStart = true;
}
else
{
if(objStart)
{
objEnd = true;
}
}
if(objEnd && objStart)
{
counter ++;
cout << "Object: " << counter << endl;
objEnd = false;
objStart = false;
}
}
usleep(1000 * 33);
}
this code is working perfect on my desktop machine. but when I run this code on BeagleBone Black with Ubuntu 13.04 linux installed, this distribution has no GUI at all, I am working on terminal, it give me CPU usage of 80% with 2 frames per second of processing. Memory Usage is very low, about 8%, I am not getting my desired performance. so please guide me if I am doing something wrong.
The objective of my question is, is there any coding related issue or, BackgroundSubtractorMOG2 is resource hungry, so I have to use another way. If there is another way, then guide me what is that way?
thanks in advance...