A list,

1. Basic concept of mean shift: cluster points are found along the rising direction of density

(1) Imagine a feature space with N sample points;

(2) Initially determine a center point center, and calculate the vectors of all points (xi) and the center point center in the circular space with a set radius of D;

(3) Calculate the average value of all vectors in the whole circular space, and get an offset mean value;

(4) Move the center point to the offset mean position;

(5) Repeat the movement until certain conditions are met;



2. Mean shift operation:

2.1 Basic formula of Mean Shift:

The mean deviation



Sh: a high-dimensional sphere with the center point x and the radius h; K: number of points contained in Sh range; Xi: points contained within the Sh range

Center to update

Move the center point to the offset mean position



Mt is the mean deviation obtained in the state of T; Xt is the center in state T

2.2 The offset mean value of the introduced kernel function:

Kernel function

The kernel function is just a simple way to calculate the inner product mapped to a higher-dimensional space in order to make low-dimensional non-fractional data into higher-dimensional separable data. By using kernel function, the mapping relation can be ignored and the calculation can be completed directly in low dimensional space.

The offset mean value of the kernel function is introduced

The concept of kernel function is introduced into the mean shift, which can make the point from the center of the calculation have a larger weight, reflecting the shorter the distance, the larger the weight. Improved offset mean:



Where, x is the center point; Xi is the point in the bandwidth range; N is the number of points in the bandwidth range; G of x is the derivative with respect to the kernel minus

3 Applications of mean shift: clustering (K-means clustering) image segmentation (mapping images to feature space and mean shift clustering of sample points) Object contour testing (ray propagation algorithm) Target tracking (solving the optimal Bhattacharya coefficient function)

4.1 Randomly select a point among the unclassified data points as the center point; 4.2 Find out all points within the bandwidth distance from the center point and record them as set M, and think that these points belong to cluster C. 4.3 Calculate the vectors from the center point to each element in set M, and add these vectors to obtain the offset vector. 4.4 The center point moves in the direction of shift, and the distance is the magnitude of the offset vector. 4.5 Repeat steps 2, 3, and 4 until the size of the offset vector meets the set threshold, remembering the center point at this time. 4.6 Repeat 1, 2, 3, 4, 5 until all points are classified. 4.7 Classification: According to the access frequency of each class and each point, the class with the largest access frequency is taken as the owning class of the current point set.

Ii. Source code

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 fromthis 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');
Copy the code

Third, the operation result

Fourth, note

Version: 2014 a