帧差法——动态检测——统计车流量
生活随笔
收集整理的這篇文章主要介紹了
帧差法——动态检测——统计车流量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
幀差法——動態檢測——統計車流量
幀差法原理
幀間差分法是一種通過對視頻圖像序列的連續兩幀圖像做差分運算獲取運動目標輪廓的方法。當監控場景中出現異常目標運動時,相鄰兩幀圖像之間會出現較為明顯的差別,兩幀相減,求得圖像對應位置像素值差的絕對值,判斷其是否大于某一閾值,進而分析視頻或圖像序列的物體運動特性。
實現流程
實現效果
動態檢測函數代碼
Mat MoveDetect(Mat frame1, Mat frame2) {Mat result = frame2.clone();Mat gray1, gray2;cvtColor(frame1, gray1, CV_BGR2GRAY);cvtColor(frame2, gray2, CV_BGR2GRAY);Mat diff;absdiff(gray1, gray2, diff);//兩幅圖做差imshow("absdiss", diff);threshold(diff, diff, 25, 255, CV_THRESH_BINARY);//二值化diffMat element = getStructuringElement(MORPH_RECT, Size(3, 3));//返回指定形狀與尺寸得結構元素Mat element2 = getStructuringElement(MORPH_RECT, Size(19, 19));erode(diff, diff, element);//腐蝕函數medianBlur(diff, diff, 3);//中值濾波dilate(diff, diff, element2);//膨脹函數 vector<vector<Point>> contours;vector<Vec4i> hierarcy;findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(0, 0));//查找輪廓vector<vector<Point>>contours_poly(contours.size());vector<Rect> boundRect(contours.size()); //定義外接矩形集合 int x0 = 0, y0 = 0, w0 = 0, h0 = 0;for (int i = 0; i<contours.size(); i++){approxPolyDP(Mat(contours[i]), contours_poly[i], 3, true);//對圖像輪廓點進行多邊形擬合:輪廓點組成的點集,輸出的多邊形點集,精度(即兩個輪廓點之間的距離),輸出多邊形是否封閉boundRect[i] = boundingRect(Mat(contours_poly[i]));if (boundRect[i].width>90 && boundRect[i].width<180 && boundRect[i].height>90 && boundRect[i].height<180) {//輪廓篩選 (90 180)x0 = boundRect[i].x;y0 = boundRect[i].y;w0 = boundRect[i].width;h0 = boundRect[i].height;rectangle(result, Point(x0, y0), Point(x0 + w0, y0 + h0), Scalar(0, 255, 0), 2, 8, 0);if ((y0 + h0 / 2 + 1) >= 198 && (y0 + h0 / 2 - 1) <= 202) {//經過這條線(區間),車輛數量+1CarNum++;}}line(result, Point(0, 200), Point(720, 200), Scalar(0, 0, 255), 1, 8);//畫紅線Point org(0, 35);putText(result, "CarNum=" + intToString(CarNum), org, CV_FONT_HERSHEY_SIMPLEX, 0.8f, Scalar(0, 255, 0), 2);}return result; }完整代碼及實驗使用視頻請到資源處下載
鏈接: https://download.csdn.net/download/weixin_46958585/12708964.
總結
以上是生活随笔為你收集整理的帧差法——动态检测——统计车流量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 蓝桥杯官网 试题 PREV-278 历届
- 下一篇: unity3d脚本控制骨骼旋转