#include
#include
#include
#include "opencv2/video/background_segm.hpp"
#include "opencv2/legacy/blobtrack.hpp"
#include "opencv2/legacy/legacy.hpp"
#include "opencv2/highgui/highgui.hpp"
#include
#include
#include
#include
#include
using namespace cv;
using namespace std;
int main()
//獲取整個幀數
long totalframenumber = capture.get(cv_cap_prop_frame_count);
//設定開始幀()
long frametostart = 20;
capture.set(cv_cap_prop_pos_frames, frametostart);
cout << "從第" << frametostart << "幀開始讀" << endl;
//設定結束幀
int frametostop = 700;
if (frametostop < frametostart)
else
double rate = capture.get(cv_cap_prop_fps);
int delay = 1000 / rate;
cv::backgroundsubtractormog2 mog(20,16,true);
cv::mat foreground;
cv::mat background;
cv::mat frame;
cv::mat frameb;
bool stop(false);
//currentframe是在迴圈體中控制讀取到指定的幀後迴圈結束的變數
long currentframe = frametostart;
while (!stop)
//gaussianblur(frame,frameb,size(5,5),0,0);
bilateralfilter(frame, frameb, 5, 20.0, 20.0);
//引數為:輸入影象、輸出影象、學習速率
mog(frameb, foreground, 0.01);
cout << currentframe << endl;
// 腐蝕
cv::erode(foreground, foreground, cv::mat());
cv::erode(foreground, foreground, cv::mat());
// 膨脹
cv::dilate(foreground, foreground, cv::mat());
cv::dilate(foreground, foreground, cv::mat());
/*// 腐蝕
cv::erode(foreground, foreground, cv::mat());
// 膨脹
cv::dilate(foreground, foreground, cv::mat());
cv::dilate(foreground, foreground, cv::mat());
*/mog.getbackgroundimage(background);
// 返回當前背景影象
cv::imshow("foreground", foreground);
cv::imshow("background", background);
if (currentframe == 150)
if (currentframe == 300)
if (currentframe == 450)
//按esc鍵退出,按其他鍵會停止在當前幀
int c = waitkey(delay);
if ((char)c == 27 || currentframe >= frametostop)
if (c >= 0)
currentframe++;
}waitkey(0);
OpenCV 物體運動檢測
安裝各種環境 學習背景分割 二值化 膨脹 腐蝕等操作。原始碼 usr bin env python 汽車運動檢測 p140 import cv2 import os import numpy as np cap cv2.videocapture 1 cap cv2.videocapture 0 調取...
python opencv3 運動檢測
git 思路 開啟攝像頭後 設定乙個當前幀為背景,在之後檢測到的幀都與背景對比不同,對不同的地方進行檢測 1 coding utf 823 4 計算幀之間的差異 考慮背景幀與其他幀之間的差異 5 67 import cv28 import numpy as np910 呼叫攝像頭 11 camera...
運動檢測ViBe演算法python實現
由於最近在做一些跟蹤檢查的研究,就用到了vibe演算法,根據網上的c 版本編寫了這個python版的演算法,在這分享給大家。class vibe classdocs defaultnbsamples 20 每個畫素點的樣本個數 defaultreqmatches 2 min指數 defaultrad...