private void btnGMMND_Click(object aoSender, EventArgs aoArg) { try { int kiK = int.Parse(txtNumClusters.Text); // number of clusters int kiDim = 3; // number of dimensions for data Matrix koX = new Matrix(this.voOrig.Height * this.voOrig.Width, 3); for (int i = 0; i < this.voOrig.Height; i++) { for (int j = 0; j < this.voOrig.Width; j++) { Color koC = this.voOrig.GetPixel(j, i); koX[(i * this.voOrig.Width) + j, 0] = koC.R; koX[(i * this.voOrig.Width) + j, 1] = koC.G; koX[(i * this.voOrig.Width) + j, 2] = koC.B; } } voGmmnd = new GMM_NDim(kiK, kiDim, koX); voGmmnd.ComputeGMM_ND(); this.voLB.DataSource = null; this.voCluster.Clear( ); for (int i = 0; i < kiK; i++) { this.voCluster.Add(String.Format("Cluster {0}", i)); } this.voLB.DataSource = this.voCluster; } catch (Exception koEx) { MessageBox.Show(koEx.Message); } }
private void voBtnSwarm_Click(object sender, EventArgs e) { int kiI; Task <GMM_NDim>[] koTask = new Task <GMM_NDim> [4]; Task koFinal; this.voSwarmGMM = new List <GMM_NDim>( ); for (kiI = 0; kiI < koTask.Length; kiI++) { koTask[kiI] = Task.Factory.StartNew <GMM_NDim>((aoContext) => { int kiK = (( int )aoContext) + 2; int kiDim = 3; // number of dimensions for data Bitmap koBmp; lock (this.voOrig){ koBmp = ( Bitmap )this.voOrig.Clone( ); } Matrix koX = new Matrix(koBmp.Height * koBmp.Width, 3); GMM_NDim koRes; for (int i = 0; i < koBmp.Height; i++) { for (int j = 0; j < koBmp.Width; j++) { Color koC = koBmp.GetPixel(j, i); koX[(i * koBmp.Width) + j, 0] = koC.R; koX[(i * koBmp.Width) + j, 1] = koC.G; koX[(i * koBmp.Width) + j, 2] = koC.B; } } koRes = new GMM_NDim(kiK, kiDim, koX); koRes.ComputeGMM_ND( ); koRes.VdInertia = koRes.MInertia(koBmp); return(koRes); }, kiI); } koFinal = Task.Factory.ContinueWhenAll(koTask, (aoTask) => { Console.WriteLine(aoTask.Length.ToString( ) + " tasks"); for (kiI = 0; kiI < aoTask.Length; kiI++) { this.voSwarmGMM.Add(aoTask[kiI].Result); } }); koFinal.Wait( ); this.voSwarmGMM.Sort( ); this.voStrSwarmGMM = new List <string>( ); for (int i = 0; i < this.voSwarmGMM.Count; i++) { this.voStrSwarmGMM.Add(String.Format("K = {0}", this.voSwarmGMM[i].ViK)); } this.voResult.DataSource = null; this.voResult.DataSource = this.voStrSwarmGMM; }
public double prepareGMMND(int clus, int dim, Matrix X, Particle pt) { myGMM = new GMM_NDim(clus, dim, X); //One mu for each dimenssion //The length og mu is the number of clusters int[] xMu = new int[clus]; int[] yMu = new int[clus]; for (int k = 0; k < clus; k++) { xMu[k] = (int)pt.Xx[k]; yMu[k] = (int)pt.Xy[k]; } myGMM.ComputeGMM_ND(xMu, yMu); double[] Xsi = new double[clus]; double[] Ysi = new double[clus]; for (int k = 0; k < clus; k++) { Xsi[k] = Math.Sqrt(Math.Abs(myGMM.sigma[k][0, 0])); Ysi[k] = Math.Sqrt(Math.Abs(myGMM.sigma[k][0, 1])); } int[] particlesInCLuster = new int[clus]; //DETERMINE WHAT CLUSTER EACH POINT BELONGS TO // determine class membership i.e., which point belongs to which cluster PList = new List <MyPoint>(); for (int i = 0; i < X.Rows; i++) //looping throught all the rows = #points of Gamma -- each col = probability for each cluster { // Gamma matrix has the probabilities for a data point for its membership in each cluster double[] probabs = new double[clus]; int cnum = 0; double maxprobab = myGMM.Gamma[i, 0]; for (int m = 0; m < clus; m++) // Going throught the cols --- each col has probability for one cluster { if (myGMM.Gamma[i, m] > maxprobab) //checking for the cluster with more probability { cnum = m; // data i belongs to cluster m maxprobab = myGMM.Gamma[i, m]; } } MyPoint pnt = new MyPoint(X[i, 0], X[i, 1], cnum); PList.Add(pnt); } double fValue = FunctionToSolve(Xsi, Ysi, particlesInCLuster); return(fValue); }
private void btnGMMImage_Click(object sender, EventArgs e) { pic1.Load("Tennis Ball 620.jpg"); bmp = new Bitmap("Tennis Ball 620.jpg"); imageDataList = new List <ImagePoints>(bmp.Width * bmp.Height); int k = int.Parse(txtNumClusters.Text); int dim = 3; imageDataList = InitializeImageData(bmp, k); Matrix imgMatrix = new Matrix(bmp.Width * bmp.Height, dim); for (int i = 0; i < (bmp.Width * bmp.Height); i++) { imgMatrix[i, 0] = imageDataList[i].red; imgMatrix[i, 1] = imageDataList[i].green; imgMatrix[i, 2] = imageDataList[i].blue; } GMM_NDim gmmnd = new GMM_NDim(k, dim, imgMatrix, bmp.Width * bmp.Height); gmmnd.ComputeGMM_ND(); // determine class membership i.e., which point belongs to which cluster imageDataList = new List <ImagePoints>(); for (int i = 0; i < imgMatrix.Rows; i++) { // Gamma matrix has the probabilities for a data point for its membership in each cluster double[] probabs = new double[k]; int cnum = 0; double maxprobab = gmmnd.Gamma[i, 0]; for (int m = 0; m < k; m++) { if (gmmnd.Gamma[i, m] > maxprobab) { cnum = m; // data i belongs to cluster m maxprobab = gmmnd.Gamma[i, m]; } } ImagePoints imgpt = new ImagePoints { clusterID = cnum, red = gmmnd.X[i, 0], green = gmmnd.X[i, 1], blue = gmmnd.X[i, 2] }; imageDataList.Add(imgpt); } //MyImageProc.DrawClusters(pic1, imageDataList, 1, k); //redraw image from rgb values MessageBox.Show("img list len = " + imageDataList.Count.ToString() + " img width = " + bmp.Width.ToString() + " img height = " + bmp.Height.ToString()); //MessageBox.Show("new image done"); }
private void voResult_SelectedIndexChanged(object aoSender, EventArgs aoArgs) { ListBox koLB = aoSender as ListBox; if (koLB.SelectedIndex >= 0) { this.voCluster.Clear( ); for (int i = 0; i < this.voSwarmGMM[koLB.SelectedIndex].ViK; i++) { this.voCluster.Add(String.Format("Cluster {0}", i)); } this.voLB.DataSource = null; this.voLB.DataSource = this.voCluster; this.voGmmnd = this.voSwarmGMM[koLB.SelectedIndex]; } else { this.voPB.Image = null; this.voPB.Image = this.voOrig; } }
private void btnGMMND_Click(object sender, EventArgs e) { int k = int.Parse(txtNumClusters.Text); // number of clusters int dim = 3; // number of dimensions for data //int datSize = 20; //System.Diagnostics.Debug.WriteLine("reach1"); Matrix X; List <MyPoint> PList; PList = InitializeData2(); //System.Diagnostics.Debug.WriteLine("reach2" + dataSize + PList.Count); X = new Matrix(dataSize, dim); for (int i = 0; i < PList.Count; i++) { X[i, 0] = PList[i].red; X[i, 1] = PList[i].blue; X[i, 2] = PList[i].green; } //System.Diagnostics.Debug.WriteLine("reach3"); GMM_NDim gmmnd; gmmnd = new GMM_NDim(k, dim, X); //System.Diagnostics.Debug.WriteLine("reach4"); gmmnd.ComputeGMM_ND2(); //System.Diagnostics.Debug.WriteLine("reach14"); #region MyRegion /* * determine class membership i.e., which point belongs to which cluster * PList = new List<MyPoint>(); * for (int i = 0; i < X.Rows; i++) * { * // Gamma matrix has the probabilities for a data point for its membership in each cluster * double[] probabs = new double[k]; * int cnum = 0; * double maxprobab = gmmnd.Gamma[i, 0]; * for (int m = 0; m < k; m++) * { * if (gmmnd.Gamma[i, m] > maxprobab) * { * cnum = m; // data i belongs to cluster m * maxprobab = gmmnd.Gamma[i, m]; * } * } * MyPoint pt = new MyPoint { ClusterId = cnum, X = X[i, 0], Y = X[i, 1] }; * PList.Add(pt); * }*/ #endregion //System.Diagnostics.Debug.WriteLine("reach15"); bmpProc = new Bitmap(bmpOrig.Width, bmpOrig.Height); bmpProc2 = new Bitmap(bmpOrig.Width, bmpOrig.Height); bmpProc3 = new Bitmap(bmpOrig.Width, bmpOrig.Height); // determine class membership i.e., which point belongs to which cluster //List<MyPoint> PList2 = new List<MyPoint>(); for (int i = 0; i < X.Rows; i++) { int cnum = 0; double maxprobab = gmmnd.Gamma[i, 0]; for (int m = 0; m < k; m++) { if (gmmnd.Gamma[i, m] > maxprobab) { cnum = m; // data i belongs to cluster m maxprobab = gmmnd.Gamma[i, m]; } } if (cnum == 1) { bmpProc.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb((int)PList[i].red, (int)PList[i].green, (int)PList[i].blue)); bmpProc2.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb(255, 255, 255)); bmpProc3.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb(255, 255, 255)); } else if (cnum == 2) { bmpProc2.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb((int)PList[i].red, (int)PList[i].green, (int)PList[i].blue)); bmpProc.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb(255, 255, 255)); bmpProc3.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb(255, 255, 255)); } else { bmpProc3.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb((int)PList[i].red, (int)PList[i].green, (int)PList[i].blue)); bmpProc.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb(255, 255, 255)); bmpProc2.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb(255, 255, 255)); } #region MyRegion //MyPoint pt; ; //if ((cnum == 1) || (cnum == 0)) //{ // //pt = new MyPoint { ClusterId = cnum, red = 255, green = 255, // // blue = 255, X=PList[i].X, Y=PList[i].Y}; // bmpProc.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb(255, 255, 255)); //} //else //{ // // pt = new MyPoint { ClusterId = cnum, red = X[i, 0], green = X[i, 1], // // blue = X[i, 2], X = PList[i].X, Y = PList[i].Y }; // //bmpProc.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb((int)X[i, 0], (int)X[i, 1], (int)X[i, 2])); // bmpProc.SetPixel((int)PList[i].X, (int)PList[i].Y, Color.FromArgb((int)PList[i].red, (int)PList[i].green, (int)PList[i].blue)); //} //pic1.Image = bmpProc; //PList2.Add(pt); #endregion } pic1.Image = bmpProc; pic2.Image = bmpProc2; pic3.Image = bmpProc3; //System.Diagnostics.Debug.WriteLine("reach16"); //System.Diagnostics.Debug.WriteLine("reach17"); }
private void btnGMMND_Click(object sender, EventArgs e) { int k = int.Parse(txtNumClusters.Text); // number of clusters int dim = 2; // number of dimensions for data //int datSize = 20; // read 2-D data //FileInfo fi = new FileInfo("d:\\csharp2016\\clusterdata\\data2D.txt"); //FileInfo fi = new FileInfo("d:\\csharp2016\\clusterdata\\cdata1.txt"); //StreamReader sr = new StreamReader(fi.Open(FileMode.Open, FileAccess.Read)); //string sline = sr.ReadLine(); //int datSize = 1; //while (sline != null) //{ // datSize++; // sline = sr.ReadLine(); //} //sr.Close(); //sr = new StreamReader(fi.Open(FileMode.Open, FileAccess.Read)); //sline = sr.ReadLine(); //Matrix X = new Matrix(datSize, dim); //int count = 0; //while (sline != null) //{ // string[] parts = sline.Split(new char[] { '\t', ' ' }); // X[count, 0] = double.Parse(parts[1].Trim()); // X[count, 1] = double.Parse(parts[2].Trim()); // count++; // sline = sr.ReadLine(); //} //sr.Close(); int datSize = 1000; List <MyPoint> PList = InitializeData(datSize); Mapack.Matrix X = new Matrix(datSize, dim); for (int i = 0; i < PList.Count; i++) { X[i, 0] = PList[i].X; X[i, 1] = PList[i].Y; } GMM_NDim gmmnd = new GMM_NDim(k, dim, X, dataSize); gmmnd.ComputeGMM_ND(); // determine class membership i.e., which point belongs to which cluster PList = new List <MyPoint>(); for (int i = 0; i < X.Rows; i++) { // Gamma matrix has the probabilities for a data point for its membership in each cluster double[] probabs = new double[k]; int cnum = 0; double maxprobab = gmmnd.Gamma[i, 0]; for (int m = 0; m < k; m++) { if (gmmnd.Gamma[i, m] > maxprobab) { cnum = m; // data i belongs to cluster m maxprobab = gmmnd.Gamma[i, m]; } } MyPoint pt = new MyPoint { ClusterId = cnum, X = X[i, 0], Y = X[i, 1] }; PList.Add(pt); } MyImageProc.DrawClusters(pic1, PList, 1, k); }
//CREATING CLUSTERS AND SHOWING MEANS FOR EACH CLUSTER private void btnCompute_Click(object sender, EventArgs e) { if (uploaded == true) { Stopwatch sw = new Stopwatch(); try { k = int.Parse(txtClusters.Text); //getting number of clusters } catch { MessageBox.Show("Wrong input."); } //sw.Start(); data = new DataPoints(picture, picture.Width, picture.Height); PList = new List <MyPoint>(data.CreatePoints()); X = new Matrix(data_Size, dimenssions); for (int i = 0; i < PList.Count; i++) { X[i, 0] = PList[i].Red; X[i, 1] = PList[i].Green; X[i, 2] = PList[i].Blue; } myGmm = new GMM_NDim(k, dimenssions, X); sw.Start(); myGmm.ComputeGMM_ND(); sw.Stop(); MessageBox.Show("Time elapsed in ms: " + sw.ElapsedMilliseconds); // determine class membership i.e., which point belongs to which cluster PList = new List <MyPoint>(); for (int i = 0; i < X.Rows; i++) //looping throught all the rows = #points of Gamma -- each col = probability for each cluster { // Gamma matrix has the probabilities for a data point for its membership in each cluster double[] probabs = new double[k]; int cnum = 0; double maxprobab = myGmm.Gamma[i, 0]; for (int m = 0; m < k; m++) // Going throught the cols --- each col has probability for one cluster { if (myGmm.Gamma[i, m] > maxprobab) //checking for the cluster with more probability { cnum = m; // data i belongs to cluster m maxprobab = myGmm.Gamma[i, m]; } } MyPoint pt = new MyPoint(X[i, 0], X[i, 1], X[i, 2], cnum); //X[i,0] = RED X[i,1] = GREEN X[i,2] = BLUE PList.Add(pt); } computed = true; showMeans(); } else { MessageBox.Show("You need to upload an image first."); } }