阅读 411

【图像跟踪】基于matlab GUI均值漂移图像跟踪【含Matlab源码 743期】

一、简介

1.均值漂移的基本概念:沿着密度上升方向寻找聚簇点
(1)设想在一个有N个样本点的特征空间;
(2)初始确定一个中心点center,计算在设置的半径为D的圆形空间内所有的点(xi)与中心点center的向量;
(3)计算整个圆形空间内所有向量的平均值,得到一个偏移均值;
(4)将中心点center移动到偏移均值位置;
(5)重复移动,直到满足一定条件结束;
在这里插入图片描述
2 均值漂移运算:
2.1 Mean shift的基础公式:
偏移均值
在这里插入图片描述
Sh:以x为中心点,半径为h的高维球区域; k:包含在Sh范围内点的个数; xi:包含在Sh范围内的点
中心更新
将中心点移动到偏移均值位置
在这里插入图片描述
Mt为t状态下求得的偏移均值; xt为t状态下的中心

2.2 引入核函数的偏移均值:
核函数
核函数只是用来计算映射到高维空间之后的内积的一种简便方法,目的为让低维的不可分数据变成高维可分。利用核函数,可以忽略映射关系,直接在低维空间中完成计算。
引入核函数的偏移均值
在均值漂移中引入核函数的概念,能够使计算中距离中心的点具有更大的权值,反映距离越短,权值越大的特性。改进的偏移均值:
在这里插入图片描述
其中,x为中心点;xi为带宽范围内的点;n为带宽范围内的点的数量;g(x)为对核函数的导数求负

3 均值漂移的应用:
聚类(K均值聚类)
图像分割(将图像映射到特征空间,对采样点进行均值漂移聚类)
对象轮廓检验(光线传播算法)
目标跟踪(求解最优化Bhattacharya系数函数)

4 均值漂移运算步骤:
4.1 在未被分类的数据点中随机选择一个点作为中心点;
4.2 找出离中心点距离在带宽之内的所有点,记做集合M,认为这些点属于簇c。
4.3 计算从中心点开始到集合M中每个元素的向量,将这些向量相加,得到偏移向量。
4.4 中心点沿着shift的方向移动,移动距离是偏移向量的模。
4.5 重复步骤2、3、4,直到偏移向量的大小满足设定的阈值要求,记住此时的中心点。
4.6 重复1、2、3、4、5直到所有的点都被归类。
4.7 分类:根据每个类,对每个点的访问频率,取访问频率最大的那个类,作为当前点集的所属类。

二、源代码

