function [mode] = mean_shift(data, h, epsilon, dist2)
% locating the mode of data (local maxima of density function)
% using mean shift.
% h - Gaussian kernel with parameter h (window size)
% eps - epsilon accuracy of mean shift step. i.e. how small m_hG to be
% considered as no improvement - NOT RECCOMAND eps = 0!!
% data - a dxN matrix of N vector of d dimension
% dist2 - pointer ro function that measures distance square
% the distance function should be able to take two d by N matrices
% and return a vector of length N, where Ni is the distance^2
% between the two i-th vectors.
%
%
% Copyright (c) Bagon Shai
% Department of Computer Science and Applied Mathmatics
% Wiezmann Institute of Science
% http://www.wisdom.weizmann.ac.il/
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, subject to the following conditions:
%
% 1. The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
% 2. No commercial use will be done with this software.
% 3. If used in an academic framework - a proper citation must be included.
%
% The Software is provided "as is", without warranty of any kind.
%
% Mar. 2007
%
NUM_OF_TRAILS = 1;
[d N] = size(data);
NUM_OF_TRAILS = min(NUM_OF_TRAILS,N);
% choose the random starting points
rsp = randsample(1:N,NUM_OF_TRAILS);
eps2 = epsilon*epsilon;
% start from a random x
x = data(:, rsp(1));
[mode, score] = mean_shift_iteration(x, data, h, eps2, dist2);
for ii = 1:NUM_OF_TRAILS-1
x = data(:,rsp(ii+1));
[m, s] = mean_shift_iteration(x, data, h, eps2, dist2);
if ( s > score )
mode = m;
score = s;
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [mode, score] = mean_shift_iteration(x, data, h, eps2, dist2, per)
% iterate mean shift search from starting point x
% compute the first mean shift step
[ms, score] = m_hG(x, data, h, dist2);
% iterate untill no step is achieved
while ( dist2(ms , x) >= eps2 )
x = ms;
[ms, score] = m_hG(x, data, h, dist2);
end
mode = ms;
% prune non local maxima points
% % if ( ~ pertrube(x, data, h, eps2, dist2) )
% % score = -1;
% % end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [LocalMax] = pertrube(x, data, h, eps2, dist2)
% we reahed a stationary point, we want to pertrube x a little
% and check if its a local maxima or not
% the critiria: move x a little if the mean shift returns to the same point
% its a local maxima.
% pertrubing by random vector of norm h/4
per = rand( size(data,1) ,1)-0.5;
per = h * per ./ ( sqrt( dist2(per, zeros(size(per)) ) ) );
[mode, score] = mean_shift_iteration(x + per, data, h, eps2, dist2);
% logical variable
% the mean shift step must be toward x.
LocalMax = ( dist2(mode, x) <= sqrt(eps2) );
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function [ms, score]=m_hG(x, data, h, dist2)
% compute the mean shift of the data
% x - relative to x location
% data - dxN
% h - gaussian window
% SUM Xi exp( -.5 || dist(X,Xi)/h ||^2 )
% m = ---------------------------------
% h,G SUM exp( -.5 || dist(X,Xi)/h ||^2 )
% e is the argument of the exponent in the term above: i.e. 1xN vector with
% ||X-Xi/h||^2 at the i-th column
e = dist2( repmat(x, 1, size(data,2)), data ) ./ (h.^2);
e = exp(-0.5*e);
score = sum(e);
% the mean shift
ms = (data*e' ./ score);
6180007mean_shift.rar_mean shift_speakxac_super pixel_超像素分割
版权申诉
133 浏览量
2022-07-14
05:30:31
上传
评论
收藏 2KB RAR 举报
御道御小黑
- 粉丝: 61
- 资源: 1万+
最新资源
- 微信小程序 - 图书管理系统源码.zip
- 微信小程序 - 图片自适应 ,富文本解析源码.zip
- 微信小程序 - 同乐居商城:购物车合算源码
- 1、根据输入的三条边值判断能组成何种三角形,并设计测试数据进行判定覆盖测试 三条边为变量a、b、c,范围为1≤边值≤10,不在范
- SQL server 练习题目8道(小白教学).zip
- Python 手写实现 iD3 决策树算法-根据信息增益公式.zip
- 411675952289057车联助手-小窗版(三星)3.5.1.apk
- 三种快速排序方法合并在一个文件中以便直接运行的Python代码示例
- 937712277954201实习5.word
- 2程序语言基础知识pdf1_1716337722703.jpeg
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