opencv

复制代码
/*int main() {
    cv::VideoCapture cap(0);
    if (!cap.isOpened()) {
        cout << "video not exist!" << endl;
        return -1;
    }

   cv:: Mat frame, mask, maskCp;
   cv:: Mat cnimg;

    std::vector<std::vector<cv::Point>> cnts;
    cv::Rect maxRect;
    const double RECT_HW_RATIO = 1.25;    // 人体长宽比阈值
    const double RECT_AREA_RATIO = 0.08;    // 人体占整个图像最小比例阈值
    const double RECT_AREA_RATIO2 = 0.2;    // 人体占整体图像最大比例阈值
   cv:: Ptr<cv::BackgroundSubtractorMOG2> bgsubtractor = cv::createBackgroundSubtractorMOG2();
    bgsubtractor->setHistory(20);
    bgsubtractor->setVarThreshold(100);
    bgsubtractor->setDetectShadows(true);
    bool hasPeople = true;        // 是否有人
    int count = 0;    // 帧数
    int hasPeopleFrameCnt = 0; // 每K帧统计到的有人帧数
    int spaceFrames = 0;        // 每隔125帧统计一次
    const int SPACE_FRAME = 5;

    while (true) {
        cap >> frame;
        cap >> cnimg;
        cv_image<bgr_pixel>pim(cnimg);
        array2d<rgb_pixel>img;
        

        frontal_face_detector detector1 = get_frontal_face_detector();
        std::vector<dlib::rectangle> face = detector1(pim);
        

        resize(frame, frame, cv::Size(frame.cols , frame.rows));
        // 背景更新
        bgsubtractor->apply(frame, mask, 0.002);
        // 中值滤波
        medianBlur(mask, mask, 3);
        // 阈值分割,去阴影
        threshold(mask, mask, 200, 255, cv::THRESH_BINARY);
        // 找轮廓
        maskCp = mask.clone();
        findContours(maskCp, cnts, 0, cv::CHAIN_APPROX_SIMPLE);
        std::vector<cv::Point> maxCnt;
        for (int i = 0; i < cnts.size(); ++i) {
            maxCnt = maxCnt.size() > cnts[i].size() ? maxCnt : cnts[i];
        }
        // 画最大外接矩形
        if (maxCnt.size() > 0) {
            maxRect = boundingRect(maxCnt);
            double rectAreaRatio = (double)maxRect.area() / (frame.cols * frame.rows);
            if ((double)maxRect.height / maxRect.width > RECT_HW_RATIO && rectAreaRatio > RECT_AREA_RATIO &&
                rectAreaRatio < RECT_AREA_RATIO2) {
                cv::rectangle(frame, maxRect.tl(), maxRect.br(), cv::Scalar(0, 255, 0), 2);
                ++hasPeopleFrameCnt;
            }
        }
        ++spaceFrames;
        if (spaceFrames >= SPACE_FRAME) {
            if (hasPeopleFrameCnt > SPACE_FRAME / 8) {
                hasPeople = true;
                cout << count << ":有人" << endl;
            }
            else {
                hasPeople = false;
                cout << count << ":无人" << endl;
            }
            hasPeopleFrameCnt = 0;
            spaceFrames = 0;
        }

       // imshow("frame识别", frame);
        imshow("te",cnimg);

        // imshow("test", gh);

        if (cv::waitKey(10) == 29) {
            break;
        }
    }
    return 0;
};*/
/*******************************************************
 > File Name: main.cpp
 > Author: admin
 > Mail: ffffffan@foxmail.com
 > Created Time: Fri 24 Apr 2020 09:46:00 CST
 > Modified Time:2020年07月01日 星期三 15时13分11秒
 > Note: No
*******************************************************/
复制代码

 

posted @   LILi2209  阅读(14)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示