private static ClusterPoint[] FindCenters(List<ClusterPoint> points) { ClusterPoint[] result = new ClusterPoint[clustersAmount]; double[] pointsAmount = new double[clustersAmount]; for (int i = 0; i < clustersAmount; i++) { pointsAmount[i] = 0; result[i] = new ClusterPoint(0, 0, 0); } foreach (var p in points) { result[p.ClusterNumber].Amplitude += p.Amplitude; result[p.ClusterNumber].Frequency += p.Frequency; result[p.ClusterNumber].Variance += p.Variance; ++pointsAmount[p.ClusterNumber]; } for (int i = 0; i < clustersAmount; i++) { result[i].Amplitude /= pointsAmount[i]; result[i].Frequency /= pointsAmount[i]; result[i].Variance /= pointsAmount[i]; } return result; }
public void FromByteArrayTest() { Guid centroidID = Guid.NewGuid(); MemoryStream stream = new MemoryStream(); stream.Write(BitConverter.GetBytes(2.53), 0, sizeof(double)); stream.Write(BitConverter.GetBytes(4.56), 0, sizeof(double)); stream.Write(centroidID.ToByteArray(), 0, 16); byte[] bytes = stream.ToArray(); ClusterPoint expected = new ClusterPoint { X = 2.53F, Y = 4.56F, CentroidID = centroidID }; ClusterPoint actual; actual = ClusterPoint.FromByteArray(bytes); const double Epsilon = 0.0001; Assert.IsTrue(Math.Abs(expected.X - actual.X) < Epsilon); Assert.IsTrue(Math.Abs(expected.Y - actual.Y) < Epsilon); Assert.AreEqual(expected.CentroidID, actual.CentroidID); }
public void ToByteArrayTest() { Guid centroidID = Guid.NewGuid(); ClusterPoint target = new ClusterPoint { X = 1.23, Y = 3.45, CentroidID = centroidID }; MemoryStream stream = new MemoryStream(); stream.Write(BitConverter.GetBytes(1.23), 0, sizeof(double)); stream.Write(BitConverter.GetBytes(3.45), 0, sizeof(double)); stream.Write(centroidID.ToByteArray(), 0, 16); byte[] expected = stream.ToArray(); byte[] actual; actual = target.ToByteArray(); Assert.AreEqual(expected.Length, actual.Length); for (int i = 0; i < expected.Length; i++) { Assert.AreEqual(expected[i], actual[i]); } }
public void FilterPoint() { if (miniCentroid == null) { //Debug.Log("MiniCentroid is null"); Hide(); return; } for (int i = 0; i < miniCentroid.points.Count; i++) { ClusterPoint p = miniCentroid.points[i]; if (!p.Active || !Active) { continue; } if (p == this) { continue; } float dist = Vector3.Distance(p.transform.position, transform.position); if (dist > 2f) { miniCentroid.points.Remove(this); Hide(); } } }
private static ClusterPoint[] FindCenters(List <ClusterPoint> points) { ClusterPoint[] result = new ClusterPoint[clustersAmount]; double[] pointsAmount = new double[clustersAmount]; for (int i = 0; i < clustersAmount; i++) { pointsAmount[i] = 0; result[i] = new ClusterPoint(0, 0, 0); } foreach (var p in points) { result[p.ClusterNumber].Amplitude += p.Amplitude; result[p.ClusterNumber].Frequency += p.Frequency; result[p.ClusterNumber].Variance += p.Variance; ++pointsAmount[p.ClusterNumber]; } for (int i = 0; i < clustersAmount; i++) { result[i].Amplitude /= pointsAmount[i]; result[i].Frequency /= pointsAmount[i]; result[i].Variance /= pointsAmount[i]; } return(result); }
public double Distance(ClusterPoint p) { double da = Amplitude - p.Amplitude; double df = Frequency - p.Frequency; double dv = Variance - p.Variance; return Math.Sqrt(DA * DA * da * da + DF * DF * df * df + DV * DV * dv * dv); }
private static void Iterate(List<ClusterPoint> points, ClusterPoint[] centers) { foreach (var p in points) { p.ClusterNumber = p.FindClosestIndex(centers); } }
public double Distance(ClusterPoint p) { double da = Amplitude - p.Amplitude; double df = Frequency - p.Frequency; double dv = Variance - p.Variance; return(Math.Sqrt(DA * DA * da * da + DF * DF * df * df + DV * DV * dv * dv)); }
private static bool IsClustered(ClusterPoint[] oldCenters, ClusterPoint[] newCenters) { for (int i = 0; i < clustersAmount; i++) { if (oldCenters[i].Amplitude != newCenters[i].Amplitude || oldCenters[i].Frequency != newCenters[i].Frequency || oldCenters[i].Variance != newCenters[i].Variance) return false; } return true; }
public void ProcessWorkerResponseTest() { KMeansJobData jobData = new KMeansJobData(Guid.NewGuid(), 4, null, 2, 10, DateTime.Now); KMeansJob_Accessor target = new KMeansJob_Accessor(jobData, "server"); target.InitializeStorage(); // Upload a block with an arbitrary ClusterPoint, so we can verify it gets copied ClusterPoint arbitraryPoint = new ClusterPoint(1, 2, Guid.NewGuid()); List <string> blockList; using (ObjectCachedBlockWriter <ClusterPoint> pointPartitionWriteStream = new ObjectCachedBlockWriter <ClusterPoint>(target.Points, point => point.ToByteArray(), ClusterPoint.Size, Environment.GetEnvironmentVariable("TEMP") + @"\" + Guid.NewGuid().ToString())) { pointPartitionWriteStream.Write(arbitraryPoint); pointPartitionWriteStream.FlushBlock(); blockList = pointPartitionWriteStream.BlockList; } KMeansTaskData taskData = new KMeansTaskData(jobData, Guid.NewGuid(), 0, 1, target.Centroids.Uri, DateTime.Now, 0, null); target.tasks.Clear(); target.tasks.Add(new KMeansTask(taskData)); KMeansTaskResult taskResult = new KMeansTaskResult(taskData); CloudBlob pointsBlockListBlob = AzureHelper.CreateBlob(jobData.JobID.ToString(), Guid.NewGuid().ToString()); using (Stream stream = pointsBlockListBlob.OpenWrite()) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(stream, blockList); } taskResult.PointsBlockListBlob = pointsBlockListBlob.Uri; taskResult.NumPointsChanged = 2; Guid centroidID = Guid.NewGuid(); taskResult.PointsProcessedDataByCentroid = new Dictionary <Guid, PointsProcessedData> { { centroidID, new PointsProcessedData() { NumPointsProcessed = 2, PartialPointSum = new Point(1, 2) } } }; target.ProcessWorkerResponse(taskResult, new List <Worker>()); // Verify that the first ClusterPoint in Points is indeed equal to arbitraryPoint using (ObjectStreamReader <ClusterPoint> pointsStream = new ObjectStreamReader <ClusterPoint>(target.Points, ClusterPoint.FromByteArray, ClusterPoint.Size)) { ClusterPoint point = pointsStream.First(); Assert.AreEqual(arbitraryPoint.X, point.X); Assert.AreEqual(arbitraryPoint.Y, point.Y); Assert.AreEqual(arbitraryPoint.CentroidID, point.CentroidID); } }
public static ClusterPoint[] Clusterization(List <ClusterPoint> points, ClusterPoint[] centers) { ClusterPoint[] newCenters = new ClusterPoint[clustersAmount]; do { Array.Copy(centers, newCenters, clustersAmount); Iterate(points, centers); centers = FindCenters(points); } while (!IsClustered(centers, newCenters)); return(centers); }
public int FindClosestIndex(ClusterPoint[] centers) { int index = 0; double min = Distance(centers[0]); for (int i = 1; i < clustersAmount; i++) { if (Distance(centers[i]) < min) { min = Distance(centers[i]); index = i; } } return index; }
public void ClusterPointConstructorTest() { Point p = new Point { X = 3.25F, Y = 4.12F }; Guid centroidID = Guid.NewGuid(); ClusterPoint target = new ClusterPoint(p, centroidID); Assert.AreEqual(p.X, target.X); Assert.AreEqual(p.Y, target.Y); Assert.AreEqual(centroidID, target.CentroidID); }
public Centroid GetClosestCentroid(Centroid closest, ClusterPoint cp) { Centroid c; closest = null; distanceMagnitude = Vector3.SqrMagnitude(cp.position - cp.centroid.position); for (int i = 0; i < Cluster.Instance.centroids.Count; i++) { c = Cluster.Instance.centroids[i]; magnitude = Vector3.SqrMagnitude(cp.position - c.position); if (magnitude < distanceMagnitude) { closest = c; distanceMagnitude = magnitude; } } return(closest); }
void OnTriggerEnter(Collider col) { if (col.gameObject.tag == "Point") { point = col.gameObject.GetComponent <ClusterPoint>(); centroid = col.gameObject.GetComponent <ClusterPoint>().centroid; if (point.distance > bound) { col.gameObject.GetComponent <ClusterPoint>().Active = false; //col.gameObject.GetComponent<SpriteRenderer>().enabled = false; col.gameObject.GetComponent <BoxCollider>().enabled = false; } else { okay = false; } return; } if (col.gameObject.tag == "Player") { okay = false; } //} //if (okey) //{ // if (col.gameObject.tag == "Point") // { // point = col.gameObject.GetComponent<ClusterPoint>(); // centroid = col.gameObject.GetComponent<ClusterPoint>().centroid; // //magnitude = Vector3.SqrMagnitude(centroid.position - point.position); // //Debug.Log(magnitude); // if (point.distance > bound) // col.gameObject.GetComponent<MeshRenderer>().enabled = false; // else // okey = false; // } //} }
public void EnumeratorTest() { ClusterPoint p = new ClusterPoint(1, 2, Guid.NewGuid()); MemoryStream stream = new MemoryStream(); const int NumElements = 5; for (int i = 0; i < NumElements; i++) { stream.Write(p.ToByteArray(), 0, ClusterPoint.Size); } ObjectStreamReader <ClusterPoint> pointStream = new ObjectStreamReader <ClusterPoint>(new MemoryStream(stream.ToArray()), ClusterPoint.FromByteArray, ClusterPoint.Size); Assert.AreEqual(p.CentroidID, pointStream.First().CentroidID); DateTime serialStart = DateTime.Now; int[] serialOutput = pointStream.Select(point => { System.Threading.Thread.Sleep(200); return(1); }).ToArray(); DateTime serialEnd = DateTime.Now; Assert.AreEqual(NumElements, serialOutput.Length); DateTime parallelStart = DateTime.Now; int[] parallelOutput = pointStream.AsParallel().Select(point => { System.Threading.Thread.Sleep(200); return(1); }).ToArray(); DateTime parallelEnd = DateTime.Now; Assert.AreEqual(NumElements, parallelOutput.Length); System.Diagnostics.Trace.WriteLine(string.Format("serial: {0}, parallel: {1}", (serialEnd - serialStart).TotalSeconds, (parallelEnd - parallelStart).TotalSeconds)); }
public void AssignClusterPointToNearestCentroidTest() { KMeansTaskData task = new KMeansTaskData(Guid.NewGuid(), Guid.NewGuid(), 1, null, 2, 3, 10, 0, null, DateTime.Now, DateTime.Now, 0, null); KMeansTaskProcessor_Accessor target = new KMeansTaskProcessor_Accessor(task); target.centroids = new List <Centroid>(); target.centroids.Add(new Centroid { ID = Guid.NewGuid(), X = 0.0F, Y = -1.0F }); target.centroids.Add(new Centroid { ID = Guid.NewGuid(), X = 10.0F, Y = 10.0F }); ClusterPoint clusterPoint = new ClusterPoint { CentroidID = Guid.Empty, X = 1.0F, Y = 2.0F }; ClusterPoint expected = new ClusterPoint { CentroidID = target.centroids[0].ID, X = 1.0F, Y = 2.0F }; ClusterPointProcessingResult_Accessor actual; actual = target.AssignClusterPointToNearestCentroid(clusterPoint); Assert.AreEqual(expected.CentroidID, actual.Point.CentroidID); }
public MiniCentroid GetClosestMiniCentroid(ClusterPoint cp) { MiniCentroid c; closest = null; if (cp.miniCentroid == null) { cp.SetMiniCentroid(this); } distanceMagnitude = Vector3.SqrMagnitude(cp.position - cp.miniCentroid.position); for (int i = 0; i < mainCentroid.miniCentroid.Count; i++) { c = mainCentroid.miniCentroid[i]; magnitude = Vector3.SqrMagnitude(cp.position - c.position); if (magnitude < distanceMagnitude) { closest = c; distanceMagnitude = magnitude; } } return(closest); }
public IEnumerator ICreateVectorPoints(List <Vector2> points) { float scaleMultiplier = 5f; for (int i = 0; i < points.Count; i++) { GameObject clone = (GameObject)Instantiate(clusterPrefab); clone.transform.SetParent(transform); clone.transform.position = new Vector3(points[i].x, 0, points[i].y); clone.transform.localScale = new Vector3(pointScale, pointScale, pointScale) * scaleMultiplier; clone.name = "point" + i; int ID = Random.Range(0, clusterSize); ClusterPoint point = clone.GetComponent <ClusterPoint>(); point.Init(ID); for (int j = 0; j < centroids.Count; j++) { if (centroids[j].ID == ID) { centroids[j].points.Add(point); point.SetCentroid(centroids[j]); } } if (i % 200 == 0) { yield return(null); } } for (int i = 0; i < centroids.Count; i++) { centroids[i].CalculateCenter(); } yield return(null); }
public static void ExpandCluster(List <ClusterPoint> data, ClusterPoint p, List <ClusterPoint> neighborPts, int cId, float epsilon, int minPts) { p.ClusterId = cId; var nCount = neighborPts.Count; for (int i = 0; i < nCount; i++) { var p2 = neighborPts[i]; if (!p2.Visited) { p2.Visited = true; var n2 = new List <ClusterPoint>(); RegionQuery(data, p2, epsilon, out n2); if (n2.Count >= minPts) { neighborPts.AddRange(n2); } } if (p2.ClusterId == 0) { p2.ClusterId = cId; } } }
private List <ClusterPoint> RangeQuery(IEnumerable <ClusterPoint> pts, ClusterPoint currentPoint) { return(pts.Where(x => _distFunc(currentPoint.Point, x.Point).CompareTo(_epsilon) <= 0).ToList()); }
private static void RegionQuery(List <ClusterPoint> data, ClusterPoint p, float epsilon, out List <ClusterPoint> neighborPts) { neighborPts = data.Where(t => t.Data.DistanceTo(p.Data) <= epsilon).ToList(); }
public void InitializeStorageTest() { KMeansJobData jobData = new KMeansJobData(Guid.NewGuid(), 2, null, 4, 10, DateTime.Now); KMeansJob target = new KMeansJob(jobData, "server"); target.InitializeStorage(); // Verify that the created containers and blobs actually exist CloudBlobClient client = AzureHelper.StorageAccount.CreateCloudBlobClient(); CloudBlobContainer c = null; try { c = client.GetContainerReference(jobData.JobID.ToString()); c.FetchAttributes(); } catch (StorageClientException e) { if (e.ErrorCode == StorageErrorCode.ResourceNotFound) { Assert.Fail(); } else { throw; } } CloudBlob points = null, centroids = null; try { points = c.GetBlobReference("points"); points.FetchAttributes(); centroids = c.GetBlobReference("centroids"); centroids.FetchAttributes(); } catch (StorageClientException e) { if (e.ErrorCode == StorageErrorCode.ResourceNotFound) { Assert.Fail(); } else { throw; } } // Verify that unpacking a ClusterPoint actually yields a point with coordinates [-50, 50) and a null centroidID byte[] pointBytes; using (BlobStream pointsStream = points.OpenRead()) { pointBytes = new byte[ClusterPoint.Size]; pointsStream.Read(pointBytes, 0, ClusterPoint.Size); } ClusterPoint p = ClusterPoint.FromByteArray(pointBytes); Assert.IsTrue(p.X >= -50 && p.X < 50); Assert.IsTrue(p.Y >= -50 && p.Y < 50); Assert.AreEqual(p.CentroidID, Guid.Empty); // Verify that the blobs are the correct length Assert.AreEqual(points.Properties.Length, ClusterPoint.Size * jobData.N); Assert.AreEqual(centroids.Properties.Length, Centroid.Size * jobData.K); }
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { backgroundWorker.ReportProgress(0, "working..."); filteredImage = (Bitmap)OriImage.Image.Clone(); int numCluster = (int)numericUpDown1.Value; int maxIterations = (int)numericUpDown2.Value; double accuracy = (double)numericUpDown3.Value; List <ClusterPoint> points = new List <ClusterPoint>(); for (int row = 0; row < originalImage.Width; ++row) { for (int col = 0; col < originalImage.Height; ++col) { Color c2 = originalImage.GetPixel(row, col); points.Add(new ClusterPoint(row, col, c2)); } } List <ClusterCentroid> centroids = new List <ClusterCentroid>(); //Create random points to use a the cluster centroids Random random = new Random(); for (int i = 0; i < numCluster; i++) { int randomNumber1 = random.Next(sourceImage.Width); int randomNumber2 = random.Next(sourceImage.Height); centroids.Add(new ClusterCentroid(randomNumber1, randomNumber2, filteredImage.GetPixel(randomNumber1, randomNumber2))); } FCM alg = new FCM(points, centroids, 2, filteredImage, (int)numericUpDown1.Value); int k = 0; do { if ((backgroundWorker.CancellationPending == true)) { e.Cancel = true; break; } else { k++; alg.J = alg.CalculateObjectiveFunction(); alg.CalculateClusterCentroids(); alg.Step(); double Jnew = alg.CalculateObjectiveFunction(); Console.WriteLine("Run method i={0} accuracy = {1} delta = {2}", k, alg.J, Math.Abs(alg.J - Jnew)); precision.Text = "Precision " + Math.Abs(alg.J - Jnew); //Format and Display the TimeSpan Value; string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", stopWatch.Elapsed.Hours, stopWatch.Elapsed.Minutes, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds / 10); duration.Text = "Duration: " + elapsedTime; SegmentedImage.Image = (Bitmap)alg.getProcessedImage; backgroundWorker.ReportProgress((100 * k) / maxIterations, "Iteration " + k); if (Math.Abs(alg.J - Jnew) < accuracy) { break; } } } while (maxIterations > k); Console.WriteLine("Done.."); //Save the Segmented Image SegmentedImage.Image = (Bitmap)alg.getProcessedImage.Clone(); alg.getProcessedImage.Save("SegmentedImage.png"); //Create a new image for each cluster in order to extratc the feature double[,] Matrix = alg.U; Bitmap[] bmapArray = new Bitmap[centroids.Count]; for (int i = 0; i < centroids.Count; i++) { bmapArray[i] = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppRgb); } for (int j = 0; j < points.Count; j++) { for (int i = 0; i < centroids.Count; i++) { ClusterPoint p = points[j]; if (Matrix[j, i] == p.ClusterIndex) { bmapArray[i].SetPixel((int)p.X, (int)p.Y, p.OriginalPixelColor); } } } //Save the image for each segmented Cluster for (int i = 0; i < centroids.Count; i++) { bmapArray[i].Save("Cluster" + i + ".png"); } imgCluster1 = (Bitmap)bmapArray[0].Clone(); imgCluster3 = (Bitmap)bmapArray[2].Clone(); imgCluster2 = (Bitmap)bmapArray[1].Clone(); pictClus1.Image = imgCluster1; pictClus2.Image = imgCluster2; pictClus3.Image = imgCluster3; // Resource Cleanup.. backgroundWorker.ReportProgress(100, "Done in " + k + "iteration."); //getDataCluster(); for (int i = 0; i < points.Count; i++) { points[i] = null; } for (int i = 0; i < centroids.Count; i++) { centroids[i] = null; } alg = null; }
public static ClusterPoint[] Clusterization(List<ClusterPoint> points, ClusterPoint[] centers) { ClusterPoint[] newCenters = new ClusterPoint[clustersAmount]; do { Array.Copy(centers, newCenters, clustersAmount); Iterate(points, centers); centers = FindCenters(points); } while (!IsClustered(centers, newCenters)); return centers; }