Exemplo n.º 1
0
 public void AddColor(OurColor color, int count)
 {
     if (!HasColor(color))
     {
         _colors.Add(color, 0);
     }
     _colors[color] += count;
     Count          += count;
 }
Exemplo n.º 2
0
        public void GetColorMap(Image image)
        {
            //var result = new int[image.Width, image.Height];
            var groupedSimilarColors = new List <SimilarColors>();

            var frequencyColors = new Dictionary <OurColor, int>();

            using (var bitmap = new Bitmap(image)) {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        Color color = bitmap.GetPixel(x, y);

                        // AddColorToSimilarColors(color, groupedSimilarColors);

                        /*if (numericColor < 6000000) {
                         *  //цвет слишком темный для фона
                         *  continue;
                         * }*/

                        var ourColor = new OurColor(color);

                        if (!frequencyColors.ContainsKey(ourColor))
                        {
                            frequencyColors.Add(ourColor, 0);
                        }
                        frequencyColors[ourColor]++;
                    }
                }
            }

            List <OurColor> keys = frequencyColors.OrderByDescending(e => e.Value).Select(e => e.Key).ToList();

            AddSimilarColors(keys, frequencyColors, groupedSimilarColors);

            List <SimilarColors> sortedSimilarColors = groupedSimilarColors.OrderByDescending(e => e.Count).ToList();

            decimal minCount = sortedSimilarColors[0].Count * 0.6m;
            List <SimilarColors> probablyBackgroundColors = sortedSimilarColors.Where(e => e.Count >= minCount).ToList();

            using (var bitmap = new Bitmap(image)) {
                for (int x = 0; x < bitmap.Width; x++)
                {
                    for (int y = 0; y < bitmap.Height; y++)
                    {
                        Color color    = bitmap.GetPixel(x, y);
                        var   ourColor = new OurColor(color);
                        if (probablyBackgroundColors.Any(e => e.HasColor(ourColor)))
                        {
                            bitmap.SetPixel(x, y, Color.Red);
                        }
                    }
                }
                bitmap.Save(_fullPathToSave, ImageFormat.Jpeg);
            }
        }
Exemplo n.º 3
0
        public static bool IsSimilarColor(OurColor color1, OurColor color2, byte delta) {
            if (!IsPartInDelta(color1._red, color2._red, delta)) {
                return false;
            }
            if (!IsPartInDelta(color1._green, color2._green, delta)) {
                return false;
            }

            bool result = IsPartInDelta(color1._blue, color2._blue, delta);
            return result;
        }
Exemplo n.º 4
0
        private static void AddSimilarColors(List <OurColor> keys,
                                             Dictionary <OurColor, int> frequencyColors,
                                             List <SimilarColors> groupedSimilarColors)
        {
            for (int i = 0; i < keys.Count; i++)
            {
                OurColor ourColor  = keys[i];
                int      frequency = frequencyColors[ourColor];
                bool     isSimilar = AddColorToSimilarColors(ourColor, frequency, groupedSimilarColors);

                if (!isSimilar && groupedSimilarColors.Count < MAX_COUNT_BASE_COLORS)
                {
                    groupedSimilarColors.Add(new SimilarColors(ourColor, frequency));
                }
            }
        }
Exemplo n.º 5
0
        public static bool AddColorToSimilarColors(OurColor ourColor,
                                                   int count,
                                                   List <SimilarColors> groupedSimilarColors)
        {
            foreach (SimilarColors similarColors in groupedSimilarColors)
            {
                if (similarColors.HasColor(ourColor))
                {
                    similarColors.AddColor(ourColor, count);
                    return(true);
                }

                if (ourColor.IsSimilarColor(similarColors.BaseColor, MAX_DEVIATION))
                {
                    similarColors.AddColor(ourColor, count);
                    return(true);
                }
            }
            return(false);
        }
Exemplo n.º 6
0
 public SimilarColors(OurColor baseColor, int count)
 {
     Count     = 0;
     BaseColor = baseColor;
     AddColor(baseColor, count);
 }
Exemplo n.º 7
0
 public bool HasColor(OurColor color)
 {
     return(_colors.ContainsKey(color));
 }
Exemplo n.º 8
0
 public bool IsSimilarColor(OurColor color, byte delta) {
     return IsSimilarColor(this, color, delta);
 }