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 *******************************************************/
本文来自博客园,作者:LILi2209,转载请注明原文链接:https://www.cnblogs.com/Lili-202209/p/17535880.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构