static int getK(MST mst) { int clusters = 1; double size = mst.getEdgesHeap().getSize(); int distictcolors = (int)size; double cost = mst.totalCost; double brevSTDev = 0; double curSTDev = mst.STDev; double Mean = mst.Mean; Edge[] edges = mst.getEdgesHeap().Sortedges(); int j = edges.Length - 1; int i = 0; double curSTDevRed = 0, brevSTDevRed = 0; while (Math.Abs(curSTDev - brevSTDev) > 0.0001) { brevSTDev = curSTDev; if (Math.Abs(edges[i].weight - Mean) > Math.Abs(edges[j].weight - Mean)) { cost -= edges[i].weight; i++; } else { cost -= edges[j].weight; j--; } size--; Mean = cost / size; double sum = 0; for (int k = i; k <= j; k++) { sum += Math.Abs(edges[k].weight - Mean) * Math.Abs(edges[k].weight - Mean); } curSTDev = Math.Sqrt(sum / Convert.ToDouble(size - 1)); // curSTDevRed = Math.Abs(curSTDev - brevSTDev); clusters++; } if (distictcolors > clusters) { return(clusters); } return(distictcolors); }
/// <summary> /// Apply Gaussian smoothing filter to enhance the edge detection /// </summary> /// <param name="ImageMatrix">Colored image matrix</param> /// <param name="filterSize">Gaussian mask size</param> /// <param name="sigma">Gaussian sigma</param> /// <returns>smoothed color image</returns> public static RGBPixel[,] GaussianFilter1D(RGBPixel[,] ImageMatrix, int filterSize, double sigma) { /////hereeeeeeeeeeeeeee List <RGBPixelD> listRGP = findDistinctColors(ImageMatrix); //O(N^2) MST mst = new MST(listRGP); mst.prim(); //O(E log V) cLustring = new CLustring(mst.getEdgesHeap(), listRGP, mst.getMSTList()); ///AutoClustring autoClustring =new AutoClustring(listRGP); Console.WriteLine(); cLustring.buildCustring(getK(mst)); // CLustring cLustring = new CLustring(autoClustring.getListOfColors(), autoClustring.getMSTDictionary()); // cLustring.buildAutoCustring(); ///return replaceNewColors(autoClustring.autoClustring(filterSize, listRGP, listRGP.Count), ImageMatrix); //return replaceNewColors(cLustring.getMatrix(), ImageMatrix); return(ImageMatrix); }