• 大小: 12.69MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-07-08
  • 语言: 其他
  • 标签: opencv  帧差法  

资源简介

帧差法实现前景背景分离,来自于BGSLibrary,对于静态的背景效果很好!

资源截图

代码片段和文件信息

#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 << number;
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  frame_Difference_1\
     目录           0  2014-10-03 20:57  frame_Difference_1\Debug\
     目录           0  2014-10-03 20:56  frame_Difference_1\frame_Difference_1\
     文件    39845888  2014-10-03 21:05  frame_Difference_1\frame_Difference_1.sdf
     文件         921  2014-10-03 20:52  frame_Difference_1\frame_Difference_1.sln
     文件       20480  2014-10-03 21:05  frame_Difference_1\frame_Difference_1.v11.suo
     目录           0  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\
     文件         762  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\cl.command.1.tlog
     文件       18994  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\CL.read.1.tlog
     文件         374  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\CL.write.1.tlog
     文件        2072  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\frame_Difference_1.Build.CppClean.log
     文件          93  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\frame_Difference_1.lastbuildstate
     文件        1252  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\frame_Difference_1.log
     文件           0  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\frame_Difference_1.unsuccessfulbuild
     文件      363520  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\vc110.idb
     文件      561152  2014-10-03 20:57  frame_Difference_1\frame_Difference_1\Debug\vc110.pdb
     文件        4018  2014-10-03 20:54  frame_Difference_1\frame_Difference_1\frame_Difference_1.vcxproj
     文件         942  2014-10-03 20:54  frame_Difference_1\frame_Difference_1\frame_Difference_1.vcxproj.filters
     文件        3967  2014-10-03 20:58  frame_Difference_1\frame_Difference_1\main.cpp
     文件     1049784  2012-02-02 09:30  frame_Difference_1\frame_Difference_1\video.avi

评论

共有 条评论