简体   繁体   中英

Object Counter in Opencv + BeagleBone Black Performance Issue

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...

I think that the best option is to use profiler(Very sleepy is quite easy to use, but still enough powerful for me, but i'm not sure whether there is linux version) and check in which part of your code there is a problem - take a look at this discussion How can I profile C++ code running in Linux? (accepted answer may not be good option in your situation, so look carefully at other answers too).
Also you may just try to decrease sleep time, it should increase fps and CPU usage.

C++ application performance is highly dependent on compiler options. Could you please provide gcc options that used to compile opencv library and your application?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM