Opencv + BeagleBone黑色性能问题中的对象计数器

时间:2013-09-10 08:31:27

标签: c++ performance opencv background-subtraction beagleboneblack

我在BeagleBone Black + Opencv对象计数器中面临性能问题。我使用BackgroundSubtractorMOG2进行背景减法和轮廓检测。以下是代码:

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);
}

此代码在我的桌面计算机上运行良好。但是当我在安装了Ubuntu 13.04 linux的BeagleBone Black上运行此代码时,这个发行版根本就没有GUI,我正在使用终端,它给我的CPU使用率为80%,每秒处理2帧。内存使用率非常低,大约8%,我没有达到我想要的性能。如果我做错了,请指导我。

我的问题的目标是,是否有任何编码相关的问题,或者BackgroundSubtractorMOG2是资源匮乏的,所以我必须使用另一种方式。如果有另一种方式,那么引导我那是什么方式?

提前感谢...

2 个答案:

答案 0 :(得分:0)

我认为最好的选择是使用分析器(非常困难,很容易使用,但对我来说仍然足够强大,但我不确定是否有linux版本)并检查你的代码中的哪一部分是一个问题 - 看一下这个讨论How can I profile C++ code running in Linux?(在你的情况下,接受的答案可能不是一个好的选择,所以也要仔细查看其他答案)。
您也可以尝试减少睡眠时间,它应该增加fps和CPU使用率。

答案 1 :(得分:0)

C ++应用程序性能高度依赖于编译器选项。你能提供用于编译opencv库和你的应用程序的gcc选项吗?