function varargout = GUI(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @GUI_OpeningFcn, ...
                   'gui_OutputFcn',  @GUI_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
function GUI_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
handles.FileName = 0;
colour = get(handles.Video_Panel,'BackgroundColor');
img = imread('Files\Play_up.bmp');
handles.icon_play_up = Set_Button_Bckgrnd(img,colour);
set(handles.Play,'CData',handles.icon_play_up);
img = imread('Files\Play_down.bmp');
handles.icon_play_down = Set_Button_Bckgrnd(img,colour);
set(handles.Speed,'Value',10);
in = imread('Files\Mark_in.bmp');
out = imread('Files\Mark_out.bmp');
in = Set_Button_Bckgrnd(in,colour);
out = Set_Button_Bckgrnd(out,colour);
set(handles.Mark_in,'CData',in);
set(handles.Mark_out,'CData',out);
icon = imread('Files\Crop.bmp');
icon = Set_Button_Bckgrnd(icon,colour);
set(handles.Crop,'CData',icon);
icon = imread('Files\Select_Target.bmp');
icon = Set_Button_Bckgrnd(icon,colour);
set(handles.Select_Target,'CData',icon);
set(handles.Gaussian,'Value',1);
handles.Kernel_type = 'Gaussian';
handles.radius = str2double(get(...
    handles.Radius_edit,'String'))/100;
% Set Default Iteration limiters
handles.f_thresh = str2double(get(...
    handles.F_thresh_edit,'String'));
handles.max_iter = str2double(get(...
    handles.Max_iter_edit,'String'));
% Update handles structure
guidata(hObject, handles);
%Display Default Images
axes(handles.Target)
img = imread('Files\Target_Default.png');
imshow(img);
axes(handles.Video)
img = imread('Files\Video_Default.png');
imshow(img);

%% Button Background Colour Function
function icon = Set_Button_Bckgrnd(icon,colour)
for i=1:size(icon,1)
    for j=1:size(icon,2)
        if mean(icon(i,j,:))>= 196
            icon(i,j,:) = icon(i,j,:)-uint8(round(...
                255*(ones(1,1,3)-permute(colour,[1 3 2]))));
        end
    end
end

%% Outputs from this function are returned to the command line.
function varargout = GUI_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;

%% Objects Creation Functions
% Executes during object creation,
%after setting all properties.
function Video_CreateFcn(hObject, eventdata, handles)
function Vid_Path_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'),...
        get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function Slider_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function Slider_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function Speed_CreateFcn(hObject, eventdata, handles)
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end
function Mark_in_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function Mark_out_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function Select_Target_CreateFcn(hObject, eventdata, handles)
function Target_CreateFcn(hObject, eventdata, handles)
function Radius_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function F_thresh_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function Max_iter_edit_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end
function Similarity_Plot_CreateFcn(hObject, eventdata, handles)

%% Close Button Callback
function Close_Callback(hObject, eventdata, handles)
% setappdata(handles.MeanShift,'mydata',matrices);
display 'GUI closed. Thanks for using it. :)'
close(handles.MeanShift);

%% Video Input Callbacks
function Browse_Callback(hObject, eventdata, handles)

[FileName,PathName] = uigetfile({'*.avi','(*.avi) AVI video file'},...
    'Select input AVI video...');
if FileName~=0
    set(handles.Vid_Path,'String',strcat(PathName,FileName));
    Vid_Path_Callback(handles.Vid_Path,[], handles);
end

function Vid_Path_Callback(hObject, eventdata, handles)
Path = get(hObject,'String');
if exist(Path,'file')==2
    L = length(Path);
    if L>3
        extension = Path(L-3:L);
        if strcmpi(extension,'.avi')==1
            set(hObject,'String',...
                'Importing video, be patient...',...
                'ForegroundColor','red',...
                'FontWeight','bold');
            drawnow;
            [handles.lngth,handles.height,handles.width,...
                handles.Movie]=Import_mov(Path);
            set(hObject,'String',Path,...
                'ForegroundColor','black',...
                'FontWeight','normal');
            for i=L:-1:1
                if strcmp(Path(i),'\')==1
                    break;
                end
            end
            handles.FileName = Path(L-i:L-3);
            Video_Init(hObject,handles);
        end
    end
end

%% Video Initialization
function Video_Init(hObject,handles)
handles.indx_start_frame = 1;
handles.indx_end_frame = handles.lngth;
guidata(hObject,handles);
set(handles.Play,'Enable','on');
set(handles.Speed,'Enable','on');
set(handles.Slider,'Min',1,'Max',handles.lngth,...
    'Value',1,'SliderStep',[0.01 0.1],'Enable','on');
Video_Callback(handles.Video,[],handles);
set(handles.Slider_edit,'Enable','on','String',1);
set(handles.Mark_in,'Enable','on');
set(handles.Mark_out,'Enable','on');
set(handles.Mark_in_edit,'Enable','on','String',1);
set(handles.Mark_out_edit,'Enable','on',...
    'String',num2str(handles.lngth));
set(handles.Crop,'Enable','on');
Set_Mark_in_pointer(handles.Mark_in_pointer,1,handles);
Set_Mark_in_pointer(handles.Mark_in_edit,1,handles);
Set_Mark_out_pointer(handles.Mark_out_pointer,...
    handles.lngth,handles);
Set_Mark_out_pointer(handles.Mark_out_edit,...
    handles.lngth,handles);
Disable_Parameters_Panels(handles);
Target_Init(handles);

%% Disable parameters access while no target selected
function Disable_Parameters_Panels(handles)
set(handles.Uniform,'Enable','off');
set(handles.Triangular,'Enable','off');
set(handles.Epanechnikov,'Enable','off');
set(handles.Gaussian,'Enable','off');
set(handles.Radius_edit,'Enable','off');
set(handles.F_thresh_edit,'Enable','off');
set(handles.Max_iter_edit,'Enable','off');
set(handles.Start,'Enable','off');
复制代码

三、运行结果

在这里插入图片描述

四、备注

版本:2014a

文章分类
人工智能
文章标签