OpenCV實現運動目標檢測的函式

2021-06-14 05:00:46 字數 1708 閱讀 2791

// 引數:

// dst – 檢測結果

void update_mhi( iplimage* img, iplimage* dst, int diff_threshold )

for( i = 0; i < n; i++ )

cvreleaseimage( &mhi );

mhi = cvcreateimage( size, ipl_depth_32f, 1 );

cvzero( mhi ); // clear mhi at the beginning

} // end of if(mhi)

cvcvtcolor( img, buf[last], cv_bgr2gray ); // convert frame to grayscale

idx1 = last;

idx2 = (last + 1) % n; // index of (last - (n-1))th frame

last = idx2;

// 做幀差

silh = buf[idx2];

cvabsdiff( buf[idx1], buf[idx2], silh ); // get difference between frames

// 對差影象做二值化

cvthreshold( silh, silh, 30, 255, cv_thresh_binary ); // and threshold it

cvupdatemotionhistory( silh, mhi, timestamp, mhi_duration ); // update mhi

cvcvtscale( mhi, dst, 255./mhi_duration,

(mhi_duration - timestamp)*255./mhi_duration );    

cvcvtscale( mhi, dst, 255./mhi_duration, 0 );    

// 中值濾波,消除小的雜訊

cvsmooth( dst, dst, cv_median, 3, 0, 0, 0 );

// 向下取樣,去掉雜訊

cvpyrdown( dst, pyr, 7 );

cvdilate( pyr, pyr, 0, 1 ); // 做膨脹操作,消除目標的不連續空洞

cvpyrup( pyr, dst, 7 );

//// 下面的程式段用來找到輪廓

//// create dynamic structure and sequence.

stor = cvcreatememstorage(0);

cont = cvcreateseq(cv_seq_eltype_point, sizeof(cvseq), sizeof(cvpoint) , stor);

// 找到所有輪廓

cvfindcontours( dst, stor, &cont, sizeof(cvcontour),

/*for(;cont;cont = cont->h_next)

// end of for-loop: "cont"

*/// 直接使用contour中的矩形來畫輪廓

for(;cont;cont = cont->h_next)

}// free memory

cvreleasememstorage(&stor);

cvreleaseimage( &pyr );

}

OpenCV實現幀差法檢測運動目標

今天的目標是用opencv實現對運動目標的檢測,這裡選用三幀幀差法。如下 include include include include include double threshold index 0 const int contour max aera 200 void trackbar int...

運動物體目標檢測實現 基於OpenCV

基於opencv實現的乙個運動目標檢測演算法,可以用來檢測行人,車輛等運動目標。include include include include int main int argc,char argv 宣告iplimage指標 iplimage pframe null iplimage pfrimg ...

運動目標檢測OpenCV背景減除法

背景減除法 1.gmm mog2演算法,高斯混合模型分離演算法,它為每個畫素選擇適當數量的高斯分布 函式 cv2.createbackgroundsubtractormog2 int history 500,double varthread 16,bool detectshadows true 2....