当前位置:   article > 正文

视频人数统计(opencv)_视频统计人数页面

视频统计人数页面

步骤:
1.视频图像灰度化img
2,选取第一帧图像first_img,视频每帧和第一帧相减,得到src
3,对src图片进行 阈值,滤波处理
4,查找处理好图片的边界findContours;
5,对边界区域进行统计,满足条件的进行计数

代码实 现:

using namespace std;
using namespace cv;


int main(){
    Mat img, src, frame, frame_gray, first_frame, threshold_src, gass_src, dilate_src;

    VideoCapture cap("m.avi");
    cap >> first_frame;
    cvtColor(first_frame, first_frame, CV_BGR2GRAY);
    int num=0;
    int zz = 1;
    while (1){
        cap >> frame;
        if (frame.empty())
            break;
        vector<Vec4i> hierarchy;
        vector<vector<Point> > contour;
        cvtColor(frame, frame_gray, CV_BGR2GRAY);
        absdiff(frame_gray,first_frame,src);
        threshold(src, threshold_src,50,255,THRESH_BINARY);
        GaussianBlur(threshold_src, gass_src,Size(5,5),1.5);
        //blur(threshold_src, gass_src, Size(5, 5));
    //  medianBlur(threshold_src, gass_src,1);

        dilate(gass_src, dilate_src,Mat());
        findContours(dilate_src,contour, CV_RETR_EXTERNAL,CV_CHAIN_APPROX_SIMPLE);
        //drawContours(frame, contour, -1, Scalar(0, 0, 255),2);
        for (int i = 0; i < contour.size();i++){
            Rect bndRect = boundingRect(contour.at(i));
            Point p1,p2;
            p1.x = bndRect.x;
            p1.y = bndRect.y;
            p2.x = bndRect.x + bndRect.width;
            p2.y = bndRect.y + bndRect.height;
            if (bndRect.area()>3000){
                rectangle(frame, p1, p2, Scalar(0, 0, 255));
                num++;
            }
        }
        string font = "Current number:";
        putText(frame,font+to_string(num),Point(100,50),FONT_HERSHEY_COMPLEX_SMALL,1,Scalar(0,0,255));
    //  cout << "人数统计:" << num<<endl;
        num = 0;
        imshow("dilate_src", dilate_src);
        imshow("frame", frame);
        imshow("first_frame", first_frame);
        waitKey(20);
    }
    return 0;
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52

效果显示:

这里写图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/948297
推荐阅读
相关标签
  

闽ICP备14008679号