資源簡介
幀差法實現(xiàn)前景背景分離,來自于BGSLibrary,對于靜態(tài)的背景效果很好!
代碼片段和文件信息
#include?
#include?
using?namespace?std;
using?namespace?cv;
//our?sensitivity?value?to?be?used?in?the?absdiff()?function
const?static?int?SENSITIVITY_VALUE?=?20;
//size?of?blur?used?to?smooth?the?intensity?image?output?from?absdiff()?function
const?static?int?BLUR_SIZE?=?10;
//we‘ll?have?just?one?object?to?search?for
//and?keep?track?of?its?position.
int?theobject[2]?=?{00};
//bounding?rectangle?of?the?object?we?will?use?the?center?of?this?as?its?position.
Rect?objectBoundingRectangle?=?Rect(0000);
//int?to?string?helper?function
string?intToString(int?number){
//this?function?has?a?number?input?and?string?output
std::stringstream?ss;
ss?< return?ss.str();
}
int?main(){
//these?two?can?be?toggled?by?pressing?‘d‘?or?‘t‘
bool?debugMode?=?true;
//pause?and?resume?code
bool?pause?=?false;
//set?up?the?matrices?that?we?will?need
//the?two?frames?we?will?be?comparing
Mat?frame1frame2;
//their?grayscale?images?(needed?for?absdiff()?function)
Mat?grayImage1grayImage2;
//resulting?difference?image
Mat?differenceImage;
//thresholded?difference?image?(for?use?in?findContours()?function)
Mat?thresholdImage;
//video?capture?object.
VideoCapture?capture;
while(1){
//we?can?loop?the?video?by?re-opening?the?capture?every?time?the?video?reaches?its?last?frame
capture.open(“video.avi“);
if(!capture.isOpened()){
cout<<“ERROR?ACQUIRING?VIDEO?FEED\n“;
getchar();
return?-1;
}
//check?if?the?video?has?reach?its?last?frame.
//we?add?‘-1‘?because?we?are?reading?two?frames?from?the?video?at?a?time.
//if?this?is?not?included?we?get?a?memory?error!
while(capture.get(CV_CAP_PROP_POS_frameS)ame_COUNT)-1){
//read?first?frame
capture.read(frame1);
//convert?frame1?to?gray?scale?for?frame?differencing
cv::cvtColor(frame1grayImage1COLOR_BGR2GRAY);
//copy?second?frame
capture.read(frame2);
//convert?frame2?to?gray?scale?for?frame?differencing
cv::cvtColor(frame2grayImage2COLOR_BGR2GRAY);
//perform?frame?differencing?with?the?sequential?images.?This?will?output?an?“intensity?image“
//do?not?confuse?this?with?a?threshold?image?we?will?need?to?perform?thresholding?afterwards.
cv::absdiff(grayImage1grayImage2differenceImage);
//threshold?intensity?image?at?a?given?sensitivity?value
cv::threshold(differenceImagethresholdImageSENSITIVITY_VALUE255THRESH_BINARY);
if(debugMode==true){
//show?the?difference?image?and?threshold?image
cv::imshow(“Difference?Image“differenceImage);
cv::imshow(“Threshold?Image“?thresholdImage);
}else{
//if?not?in?debug?mode?destroy?the?windows?so?we?don‘t?see?them?anymore
cv::destroyWindow(“Difference?Image“);
cv::destroyWindow(“Threshold?Image“);
}
//blur?the?image?to?get?rid?of?the?noise.?This?will?output?an?intensity?image
cv::blur(thresholdImagethresho
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????目錄???????????0??2014-10-03?21:05??fr
?????目錄???????????0??2014-10-03?20:57??fr
?????目錄???????????0??2014-10-03?20:56??fr
?????文件????39845888??2014-10-03?21:05??fr
?????文件?????????921??2014-10-03?20:52??fr
?????文件???????20480??2014-10-03?21:05??fr
?????目錄???????????0??2014-10-03?20:57??fr
?????文件?????????762??2014-10-03?20:57??fr
?????文件???????18994??2014-10-03?20:57??fr
?????文件?????????374??2014-10-03?20:57??fr
?????文件????????2072??2014-10-03?20:57??fr
?????文件??????????93??2014-10-03?20:57??fr
?????文件????????1252??2014-10-03?20:57??fr
?????文件???????????0??2014-10-03?20:57??fr
?????文件??????363520??2014-10-03?20:57??fr
?????文件??????561152??2014-10-03?20:57??fr
?????文件????????4018??2014-10-03?20:54??fr
?????文件?????????942??2014-10-03?20:54??fr
?????文件????????3967??2014-10-03?20:58??fr
?????文件?????1049784??2012-02-02?09:30??fr
評論
共有 條評論