private static int distance(ColorRGBModel a, ColorRGBModel b) { int dr = (a.R - b.R) * (a.R - b.R); int db = (a.B - b.B) * (a.B - b.B); int dg = (a.G - b.G) * (a.G - b.G); return((int)Math.Sqrt(dr + db + dg)); }
private static int distance(ColorRGBModel a, int k) { int dr = (a.R - k) * (a.R - k); int db = (a.B - k) * (a.B - k); int dg = (a.G - k) * (a.G - k); return((int)Math.Sqrt(dr + db + dg)); }
private static void newMean(int k, byte[,] kv, List <ColorRGBModel>[] cols, ref bool IsQuit) { for (int i = 0; i < k; i++) { ColorRGBModel kMeanValue = mean(cols[i]); if (kv[i, 0] == kMeanValue.R && kv[i, 1] == kMeanValue.G && kv[i, 2] == kMeanValue.B) { IsQuit = true; } else { kv[i, 0] = kMeanValue.R; kv[i, 1] = kMeanValue.G; kv[i, 2] = kMeanValue.B; IsQuit = false; } } }
private static void KAddUp(int k, int width, int height, byte[, ,] martix, out List <ColorRGBModel>[] cols) { cols = new List <ColorRGBModel> [k]; for (int i = 0; i < k; i++) { cols[i] = new List <ColorRGBModel>(); } for (int y = 0; y < height; y++) { for (int x = 0; x < width; x++) { int KPosition = martix[y, x, 0]; ColorRGBModel value = new ColorRGBModel(); value.R = martix[y, x, 1]; value.G = martix[y, x, 2]; value.B = martix[y, x, 3]; cols[KPosition].Add(value); } } }
private static ColorRGBModel mean(List <ColorRGBModel> collections) { ColorRGBModel res = new ColorRGBModel(); if (!(collections.Count > 0)) { return(res); } int r = 0, g = 0, b = 0; foreach (ColorRGBModel c in collections) { r += c.R; g += c.G; b += c.B; } res.R = (byte)(r / collections.Count); res.G = (byte)(g / collections.Count); res.B = (byte)(b / collections.Count); return(res); }