Last active
February 14, 2019 14:15
-
-
Save komietty/58bfd9ad06c43f249504d09ca8177e73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
namespace kmty.Util | |
{ | |
public class MeanShift | |
{ | |
public float Threshold { get; private set; } | |
public int LoopLimit { get; private set; } | |
public Vector2[] Samples { get; private set; } | |
public Meanshift(float th, int lim, Vector2[] smpls) | |
{ | |
this.Threshold = th; | |
this.LoopLimit = lim; | |
this.Samples = smpls; | |
} | |
public Vector2 Procedure(Vector2 initial) | |
{ | |
Vector2 curr = initial; | |
Vector2 next = Vector2.zero; | |
float h = 1f; | |
for (var j = 0; j < LoopLimit; j++) | |
{ | |
Vector2 molecule = Vector2.zero; | |
float denominator = 0; | |
for (int i = 0; i < Samples.Length; i++) | |
{ | |
float arg = Vector2.SqrMagnitude(curr - Samples[i]) / h; | |
float g = GaussionKernelDifferential(arg); | |
molecule += g * Samples[i]; | |
denominator += g; | |
} | |
next = molecule / denominator; | |
if (Vector2.Distance(next, curr) < Threshold) break; | |
curr = next; | |
} | |
return next; | |
} | |
float GaussionKernelDifferential(float t) | |
{ | |
return t * Mathf.Exp(-t * t / 2); | |
} | |
float EpanechnikovKernelDifferential(float t) | |
{ | |
if (Mathf.Abs(t) <= 1) return 1f; | |
else return 0f; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment