背景建模,利用平均帧作为背景
%%Obtain the background image
imageDataSet=[];
subfolders = {'vidf1_33_000.y', 'vidf1_33_001.y', 'vidf1_33_002.y', 'vidf1_33_003.y', 'vidf1_33_004.y','vidf1_33_005.y', 'vidf1_33_006.y', 'vidf1_33_007.y', 'vidf1_33_008.y', 'vidf1_33_009.y'};
for i=1:length(subfolders)
for j=1:200
disp(['Processing image #' num2str(j) ' (out of 200)...']);
im = imread(['data/',subfolders{i},'/vidf1_33_', num2str(i-1, '%03d'),'_f',num2str(j, '%03d'),'.png']);
im=im(:);
imageDataSet=[imageDataSet,im];
disp('----------');
end
end
AveImg=sum(imageDataSet');
BacGImg=AveImg/2000;
figure;
imshow(reshape(BacGImg,158,238)/256);
foreGround=double(imageDataSet)-repmat(BacGImg',1,2000);
foreGround=abs(foreGround(3:end,3:end));
save('foreGround.mat','foreGround');
int main(){
Mat frame,mask,thresholdImage, output;
BackgroundSubtractorMOG2 bgSubtractor(20,16,true);
char image_name[35];//保存的图片名
IplImage * frame_std;
/*frame_std=cvLoadImage("D:\\vidf1_33_000_f001.png");
IplImage *dst = cvCreateImage(cvSize(frame.cols,frame.rows),frame_std->depth,frame_std->nChannels);
IplImage *img1 = cvCreateImage(cvGetSize(dst),IPL_DEPTH_8U,1);*/
//cvSaveImage("D:\\saveImage.jpg",dst);
IplImage * im_median_filter = cvCreateImage(cvSize(frame.cols,frame.rows), IPL_DEPTH_8U, 1);
for(int i=0;i<10;i++){
for(int j=1;j<201;j++){
sprintf(image_name,"data/vidf1_33_%03d_f%03d.png",i,j);
IplImage * im_input=cvLoadImage(image_name);
cv::Mat frame(im_input,0);
bgSubtractor(frame,mask,0.001);
//imshow("foreGround",mask);
IplImage * im_mask_gray=&(IplImage)mask;
//cvSmooth(im_mask_gray, im_mask_gray, CV_MEDIAN);
sprintf(image_name,"Foredata/vidf1_33_%03d_f%03d.png",i,j);
cvSaveImage(image_name,im_mask_gray);//保存目标图像
//cvCvtColor(dst,img1,CV_BGR2GRAY);
//cvShowImage("Example3",im_mask_gray);
}
}
}