[opencv]目标追踪之前景建模(高斯混合模型)

这篇博客记录了利用高斯混合模型来进行目标追踪的学习过程。

有关高斯混合模型的原理请见 http://blog.csdn.net/smuevian/article/details/70169811

一、算法原理及过程

这部分内容摘自论文《基于高斯背景模型的运动目标检测与跟踪 》,为简便起见,我直接贴原文的图,方便以后查询:

[opencv]目标追踪之前景建模(高斯混合模型)_第1张图片

[opencv]目标追踪之前景建模(高斯混合模型)_第2张图片

[opencv]目标追踪之前景建模(高斯混合模型)_第3张图片

[opencv]目标追踪之前景建模(高斯混合模型)_第4张图片

[opencv]目标追踪之前景建模(高斯混合模型)_第5张图片

算法的流程图如下:

[opencv]目标追踪之前景建模(高斯混合模型)_第6张图片

二、Matlab程序实现:

clc,clear;
filename='E:\课题\背景建模\code\1\1.mp4';
source = VideoReader(filename);
t=cputime;
% -----------------------  frame size variables -----------------------
fr = read(source,4);            % read in 1st frame as background frame
fr_bw = rgb2gray(fr);           % convert background to greyscale
fr_size = size(fr);             % size of the frame!
width = fr_size(2);
height = fr_size(1);
fg = zeros(height, width);      %fg is the matrix of foreground 
bg_bw = zeros(height, width);   %bg_bw
% --------------------- MOG variables -----------------------------------
C = 1;                                  % number of gaussian components (typically 3-5)
M = 1;                                  % number of background components
D = 2.5;                                % positive deviation threshold
alpha = 0.01;                           % learning rate (between 0 and 1) (here is 0.01)
thresh = 0.45;                          % foreground threshold (0.25 or 0.75 in general)
sd_init = 6;                            % initial standard deviation(初始化标准方差) (for new components) var = 36 here
w = zeros(height,width,C);              % initialize weights array
mean = zeros(height,width,C);           % pixel means
sd = zeros(height,width,C);             % pixel standard deviations(标准方差)
u_diff = zeros(height,width,C);         % difference of each pixel from mean
p = alpha/(1/C);                        % initial p variable (used to update mean and sd)
rank = zeros(1,C);                      % rank of components (w/sd)
% --------------------- initialize component means and weights -----------
pixel_depth = 8;                        % 8-bit resolution
pixel_range = 2^pixel_depth -1;         % pixel range (# of possible values)
% 对每个像素点,定义K个高斯模型来表征图像中的各个像素点的特征
for i=1:height
    for j=1:width
        for k=1:C
            
            mean(i,j,k) = rand*pixel_range;     % means random (0-255)
            w(i,j,k) = 1/C;                     % weights uniformly dist
            sd(i,j,k) = sd_init;                % initialize to sd_init
            
        end
    end
end
%--------------------- process frames -----------------------------------
LengthFrame_source=source.Duration*source.FrameRate-4;
for n = 4:int8(LengthFrame_source)

    fr = read(source,n);        % read in frame
    fr_bw = rgb2gray(fr);       % convert frame to grayscale,转换成灰度图像    

    % calculate difference of pixel values from mean
    for m=1:C
        u_diff(:,:,m) = abs(double(fr_bw) - double(mean(:,:,m)));
    end
     
    % update gaussian components for each pixel,参数更新
    for i=1:height
        for j=1:width
            
            match = 0;
            for k=1:C                       
                if (abs(u_diff(i,j,k)) <= D*sd(i,j,k))       % pixel matches component
                    
                    match = 1;                          % variable to signal component match
                    
                    % update weights, mean, sd, p
                    w(i,j,k) = (1-alpha)*w(i,j,k) + alpha;
                    p = alpha/w(i,j,k);                  
                    mean(i,j,k) = (1-p)*mean(i,j,k) + p*double(fr_bw(i,j));
                    sd(i,j,k) =   sqrt((1-p)*(sd(i,j,k)^2) + p*((double(fr_bw(i,j)) - mean(i,j,k)))^2);
                else                                    % pixel doesn't match component
                    w(i,j,k) = (1-alpha)*w(i,j,k);      % weight slighly decreases
                    
                end
            end
            
            w(i,j,:) = w(i,j,:)./sum(w(i,j,:));        %normalize the weight w(i,j,k),so [sum of w(i,j,k)=1],对权重进行归一化
            
            bg_bw(i,j)=0;                              %bg_bw的含义是什么
            for k=1:C
                bg_bw(i,j) = bg_bw(i,j)+ mean(i,j,k)*w(i,j,k);
            end
            
            % if no components match, create new component,替换权重最小的高斯分布
            if (match == 0)
                [min_w, min_w_index] = min(w(i,j,:));  
                mean(i,j,min_w_index) = double(fr_bw(i,j));
                sd(i,j,min_w_index) = sd_init;
            end

            rank = w(i,j,:)./sd(i,j,:);             % calculate component rank
            rank_ind = [1:1:C];
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            % sort rank values,高斯分布由大到小排列
            for k=2:C               
                for m=1:(k-1)
                    
                    if (rank(:,:,k) > rank(:,:,m))                     
                        % swap max values
                        rank_temp = rank(:,:,m);  
                        rank(:,:,m) = rank(:,:,k);
                        rank(:,:,k) = rank_temp;
                        
                        % swap max index values
                        rank_ind_temp = rank_ind(m);  
                        rank_ind(m) = rank_ind(k);
                        rank_ind(k) = rank_ind_temp;    

                    end
                end
            end
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         %may be it can be cope like this:
         %[rank,rank_ind]=sort(rank);
         %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
           
         % calculate foreground,计算前景
            match = 0;
            k=1;
            
            fg(i,j) = 0;
            fg2(i,j) = 0;
            while ((match == 0)&&(k<=M))                      % M=3:number of background components

                if (w(i,j,rank_ind(k)) >= thresh)
                    if (abs(u_diff(i,j,rank_ind(k))) <= D*sd(i,j,rank_ind(k)))
                        fg(i,j) = 0;
                        fg2(i,j)=0;
                        match = 1;
                    else
                       %fg(i,j) = fr_bw(i,j);  
                       %fg(i,j) = fr(i,j); 
                       fg(i,j) = 1;
                       fg2(i,j)=255;
                    end
                end
                k = k+1;
            end
        end
    end
   % se=strel('line',5,0);
%     se=strel('square',3);
%     
% fg2=imdilate(fg2,se);
% 对图像进行形态学处理
se=strel('rectangle',[2 2]); 
fg2=bwmorph(fg2,'skel',Inf);
fg2=imfill(fg2,'holes');
fg2=bwareaopen(fg2,5);
fg2=imdilate(fg2,se);
fg2=bwareaopen(fg2,1);
fg2 = bwareaopen(fg2,500);
figure(1);imshow(fg2);   
%fr是读取视频的每帧图像
%fg,skin是一个大小为[height,width,C]的矩阵,若像素点为前景,则对应的点为1 
skin=fg;
fg(:,:,1)= double(fr(:,:,1)).*skin(:,:,1); 
fg(:,:,2)= double(fr(:,:,2)).*skin(:,:,1);  
fg(:,:,3)= double(fr(:,:,3)).*skin(:,:,1); 
figure(2),imshow(fr)
figure(3),imshow(uint8(bg_bw))
figure(4),imshow(uint8(fg))


figure(4),subplot(2,2,1),imshow(fr)
subplot(2,2,2),imshow(uint8(bg_bw))
subplot(2,2,3),imshow(uint8(fg)) 
subplot(2,2,4),imshow(uint8(fg2)) 
Mov1(n)  = im2frame(uint8(fg),gray);              % put frames into movie
% Mov2(n)  = im2frame(uint8(bg_bw),gray);           % put frames into movie
Mov3(n)  = im2frame(uint8(fg2),gray);             % put frames into movie
end
movie2avi(Mov1,'mixture_of_gaussians_output','fps',15);               % save movie as avi 
movie2avi(Mov2,'mixture_of_gaussians_background','fps',15);           % save movie as avi 
movie2avi(Mov3,'mixture_of_gaussians_out','fps',15);
figure;imshow(uint8(bg_bw));
figure;imshow(uint8(fg)) ;
time=cputime-t;
fprintf('%f',time);
上述代码与流程图的步骤稍有不符,主要是在k


三、利用opencv实现该模型

#include
#include
#include
using namespace std;
using namespace cv;
int main() {
	//视频路径
	string FilePath = "E:/study/book/opencv/opencv-2-cookbook-src-master/images/bike.avi";
	//读取视频
	VideoCapture capture(FilePath);
	if (!capture.isOpened()) {
		cout << "Can't open the video!" << endl;
		return  1;
	}
	//读取帧率
	double rate = capture.get(CV_CAP_PROP_FPS);
	int delay = 1000 / rate;

	Mat frame;
	Mat foreground;
	Mat mask;
	namedWindow("Extracted Foreground");
	Ptrbgsubstractor = createBackgroundSubtractorMOG2();
	bgsubstractor->setVarThreshold(20);
	bool stop(false);
	while (!stop) {
		if (!capture.read(frame))
		{
			break;
		}
		bgsubstractor->apply(frame, mask, 0.01);
		imshow("mask", mask);
		if (waitKey(delay) > 0)
			stop = true;
	}
	capture.release();
	waitKey();
	return 0;

}


四、参考博客

http://www.cnblogs.com/tornadomeet/archive/2012/06/02/2531565.html

利用opencv底层函数来实现这个模型的源代码:

http://blog.csdn.net/zouxy09/article/details/9622401,要注意二楼的回复,按照他的方法,代码可以运行成功,但效果不是很好。


你可能感兴趣的:(C++,机器学习)