private void InitializeSettings() { _filepath = null; DstColorRegion = new HSVColorRegion(); MethodList = new Dictionary <string, CalculateMethod> { { "Average", new CalculateMethod { Method = HSVColorRegion.Average, Distance = HSVColorRegion.LengthSquared } }, { "Average2", new CalculateMethod { Method = HSVColorRegion.Average2, Distance = HSVColorRegion.Distance } } }; CalculateMethodKey = MethodList.Keys.First(); isBusy = false; DstColorRegion.HueStart = 0.0; DstColorRegion.HueEnd = 361.0; DstColorRegion.SaturationStart = 0.0; DstColorRegion.SaturationEnd = 101.0; DstColorRegion.ValueStart = 0.0; DstColorRegion.ValueEnd = 101.0; ClusterNum = 3; LoopUpperLimit = 10; Seed = 0; }
private void MouseMoveOnImage(object sender, MouseEventArgs e) { Point point = e.GetPosition(image); double x = point.X; double y = point.Y; BitmapSource b = (BitmapSource)image.Source; Color c = GetPixelColor((int)(x / image.ActualWidth * b.PixelWidth), (int)(y / image.ActualHeight * b.PixelHeight), b); var hsv = HSVColorRegion.RGBtoHSV(new Vector3(c.R, c.G, c.B)); currenthsv.Text = "H:" + ((int)hsv.X).ToString() + ",S:" + ((int)hsv.Y).ToString() + ",V:" + ((int)hsv.Z).ToString(); currentcolor.Background = new SolidColorBrush(c); }
private void AnalyzeImage() { Bitmap bitmap = new Bitmap(ImageFilePath); Vector3[,] hsvimage = new Vector3[bitmap.Width, bitmap.Height]; Vector3[] features = new Vector3[bitmap.Width * bitmap.Height]; int count = 0; for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { var p = bitmap.GetPixel(x, y); var tmp = HSVColorRegion.RGBtoHSV(HSVColorRegion.HSLtoRGB(new Vector3(p.GetHue(), p.GetSaturation() * 100, p.GetBrightness() * 100))); features[count] = tmp; hsvimage[x, y] = tmp; count += 1; } } var a = Clustering.KMeans(features.Where(v => DstColorRegion.IsHSVColorInRegion(v.X, v.Y, v.Z)).ToArray(), ClusterNum, LoopUpperLimit, MethodList[CalculateMethodKey].Method, MethodList[CalculateMethodKey].Distance, Seed); var center = a.Item1; var labels = a.Item2; var tmppallets = new List <Pallet>(); for (int i = 0; i < ClusterNum; i++) { var targetvec = center[i]; var targetrgb = HSVColorRegion.HSVtoRGB(targetvec); var color = System.Drawing.Color.FromArgb((byte)targetrgb.X, (byte)targetrgb.Y, (byte)targetrgb.Z); tmppallets.Add(new Pallet { H = (int)targetvec.X, S = (int)targetvec.Y, V = (int)targetvec.Z, color = color }); } Pallets = tmppallets; Bitmap dst = new Bitmap(bitmap.Width, bitmap.Height, System.Drawing.Imaging.PixelFormat.Format32bppRgb); count = 0; for (int x = 0; x < bitmap.Width; x++) { for (int y = 0; y < bitmap.Height; y++) { var colorvec = hsvimage[x, y]; if (DstColorRegion.IsHSVColorInRegion(colorvec.X, colorvec.Y, colorvec.Z)) { dst.SetPixel(x, y, Pallets[labels[count]].color); count++; } else { //var srccolor = bitmap.GetPixel(x, y); //dst.SetPixel(x, y, System.Drawing.Color.FromArgb(1, srccolor.R, srccolor.G, srccolor.B)); } } } DstBitmap = dst; }