c++ - 使用 OpenCV 检测视频中的基本变化

标签 c++ opencv motion-detection

尝试重新创建一个基本的变化检测程序,我从 Adrian Rosebrock 的一篇很棒的博客中获得(如果想了解 python 和 OpenCV,请点击这里)。该代码是用 python 设计的,我正在尝试将其转换为 C++。您可以找到博文 here .我的挣扎是 absdiff(firsFrame, gray, imageDifference) 因为循环的每次迭代都使 firstFrame 和 gray 相等。我认为问题出在我初始化 firstFrame = gray 的地方,但我做了一个 cout 检查以查看它被击中了多少次,所以不确定。这是代码:

int min_area = 500; //min area of motion detectable

//get camera operational and make sure working correctly
VideoCapture camera(0);
if(!camera.isOpened()){
    cout << "cannot open camera" << endl;
    return(1);
}

Mat firstFrame, gray, imageDifference, thresh;
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;


while(true){
    Mat frame;
    camera.read(frame);
    if(frame.empty()){
        cout << "frame was not captured" << endl;
        return(2);
    }
    //pre processing
    //resize(frame, frame, Size (1200,900));
    cvtColor(frame, gray, COLOR_BGR2GRAY);
    GaussianBlur(gray, gray, Size( 21, 21 ), 0, 0 );

    //initrialize first frame if necessary
    if(firstFrame.empty()){
        cout << "hit" << endl;
        firstFrame = gray;
        continue;
    }

    //get difference
    absdiff(firstFrame, gray, imageDifference);
    threshold(imageDifference, thresh, 25, 255, THRESH_BINARY);
    //fill in holes
    dilate(thresh, thresh, Mat(), Point(-1, -1), 2, 1, 1);
    findContours(thresh, contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE);

    //loop over contours
    for(int i = 0; i < contours.size(); i++){
        //get the boundboxes and save the ROI as an Image
        if (contourArea(contours[i]) < min_area){
            continue;
        }
        Rect boundRect = boundingRect( Mat(contours[i]));
        rectangle( frame, boundRect.tl(), boundRect.br(), (0,255,0), 1, 8, 0 );

    }
    //draw everything
    imshow("Security feed", frame);
    imshow("Thresh", thresh);
    imshow("Difference", imageDifference);
    if (waitKey(30) >= 0)
        break;
}

camera.release();
destroyAllWindows();
return(0);

最佳答案

一些有待改进的地方:

  1. firstFrame = gray => gray.copyTo(firstFrame)

    你定义firstFramegray在同一行,然后做 firstFrame = gray ,它们共享相同的数据存储器。所以每次它们都是一样的。

  2. 跳过一些帧。

    由于相机刚刚启动,所以第一帧不太稳定,你应该跳过一些帧(比如10帧)。

  3. cv::标量<==>元组

    在 C++ 中:

    cv::Scalar(b,g,r) <==> tuple(b,g,r)

    (b,g,r) ==> r


修改后的代码:

int cnt = 0;

while(true) {

    Mat frame;
    camera.read(frame);
    if(frame.empty()) {
        cout << "frame was not captured" << endl;
        return(2);
    }
    //pre processing
    //resize(frame, frame, Size (1200,900));
    cvtColor(frame, gray, COLOR_BGR2GRAY);
    GaussianBlur(gray, gray, Size( 21, 21 ), 0, 0 );

    //initrialize first frame if necessary
    if(firstFrame.empty()) {
        if(cnt<10){
            ++cnt;
        }else{
            cout << "hit" << endl;
            gray.copyTo(firstFrame);
        }
        continue;
    }

    // ...

    ///  cv::Scalar(b,g,r) <==> tuple(b,g,r)
    ///  (b,g,r) ==> r
    rectangle( frame, boundRect.tl(), boundRect.br(), Scalar(0,255,0), 1, 8, 0 );

    // ...


}

enter image description here

关于c++ - 使用 OpenCV 检测视频中的基本变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48190192/

相关文章:

android - 比较连续的帧以确定使用OpenCV的运动方向

vector 的 vector 的 C++ 迭代器

c++ - OpenCV计算时间检测特征

java - 整个屏幕的OnTouchListener

opencv - OpenCV getRectSubPix:为什么其公式使用(dst.cols-1)* 0.5?

image-processing - OpenCV - 找到最接近图像中心的 Blob

matlab - 计算空间共置的有效方法

c++ - 为什么 strlen (..) 不期望 const char* const str 而不是 const char*

c++ - 使用内联 x86 C++ 程序集将寄存器内容移动到变量时出现错误 C2403

python - 调用返回列表的 C++ 函数