1.软件版本
MATLAB2013b
2.本算法理论知识
3.部分核心代码
function varargout = tops(varargin)
% TOPS MATLAB code for tops.fig
% TOPS, by itself, creates a new TOPS or raises the existing
% singleton*.
%
% H = TOPS returns the handle to a new TOPS or the handle to
% the existing singleton*.
%
% TOPS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in TOPS.M with the given input arguments.
%
% TOPS('Property','Value',...) creates a new TOPS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before tops_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to tops_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help tops
% Last Modified by GUIDE v2.5 19-Mar-2014 02:06:17
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @tops_OpeningFcn, ...
'gui_OutputFcn', @tops_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before tops is made visible.
function tops_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to tops (see VARARGIN)
% Choose default command line output for tops
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes tops wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = tops_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
parameter;
FileName_AVI = 'a.avi';
[pixel_gray,pixel_original,frameNum_Original] = func_vedio_process(FileName_AVI);
axes(handles.axes1)
for tt = 1:frameNum_Original
imshow(pixel_original(:,:,:,tt));
drawnow;
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
parameter;
FileName_AVI = 'Vedio\b.avi';
[pixel_gray,pixel_original,frameNum_Original] = func_vedio_process(FileName_AVI);
axes(handles.axes1)
for tt = 1:frameNum_Original
imshow(pixel_original(:,:,:,tt));
drawnow;
end
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
parameter;
Time1 = 0;
tic;
for tt = 1:round(frameNum_Original)
disp('当前帧数');
tt
image = Temp(:,:,tt);
for kk = 1:K
Datac = double(Temp(:,:,tt))-reshape(Mus(:,kk,:),D,CRGB);
Squared(:,kk) = sum((Datac.^ 2)./reshape(Sigmas(:,kk,:),D,CRGB),2);
end
[junk,index] = min(Squared,[],2);
Gaussian = zeros(size(Squared));
Gaussian(sub2ind(size(Squared),1:length(index),index')) = ones(D,1);
Gaussian = Gaussian&(Squared<Deviation_sq);
%参数更新
Weights = (1-Alpha).*Weights+Alpha.*Gaussian;
for kk = 1:K
pixel_matched = repmat(Gaussian(:,kk),1,CRGB);
pixel_unmatched = abs(pixel_matched-1);
Mu_kk = reshape(Mus(:,kk,:),D,CRGB);
Sigma_kk = reshape(Sigmas(:,kk,:),D,CRGB);
Mus(:,kk,:) = pixel_unmatched.*Mu_kk+pixel_matched.*(((1-Rho).*Mu_kk)+(Rho.*double(image)));
Mu_kk = reshape(Mus(:,kk,:),D,CRGB);
Sigmas(:,kk,:) = pixel_unmatched.*Sigma_kk+pixel_matched.*(((1-Rho).*Sigma_kk)+repmat((Rho.* sum((double(image)-Mu_kk).^2,2)),1,CRGB));
end
replaced_gaussian = zeros(D,K);
mismatched = find(sum(Gaussian,2)==0);
for ii = 1:length(mismatched)
[junk,index] = min(Weights(mismatched(ii),:)./sqrt(Sigmas(mismatched(ii),:,1)));
replaced_gaussian(mismatched(ii),index) = 1;
Mus(mismatched(ii),index,:) = image(mismatched(ii),:);
Sigmas(mismatched(ii),index,:) = ones(1,CRGB)*Variance;
Weights(mismatched(ii),index) = Props;
end
Weights = Weights./repmat(sum(Weights,2),1,K);
active_gaussian = Gaussian+replaced_gaussian;
%背景分割
[junk,index] = sort(Weights./sqrt(Sigmas(:,:,1)),2,'descend');
bg_gauss_good = index(:,1);
linear_index = (index-1)*D+repmat([1:D]',1,K);
weights_ordered = Weights(linear_index);
for kk = 1:K
Weight(:,kk)= sum(weights_ordered(:,1:kk),2);
end
bg_gauss(:,2:K) = Weight(:,1:(K-1)) < Back_Thresh;
bg_gauss(:,1) = 1;
bg_gauss(linear_index) = bg_gauss;
active_background_gaussian = active_gaussian & bg_gauss;
foreground_pixels = abs(sum(active_background_gaussian,2)-1);
foreground_map = reshape(sum(foreground_pixels,2),RR,CC);
Images1(:,:,tt) = foreground_map;
objects_map = zeros(size(foreground_map),'int32');
object_sizes = [];
Obj_pos = [];
new_label = 1;
%计算连通区域
[label_map,num_labels] = bwlabel(foreground_map,8);
for label = 1:num_labels
object = (label_map == label);
object_size = sum(sum(object));
if(object_size >= Comp_Thresh)
objects_map = objects_map + int32(object * new_label);
object_sizes(new_label) = object_size;
[X,Y] = meshgrid(1:CC,1:RR);
object_x = X.*object;
object_y = Y.*object;
Obj_pos(:,new_label) = [sum(sum(object_x)) / object_size;
sum(sum(object_y)) / object_size];
new_label = new_label + 1;
end
end
num_objects = new_label - 1;
%去除阴影
index = sub2ind(size(Mus),reshape(repmat([1:D],CRGB,1),D*CRGB,1),reshape(repmat(bg_gauss_good',CRGB,1),D*CRGB,1),repmat([1:CRGB]',D,1));
background = reshape(Mus(index),CRGB,D);
background = reshape(background',RR,CC,CRGB);
background = uint8(background);
background_Update(:,:,:,tt) = background;
background_hsv = rgb2hsv(background);
image_hsv = rgb2hsv(image_sequence(:,:,:,tt));
for i = 1:RR
for j = 1:CC
if (objects_map(i,j))&&...
(abs(image_hsv(i,j,1)-background_hsv(i,j,1))<SHADOWS(1))&&...
(image_hsv(i,j,2)-background_hsv(i,j,2)<SHADOWS(2))&&...
(SHADOWS(3)<=image_hsv(i,j,3)/background_hsv(i,j,3)<=SHADOWS(4))
Shadows(i,j) = 1;
else
Shadows(i,j) = 0;
end
end
end
Images0(:,:,tt) = objects_map;
objecs_adjust_map = Shadows;
Images2(:,:,tt) = objecs_adjust_map;
Images3(:,:,tt)= Images2(:,:,tt);
end
Time1 = toc/frameNum_Original;
msgbox(['视频每帧耗时:',num2str(Time1),'s']);
axes(handles.axes2)
imshow(uint8(background_Update(:,:,:,end)));
drawnow;
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
parameter;
Time2 = 0;
tic;
for tt = 1:frameNum_Original
disp('当前帧数');
tt
image = Temp(:,:,tt);
for kk = 1:K
Datac = double(Temp(:,:,tt))-reshape(Mus(:,kk,:),D,CRGB);
Squared(:,kk) = sum((Datac.^ 2)./reshape(Sigmas(:,kk,:),D,CRGB),2);
end
[junk,index] = min(Squared,[],2);
Gaussian = zeros(size(Squared));
Gaussian(sub2ind(size(Squared),1:length(index),index')) = ones(D,1);
Gaussian = Gaussian&(Squared<Deviation_sq);
%参数更新
Weights = (1-Alpha).*Weights+Alpha.*Gaussian;
for kk = 1:K
pixel_matched = repmat(Gaussian(:,kk),1,CRGB);
pixel_unmatched = abs(pixel_matched-1);
Mu_kk = reshape(Mus(:,kk,:),D,CRGB);
Sigma_kk = reshape(Sigmas(:,kk,:),D,CRGB);
Mus(:,kk,:) = pixel_unmatched.*Mu_kk+pixel_matched.*(((1-Rho).*Mu_kk)+(Rho.*double(image)));
Mu_kk = reshape(Mus(:,kk,:),D,CRGB);
Sigmas(:,kk,:) = pixel_unmatched.*Sigma_kk+pixel_matched.*(((1-Rho).*Sigma_kk)+repmat((Rho.* sum((double(image)-Mu_kk).^2,2)),1,CRGB));
end
replaced_gaussian = zeros(D,K);
mismatched = find(sum(Gaussian,2)==0);
for ii = 1:length(mismatched)
[junk,index] = min(Weights(mismatched(ii),:)./sqrt(Sigmas(mismatched(ii),:,1)));
replaced_gaussian(mismatched(ii),index) = 1;
Mus(mismatched(ii),index,:) = image(mismatched(ii),:);
Sigmas(mismatched(ii),index,:) = ones(1,CRGB)*Variance;
Weights(mismatched(ii),index) = Props;
end
Weights = Weights./repmat(sum(Weights,2),1,K);
active_gaussian = Gaussian+replaced_gaussian;
%背景分割
[junk,index] = sort(Weights./sqrt(Sigmas(:,:,1)),2,'descend');
bg_gauss_good = index(:,1);
linear_index = (index-1)*D+repmat([1:D]',1,K);
weights_ordered = Weights(linear_index);
for kk = 1:K
Weight(:,kk)= sum(weights_ordered(:,1:kk),2);
end
bg_gauss(:,2:K) = Weight(:,1:(K-1)) < Back_Thresh;
bg_gauss(:,1) = 1;
bg_gauss(linear_index) = bg_gauss;
active_background_gaussian = active_gaussian & bg_gauss;
foreground_pixels = abs(sum(active_background_gaussian,2)-1);
foreground_map = reshape(sum(foreground_pixels,2),RR,CC);
Images1(:,:,tt) = foreground_map;
objects_map = zeros(size(foreground_map),'int32');
object_sizes = [];
Obj_pos = [];
new_label = 1;
%计算连通区域
[label_map,num_labels] = bwlabel(foreground_map,8);
for label = 1:num_labels
object = (label_map == label);
object_size = sum(sum(object));
if(object_size >= Comp_Thresh)
objects_map = objects_map + int32(object * new_label);
object_sizes(new_label) = object_size;
[X,Y] = meshgrid(1:CC,1:RR);
object_x = X.*object;
object_y = Y.*object;
Obj_pos(:,new_label) = [sum(sum(object_x)) / object_size;
sum(sum(object_y)) / object_size];
new_label = new_label + 1;
end
end
num_objects = new_label - 1;
%去除阴影
index = sub2ind(size(Mus),reshape(repmat([1:D],CRGB,1),D*CRGB,1),reshape(repmat(bg_gauss_good',CRGB,1),D*CRGB,1),repmat([1:CRGB]',D,1));
background = reshape(Mus(index),CRGB,D);
background = reshape(background',RR,CC,CRGB);
background = uint8(background);
background = background_Update(:,:,:,end);
%background_Update(:,:,:,tt) = background;
background_hsv = rgb2hsv(background);
image_hsv = rgb2hsv(image_sequence(:,:,:,tt));
for i = 1:RR
for j = 1:CC
if (objects_map(i,j))&&...
(abs(image_hsv(i,j,1)-background_hsv(i,j,1))<SHADOWS(1))&&...
(image_hsv(i,j,2)-background_hsv(i,j,2)<SHADOWS(2))&&...
(SHADOWS(3)<=image_hsv(i,j,3)/background_hsv(i,j,3)<=SHADOWS(4))
Shadows(i,j) = 1;
else
Shadows(i,j) = 0;
end
end
end
Images0(:,:,tt) = objects_map;
objecs_adjust_map = Shadows;
Images2(:,:,tt) = objecs_adjust_map;
Images3(:,:,tt)= Images2(:,:,tt);
background_Update2 = background_Update(:,:,:,tt);
end
Time2 = toc/frameNum_Original;
msgbox(['视频每帧耗时:',num2str(Time2),'s']);
for tt = 1:frameNum_Original
tt
[res3(:,:,:,tt),flags(tt)] = func_fangk(Images0(:,:,tt),pixel_original(:,:,:,tt));
end
axes(handles.axes3)
for tt = 1:frameNum_Original
imshow((Images0(:,:,tt)));
drawnow;
end
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
parameter;
msgbox(['跟踪正确率:',num2str(100*(1-(sum(flags)/frameNum_Original))),'%']);
axes(handles.axes4)
for tt = 1:frameNum_Original
tt
imshow(uint8(res3(:,:,:,tt)));
drawnow;
end
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
clc;
clear;
close all;
4.操作步骤与仿真结论
仿真处理时间计算如下:
正确率如下:
5.参考文献
[1]何宣霖. 基于图像全局和局部特征学习的红外目标检测跟踪算法研究.A10-22