private static Bitmap GetBitmapFromBlob(Bitmap source, Blob blob) { int rX = blob.Rectangle.Width / 2; int rY = blob.Rectangle.Height / 2; Bitmap result = new Bitmap(blob.Rectangle.Width, blob.Rectangle.Height); IColorQuantizer quantizer = new MedianCutQuantizer(); for (int x = 0; x < blob.Rectangle.Width - 1; x++) { for (int y = 0; y < blob.Rectangle.Height - 1; y++) { Color color = source.GetPixel(blob.Rectangle.Location.X + x, blob.Rectangle.Location.Y + y); if (x > (rX / 2) && x < blob.Rectangle.Width - (rX / 2) && y > (rY / 2) && y < blob.Rectangle.Height - (rY / 2)) { quantizer.AddColor(color); } result.SetPixel(x, y, color); } } Color[] palette = quantizer.GetPalette(1); return(result); }
public static Bitmap MedianCut(Bitmap bmp, int intensity) { ColorImageQuantizer ciq = new ColorImageQuantizer(new MedianCutQuantizer()); IColorQuantizer quantizer = new MedianCutQuantizer(); Bitmap newImage = ciq.ReduceColors(bmp, intensity); return(newImage); }
private static ObjectStruct GetDominantColorFromBlob(Bitmap source, Blob blob) { int rX = blob.Rectangle.Width / 2; int rY = blob.Rectangle.Height / 2; // int r = blob.Rectangle.Width > 200 ? 40 : 2; int r = blob.Rectangle.Width / 4; IColorQuantizer quantizer = new MedianCutQuantizer(); for (int x = 0; x < blob.Rectangle.Width - 1; x++) { for (int y = 0; y < blob.Rectangle.Height - 1; y++) { Color color = source.GetPixel(blob.Rectangle.Location.X + x, blob.Rectangle.Location.Y + y); if (x > rX - r && x < rX + r && y > rY - r && y < rY + r) { double hue; double saturation; double value; ColorToHSV(color, out hue, out saturation, out value); Color alignedСolor = ColorFromHSV(hue, saturation, value); quantizer.AddColor(alignedСolor); } } } // int paletteLenght = blob.Rectangle.Width > 200 ? 24 : 24; 36 64 int paletteLenght = 256; Color[] color1 = quantizer.GetPalette(paletteLenght); ObjectStruct obj = new ObjectStruct(); int lenght = paletteLenght; obj.Tone = new HSVColor[lenght]; int index = 0; foreach (Color color in color1) { double hue; double saturation; double value; ColorToHSV(color, out hue, out saturation, out value); obj.Tone[index] = new HSVColor(hue, saturation, value); index++; } obj.Radius = rX; return(obj); }