0

我在 BeagleBone Black + Opencv Object Counter 中面临性能问题。我正在使用 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,我在终端上工作,它给我 80% 的 CPU 使用率,每秒处理 2 帧。内存使用率非常低,大约 8%,我没有得到我想要的性能。所以如果我做错了什么,请指导我。

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

提前致谢...

4

2 回答 2

0

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

于 2013-09-10T11:36:54.393 回答
0

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

于 2013-09-11T06:15:40.363 回答