/*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: [email protected] > Created Time: Fri 24 Apr 2020 09:46:00 CST > Modified Time:2020年07月01日 星期三 15时13分11秒 > Note: No *******************************************************/
标签:int,double,frame,opencv,maxRect,cv,RECT From: https://www.cnblogs.com/liliczw2209/p/17535880.html