捕获的网络摄像头视频中的对象检测在两分钟后卡住?

时间:2015-08-24 20:17:44

标签: c++ opencv memory-management object-detection

我使用的方法是使用OpenCV检测网络摄像头中的对象(已经过滤色)。它已经正常工作,但两分钟后fps速率因某种原因而降低。

一些事实:

  • 操作系统:Windows 8.1
  • OpenCV:2.4.10
  • IDE:Visual Studio 2013

我认为它与内存分配有关,因为我说它在前两分钟工作正常。

为了确定它是否与计算机有关,我已经在另一台电脑上测试了它,但也发生了同样的错误。

void findAndDrawRect(Mat* imgThreshold, Mat* imgOriginal, SerialPort^ arduino){

// contains rectangles - each rectangle exists of points- of imgThreshold 
std::vector<std::vector<cv::Point> > contours;

// contains hierarchical information from black-n white image
std::vector<cv::Vec4i> hierarchy;

cv::findContours(*imgThreshold,
    contours,
    hierarchy,
    CV_RETR_TREE,
    CV_CHAIN_APPROX_SIMPLE,
    cv::Point(0, 0));

std::vector<std::vector<cv::Point> > contours_poly(contours.size());

// holds place for rectangle of each contour on the image
cv::Rect boundRect;

// tempory variable to identify largest rectangle
double refArea = 0;
double area = 0;

// pointer to largest rectangle
cv::Rect* obj = NULL;

for (int i = 0; i < contours.size(); i++)
{
    // approximates a polygonal curve for each contour of third grade
    cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 3, true);

    // converts polynom of thrid grade to a rectangle
    boundRect = cv::boundingRect(cv::Mat(contours_poly[i]));

    // calculation of the area
    area = boundRect.width * boundRect.height;

    // if area is less than MIN_OBJECT_AREA, the rectangle is probably a noise
    // if area is larger than MAX_OBJECT_AREA, size of rectangle is larger than 2/3 of screen = filter fail or approching cyclist is too near
    if (area > MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea){
        refArea = area;
        obj = &boundRect;
    }
}

// if rectangle exists, estimate the distance to the object and send warnings to the helmet
if (obj != NULL){

    //calculation of distance to object
    double currentDistance = (objRealWidth * focal_length) / obj->width;

    // focal length = current measured width of A4 Paper in pixel * distance to camera(measured) / real width of A4 (measured)
    focal_length = (measuredPixelWidth * distanceToCam) / objRealWidth;

    // printf("width: %d \n", obj->width);

    // convert from cm to m
    currentDistance /= 100;

    sprintf_s(Distance, "Distance :%f m", currentDistance);
    putText(*imgOriginal, Distance, Point(0, 50), 1, 1, Scalar(0, 255, 0), 2);
    rectangle(*imgOriginal, obj->tl(), obj->br(), cv::Scalar(0, 0, 255), 2, 8, 0);

    switch (modus){
        case 0: // NOISE
            arduino->WriteLine("1");
            objDetected = '1';
            break;
        case 1: // VIBRATION
            arduino->WriteLine("2");
            objDetected = '2';
            break;
        case 2: // LED
            arduino->WriteLine("3");
            objDetected = '3';
            break;
        case 3:
            //measurement without alerting subject
            arduino->WriteLine("4");
            objDetected = '4'; 
            break;
        default:
            break;
    }

}
else{
    // no object recognized 
    arduino->WriteLine("0");
    objDetected = '0';
}

}

在我的这部分功能之后,我只在图片上画一个矩形,它由代码片段最后一行中的变量obj引用。 编辑:全局变量'objDetected'用于通过命名管道进行通信:

for (;;){
        // This call blocks until a client process reads all the data
        //const wchar_t *data = L"*** Hello Pipe ONUR ***";
        char *data = &objDetected;
        DWORD numBytesWritten = 0;
        result = WriteFile(
            pipe, // handle to our outbound pipe
            data, // data to send
            1, // length of data to send (bytes) + terminating !
            &numBytesWritten, // will store actual amount of data sent
            NULL // not using overlapped IO
            );

我真的不知道如何解决这个问题,因为我尝试了我能想到的一切。例如:我在计算过程中删除了所有未使用的向量条目,但它没有用,所以我从代码中删除了它)

如果有任何问题请随时提出并提前感谢您!

问候, 沮丧的OpenCV新手!

0 个答案:

没有答案