MeanShift
更多信息和选项
- MeanShift 也称为模式搜寻,并且通常用于数据数组和图像的平滑处理.
- MeanShift 保持输入元素的顺序.
- 在 MeanShift[image,d,parts] 中,parts 可以是一个记号图像或者 {row,column} 位置列表.
- 可以给出下列选项:
-
DistanceFunction EuclideanDistance 距离度量函数 MaxIterations 1 执行的最大迭代次数 Tolerance 0 假定收敛性允许的容差 Weights Automatic 用于计算均值的权值 - 设置 Tolerance->t 时,如果没有点改变超过 t 时迭代停止.
- 默认情况下,使用单位权值. 使用 Weights->f,应用于元素之间的可调节尺寸的距离的函数 f 用于计算和返回数值的加权均值. 0和 d 之间的距离缩放到范围0和1之间.
- Weights 的典型设置包括:
-
UnitStep 单位权值(默认) UnitTriangle 线性递减权值 "Gaussian" 基于具有 sigma
的高斯窗的权值{"Gaussian",σ} 具有 sigma σ 的高斯窗 - 通常将 DistanceFunction 选项设置为:
-
ManhattanDistance 曼哈顿或者“市街区”距离 EuclideanDistance 欧几里得距离 SquaredEuclideanDistance 平方欧几里得距离 NormalizedSquaredEuclideanDistance 归一化的平方欧几里得距离 CosineDistance 角余弦距离 CorrelationDistance 相关系数距离 f 使用任意函数 f
范例
打开所有单元 关闭所有单元基本范例 (3)
范围 (4)
data = Table[1 + RandomReal[{-0.1, 0.1}], {10}]MeanShift[data, 0.1, MaxIterations -> 10]MeanShift[{1, 2, a + 5, a + 6}, 1]MeanShift[{10, 20, 30, 45, 50}, 10, {1, 3}]MeanShift[{1.0000000000000000000, 2.0000000000000000000, 3.0000000000000000000, 4.0000000000000000000}, 2]选项 (8)
DistanceFunction (3)
默认情况下,使用 EuclideanDistance:
MeanShift[{0, 1, 10, 11}, 2]MeanShift[{{0, 0}, {1, 1}, {0, 10}, {0, 11}}, 1]MeanShift[{{0, 0}, {1, 1}, {0, 10}, {0, 11}}, 1, DistanceFunction -> ChessboardDistance]MeanShift[{{0, 0}, {1, 1}, {0, 10}, {0, 11}}, 1, DistanceFunction -> (Min[#1 - #2]&)]MaxIterations (2)
MeanShift[{1, 2, 3, 4, 5}, 1]使用 MaxIterations 指定迭代次数:
MeanShift[{1, 2, 3, 4, 5}, 1, MaxIterations -> 5]使用 MaxIterations->Infinity 运行直到收敛:
MeanShift[{1, 2, 3, 4, 5}, 1, MaxIterations -> Infinity]使用 MaxIterations 对数据进行迭代平滑:
BlockRandom[SeedRandom[0];
data = Table[1. + RandomReal[{-0.1, 0.1}], {1000}]];
{Table[ListLinePlot[{data, MeanShift[data, 0.05, MaxIterations -> iter]}],
{iter, {1, 5, 10}}]}//GridTolerance (1)
Weights (2)
MeanShift[{0, 1, 3}, 2]MeanShift[{0, 1, 3}, 2, Weights -> UnitTriangle]MeanShift[{0, 1, 3}, 2, Weights -> {"Gaussian", 1 / 2}]i = [image];MeanShift[i, .3, MaxIterations -> Infinity, Weights -> "Gaussian"]MeanShift[i, .3, MaxIterations -> Infinity]应用 (9)
平滑 (2)
data = ({Sin[#], Cos[#]} + RandomReal[{-0.1, 0.1}, 2])& /@ RandomReal[{0, 2 Pi}, 2000];ListPlot[data]ListPlot[MeanShift[data, 0.1, MaxIterations -> 3]]使用联合空间范围区域中五维特征的均值漂移,对图像进行平滑处理:
img = [image];
{w, h} = ImageDimensions[img];使用 LABColor 数值,适用于计算颜色距离:
pix = Flatten[ImageData[ColorConvert[img, "LAB"]], 1];pos = Flatten[N@Array[{#1, #2}&, {h, w}], 1];data = Join[pix, pos / Max[{h, w}], 2];使用 "Gaussian" 权值和 EuclideanDistance 执行均值漂移的一次迭代,等价于对高斯权值的空间和范围特征加倍:
ms = MeanShift[data, 0.1, Weights -> "Gaussian"];Image[ArrayReshape[ms[[All, ;; 3]], {h, w, 3}], ColorSpace -> "LAB"]AbsoluteTiming[ms = MeanShift[data, 0.1, Weights -> "Gaussian", MaxIterations -> 10];]Image[ArrayReshape[ms[[All, ;; 3]], {h, w, 3}], ColorSpace -> "LAB"]均值漂移的移动 (2)
list = {1, 2, 3, 4};
ms = MeanShift[list, 1]msvector = ms - listBlockRandom[
SeedRandom[0];
list = Join[RandomVariate[BinormalDistribution[{0, 0}, {1, 1 / 2}, 0], 2000], {{2., -1.}}]];Histogram3D[list]index = {Length[list]};
radius = 1 / 2;
pos = Flatten[
FixedPointList[
Part[MeanShift[ReplacePart[list, Thread[index -> #]], radius, index], index]&,
Part[list, index], SameTest -> (Max@Abs[#1 - #2] < 10 ^ -10&)
], 1];msvectors = Differences[pos];ListLinePlot[Norm /@ msvectors, PlotRange -> All, Filling -> Axis, FillingStyle -> Automatic, ColorFunctionScaling -> False, ColorFunction -> Function[{x, y}, Hue[Rescale[ArcTan[Sequence@@msvectors[[x]]], {-Pi, Pi}]]]]pdf = Function[{x, y}, Evaluate[PDF[SmoothKernelDistribution[list], {x, y}]]];
point3D[{x_, y_}] := Point[{x, y, pdf[x, y] + .005}];Show[Plot3D[pdf[x, y], {x, -4, 4}, {y, -2, 2}, PlotRange -> All, ImageSize -> 300], Graphics3D[{Red, PointSize[Medium], point3D /@ pos}]]求估计分布的模式 (1)
dist = MixtureDistribution[{1, 1, 2}, {NormalDistribution[0, 1 / 4], NormalDistribution[2, 1 / 3], NormalDistribution[4, 1 / 2]}];n = 10000;
BlockRandom[SeedRandom[0];data = RandomVariate[dist, n]];Show[Histogram[data, Automatic, "PDF", PlotRange -> {{-1, 6}, {0, .5}}], Plot[PDF[dist, x], {x, -1, 6}]]idx = Flatten[Position[data, #, 1, 1]& /@ Quantile[data, Range[1, n, n / 100] / n]];radius = 0.25;
ms = Part[MeanShift[data, radius, idx, MaxIterations -> ∞, Weights -> "Gaussian", Tolerance -> 10 ^ -10], idx];通过从扰动位置重新开始均值漂移,对非内在概率密度函数的局部最大值的点进行处理:
perturbed = ReplacePart[data, Thread[idx -> ms + radius * RandomChoice[{-1, 1}, Length[idx]]]];msperturb = Part[MeanShift[perturbed, radius, idx, MaxIterations -> ∞, Weights -> "Gaussian", Tolerance -> 10 ^ -10], idx];peaks = Extract[ms, Position[ms - msperturb, x_ /; Abs[x] < 10 ^ -2]]modes = Mean /@ Gather[peaks, Abs[#1 - #2] ≤ radius&]Show[Histogram[data, Automatic, "PDF", PlotRange -> {{-1, 6}, {0, .5}}], Plot[PDF[dist, x], {x, -1, 6}], Epilog -> {Red, Line[{{#, 0}, {#, 1}}]& /@ modes}]聚类 (2)
list = Join[
({Cos[#], Sin[#]} + RandomReal[{-0.1, 0.1}, 2])& /@ RandomVariate[NormalDistribution[Pi, 1], 1000],
RandomVariate[BinormalDistribution[{1, -1}, {.3, .5}, 0], 1000],
RandomVariate[BinormalDistribution[{1, 1}, {.3, .5}, 0], 1000]
];ListPlot[list]res = MeanShift[list, .4, MaxIterations -> ∞, DistanceFunction -> SquaredEuclideanDistance, Weights -> UnitTriangle, Tolerance -> 10 ^ -10];c = ArrayComponents[res, 1];
clust = GatherBy[Transpose[{list, c}], Last];
clust = #[[All, 1]]& /@ clust;ListPlot[clust]n = 10000;
dist = MixtureDistribution[{1, 1}, {BinormalDistribution[{-3 / 2, 0}, {1, 1}, 0], BinormalDistribution[{3 / 2, 0}, {1, 1}, 0]}];
BlockRandom[SeedRandom[1];data = RandomVariate[dist, n]];{ListPlot[data], Plot3D[PDF[dist, {x, y}], {x, -6, 6}, {y, -4, 4}, PlotRange -> All]}indices = RandomSample[Range[n], 100];
radius = .2;paths = Transpose@FixedPointList[
Part[MeanShift[ReplacePart[data, Thread[indices -> #]], 1, indices, Weights -> "Gaussian"], indices]&,
Part[data, indices],
∞, SameTest -> (Max@Abs[#1 - #2] < 10 ^ -10&)];ListLinePlot[paths, PlotRange -> {{-4, 4}, {-3, 3}}]groups = Gather[Range[Length@indices], EuclideanDistance[paths[[#1, -1]], paths[[#2, -1]]] ≤ radius&];l = Table[Position[groups, i, 2][[1, 1]], {i, Length[indices]}];
Short[l]labels = ConstantArray[0, n];
labels[[indices]] = l;nf = Nearest[Flatten[Table[# -> l[[i]]& /@ paths[[i]], {i, Length[indices]}]]];Table[
labels[[i]] = First@nf[data[[i]]],
{i, Complement[Range[n], indices]}];clust = GatherBy[Transpose[{data, labels}], Last];
clust = #[[All, 1]]& /@ clust;
ListPlot[clust]AbsoluteTiming[res = MeanShift[data, 1, MaxIterations -> ∞, Weights -> "Gaussian", Tolerance -> 10 ^ -10];]图像分割 (2)
在 "LAB" 颜色分量的三维特征空间中迭代进行均值漂移滤波:
img = [image];radius = 0.15;
ms = MeanShift[ColorConvert[img, "LAB"], radius, MaxIterations -> 10]img = [image];
{w, h} = ImageDimensions[img];pix = Flatten[ImageData[ColorConvert[img, "LAB"]], 1];
pos = Flatten[N@Array[{#1, #2} / Max[{h, w}]&, {h, w}], 1];
data = Join[pix, pos, 2];radius = 0.25;
AbsoluteTiming[ms = MeanShift[data, radius, Weights -> "Gaussian", MaxIterations -> 10];]comp = SortBy[Tally[ms], -Last[#]&];
groups = SortBy[#, Function[c, -Last[c]]]& /@ Gather[comp, EuclideanDistance[First[#1], First[#2]] ≤ radius&];
Length[groups]d = ms;
rules = Table[
Map[#[[1]] -> g[[1, 1]]&, Rest[g]],
{g, Cases[groups, x_ /; Length[x] > 1]}];
d = d /. Dispatch[Flatten[rules]];segments = ArrayReshape[ArrayComponents[d, 1], {h, w}];
resimg = Image[ArrayReshape[d[[All, ;; 3]], {h, w, 3}], ColorSpace -> "LAB"];Image[#, ImageSize -> 200]& /@ {Colorize[segments], resimg}属性和关系 (3)
list = {10, 20, 30, 40, 55};
parts = {4, 1};
ms = MeanShift[list, 10, parts]Part[ms, parts]i = [image];mask = SelectComponents[Binarize[i], #Label == 1&]ms = MeanShift[i, .15, mask, MaxIterations -> 10]PixelValue[ms, PixelValuePositions[mask, 1]]//Shallowdist = BinormalDistribution[{0, 0}, {1, 2}, 0];
radius = 1;BlockRandom[
SeedRandom[1];
data = RandomVariate[dist, 10 ^ 4]
];msvector = MeanShift[data, radius] - data;pdf = Function[{x, y}, Evaluate[PDF[dist, {x, y}]]];
grad = Function[{x, y}, Evaluate[{D[pdf[x, y], x], D[pdf[x, y], y]}]];δ = MapThread[CosineDistance[#1, grad[Sequence@@#2] / pdf[Sequence@@#2]]&, {msvector, data}];{Min[δ], Median[δ], Max[δ]}文本
Wolfram Research (2010),MeanShift,Wolfram 语言函数,https://reference.wolfram.com/language/ref/MeanShift.html (更新于 2014 年).
CMS
Wolfram 语言. 2010. "MeanShift." Wolfram 语言与系统参考资料中心. Wolfram Research. 最新版本 2014. https://reference.wolfram.com/language/ref/MeanShift.html.
APA
Wolfram 语言. (2010). MeanShift. Wolfram 语言与系统参考资料中心. 追溯自 https://reference.wolfram.com/language/ref/MeanShift.html 年
BibTeX
@misc{reference.wolfram_2026_meanshift, author="Wolfram Research", title="{MeanShift}", year="2014", howpublished="\url{https://reference.wolfram.com/language/ref/MeanShift.html}", note=[Accessed: 25-November-2026]}
BibLaTeX
@online{reference.wolfram_2026_meanshift, organization={Wolfram Research}, title={MeanShift}, year={2014}, url={https://reference.wolfram.com/language/ref/MeanShift.html}, note=[Accessed: 25-November-2026]}