public List<Bitmap> Apply(Bitmap srcimg)
        {
            int mw = srcimg.Width;
            int mh = srcimg.Height;
            int[] pixels = ImageUtils.BitmapToArray1DIntRGB(srcimg);
            ColorHistogram colorHist = new ColorHistogram(pixels);
            int[] color_array = colorHist.getColorArray();
            List<bool[,]> img_list = new List<bool[,]>();
            for (int i = 0; i < color_array.Length; i++)
            {
                img_list.Add(new bool[mh, mw]);
                fg_count_list.Add(0);
                fg_idx_color_hash.Add(i, color_array[i]);
                fg_color_idx_hash.Add(color_array[i], i);
            }

            for(int i=0;i<mw;i++)
                for (int j = 0; j < mh; j++)
                {
                    int idx = (int)fg_color_idx_hash[pixels[j * mw + i]];
                    (img_list[idx])[j,i] = true;
                    fg_count_list[idx]++;
                }

            List<Bitmap> bitmap_list = new List<Bitmap>();
            for (int i = 0; i < img_list.Count; i++)
                bitmap_list.Add(ImageUtils.ArrayBool2DToBitmap(img_list[i]));
            return bitmap_list;
        }
        public static int countUniqueColorNumber(String fullFilePath)
        {
            return 40;

            Bitmap srcimg = new Bitmap(fullFilePath);
            int[] pixels = ImageUtils.BitmapToArray1DIntRGB(srcimg);
            ColorHistogram colorHist = new ColorHistogram(pixels);
            int[] color_array = colorHist.getColorArray();

            return color_array.Length;
        }
        public void getRGBData(ColorHistogram colorHist, double[][] data)
        {
            int size = colorHist.getNumberOfColors();

            for (int x = 0; x < size; x++)
            {
                data[x] = new double[3];
                int rgb = colorHist.getColor(x);
                data[x][0] = ((rgb & 0xFF0000) >> 16);
                data[x][1] = ((rgb & 0xFF00) >> 8);
                data[x][2] = (rgb & 0xFF);
            }
        }
        public void Apply(string intermediatePath)
        {
            //tergetURL = "http://secure.futureclimateinfo.com/api/wms?request=GetMap&BBOX=314100,543800,314600,544300&SRS=EPSG:27700&WIDTH=1056&HEIGHT=1056&LAYERS=nls-sixinch&STYLES=&FORMAT=image/png";
            //tergetURL = "http://secure.futureclimateinfo.com/api/wms?SERVICE=WMS&VERSION=1.1.1&request=GetMap&BBOX=314100,543800,314600,544300&SRS=EPSG:27700&WIDTH=1056&HEIGHT=1056&LAYERS=nls-sixinch&STYLES=&FORMAT=image/pnG";
            //tergetURL = "http://secure.futureclimateinfo.com/api/wms?request=GetMap&BBOX=589400,194000,589900,194500&SRS=EPSG:27700&WIDTH=1056&HEIGHT=1056&LAYERS=nls-sixinch&STYLES=&FORMAT=image/png";
            //tergetURL = "http://secure.futureclimateinfo.com/api/wms?request=GetMap&BBOX=589400,194000,590400,195000&SRS=EPSG:27700&WIDTH=947&HEIGHT=947&LAYERS=nls-sixinch&STYLES=&FORMAT=image/pnG";
            //MapServerParameters.targetURL = "http://secure.futureclimateinfo.com/api/wms?&&SERVICE=WMS&VERSION=1.1.1&REQUEST=GetMap&BBOX=597528,209822,598528,210822&SRS=EPSG:27700&WIDTH=1500&HEIGHT=1500&LAYERS=nls-sixinch:&STYLES=&FORMAT=image/png&DPI=96&MAP_RESOLUTION=96&FORMAT_OPTIONS=dpi:96&TRANSP...
            Bitmap SourceImage = ConnectToMapServer.ConnectToWMS(MapServerParameters.targetURL);

            SourceImage.Save(intermediatePath+StraboParameters.sourceMapFileName);
            ColorHistogram ch = new ColorHistogram();

            if (!ch.CheckImageColors(SourceImage,2))
            {
                Exception e = new Exception("The returned image is invalid, it has less than two colors.");
                Log.WriteLine("The returned image is invalid, it has less than two colors.");
                throw e;
            }
        }
        public Dictionary<int, Color> ApplyFast(String inputName, String saveName)
        {
            Dictionary<int, Color> tbl = new Dictionary<int, Color>();

            Bitmap srcimg = new Bitmap(inputName);

            List<int> fg_count_list = new List<int>();
            Hashtable fg_color_idx_hash = new Hashtable();

            int mw = srcimg.Width;
            int mh = srcimg.Height;
            int[] pixels = ImageUtils.BitmapToArray1DIntRGB(srcimg);
            ColorHistogram colorHist = new ColorHistogram(pixels);
            int[] color_array = colorHist.getColorArray();
            List<int[,]> img_list = new List<int[,]>();
            List<Bitmap> bitmap_list = new List<Bitmap>();
            for (int i = 0; i < color_array.Length; i++)
            {
                bitmap_list.Add(new Bitmap(mw, mh));
                img_list.Add(new int[mh, mw]);
                fg_count_list.Add(0);
                fg_color_idx_hash.Add(color_array[i], i);
                tbl.Add(i, getColor(color_array[i]));
            }

            for (int i = 0; i < mw; i++)
            {
                for (int j = 0; j < mh; j++)
                {
                    int idx = (int)fg_color_idx_hash[pixels[j * mw + i]];
                    bitmap_list[idx].SetPixel(i, j, getColor(pixels[j * mw + i]));
                    fg_count_list[idx]++;
                }
            }

            for (int k = 0; k < img_list.Count; k++)
            {
                bitmap_list[k].Save(string.Format(saveName, k));
            }

            return tbl;
        }
        public string Apply(int k, string fn, string outImagePath)
        {
            Bitmap srcimg = new Bitmap(fn);
            pixels = ImageUtils.BitmapToArray1DIntRGB(srcimg);
            ColorHistogram colorHist = new ColorHistogram(pixels, false);
            int size = colorHist.getNumberOfColors();
            if (size <= k)
                srcimg.Save(outImagePath, ImageFormat.Png);
            else
            {
                double[][] data = new double[size][];

                getRGBData(colorHist, data);
                ClusterCollection clusters;
                KMeansParallel kMeans = new KMeansParallel();
                clusters = kMeans.ClusterDataSet(k, data);
                PaintRGB(srcimg, kMeans, clusters).Save(outImagePath, ImageFormat.Png);
            }
            return outImagePath;
        }
 public void ApplySaveInd(Bitmap srcimg, string dir, string fn)
 {
     int mw = srcimg.Width;
     int mh = srcimg.Height;
     int[] pixels = ImageUtils.BitmapToArray1DIntRGB(srcimg);
     ColorHistogram colorHist = new ColorHistogram(pixels);
     int[] color_array = colorHist.getColorArray();
     //List<bool[,]> img_list = new List<bool[,]>();
     //for (int i = 0; i < color_array.Length; i++)
     //{
     //    img_list.Add(new bool[mh, mw]);
     //    fg_count_list.Add(0);
     //    fg_idx_color_hash.Add(i, color_array[i]);
     //    fg_color_idx_hash.Add(color_array[i], i);
     //}
     for (int c = 0; c < color_array.Length; c++)
     {
         int rgb = color_array[c];
         bool[,] bool_img = new bool[mh, mw];
         for (int i = 0; i < mw; i++)
             for (int j = 0; j < mh; j++)
             {
                 if(pixels[j * mw + i]==rgb)
                     bool_img[j, i] = true;
             }
         ImageUtils.ArrayBool2DToBitmap(bool_img).Save(dir + fn+c+".png");
     }
 }
 public void GenerateColorPalette(string fn, List<int> qnum_list)
 {
     this.qnum_list = qnum_list;
     srcimg = new Bitmap(fn);
     width = srcimg.Width;
     height = srcimg.Height;
     pixels = ImageUtils.BitmapToArray1DIntRGB(srcimg);
     for (int i = 0; i < qnum_list.Count; i++)
     {
         color_table_list.Add( new Hashtable());
         ini_color_table_list.Add(new Hashtable());
     }
     ColorHistogram colorHist = new ColorHistogram(pixels, false);
     int K = colorHist.getNumberOfColors();
     findRepresentativeColors(K, colorHist);
 }
        void findRepresentativeColors(int K, ColorHistogram colorHist)
        {
            imageColors = new ColorNode[K];
            for (int i = 0; i < K; i++)
            {
                int rgb = colorHist.getColor(i);
                int cnt = colorHist.getCount(i);
                imageColors[i] = new ColorNode(rgb, cnt);
            }

            //if (K <= qnum_list[0]) // image has fewer colors than Kmax
            //    rCols = imageColors;
            //else
            {
                ColorBox initialBox = new ColorBox(0, K - 1, 0, imageColors);
                List<ColorBox> colorSet = new List<ColorBox>();
                colorSet.Add(initialBox);
                int k = 1;
                for (int i = 0; i < qnum_list.Count; i++)
                {
                    int Kmax = qnum_list[i];
                    bool done = false;
                    while (k < Kmax && !done)
                    {
                        ColorBox nextBox = findBoxToSplit(colorSet);
                        if (nextBox != null)
                        {
                            ColorBox newBox = nextBox.splitBox();
                            colorSet.Add(newBox);
                            k = k + 1;
                        }
                        else
                        {
                            done = true;
                        }
                    }
                    quantColors_list.Add(averageColors(colorSet,i));
                }
            }
            colorHist = null;
            GC.Collect();
        }
Example #10
0
        public void YIQData(ColorHistogram colorHist, double[][] data)
        {
            int size = colorHist.getNumberOfColors();

            for (int x = 0; x < size; x++)
            {
                data[x] = new double[3];
                int rgb = colorHist.getColor(x);
                double[] yiq = RGB2YIQ(rgb);

                for (int y = 0; y < 3; y++)
                {
                    data[x][y] = yiq[y];
                }
            }
        }
 public void GenerateColorFromNegativeExamples(Bitmap srcimg)
 {
     ColorHistogram color_histogram = new ColorHistogram();
     HashSet<int> color = color_histogram.GetColorHashSet(srcimg);
     foreach (int c in color)
         negtive_color.Add(c);
 }
        public double[] getLayerColorHSV(string layerPath, int n)
        {
            Bitmap srcimg = new Bitmap(layerPath);
            int[] pixels = ImageUtils.BitmapToArray1DIntRGB(srcimg);
            ColorHistogram colorHist = new ColorHistogram(pixels);
            int[] color_array = colorHist.getColorArray();

            String st = color_array[1].ToString();

            Color c = getColor(int.Parse(st));
            //Hsv hsv = new Hsv(c.GetHue(),c.GetSaturation(),c.GetBrightness());
            //double[] f = { hsv.Hue, hsv.Satuation, hsv.Value };
            //double[] f = { c.GetHue(), c.GetSaturation() ,c.GetBrightness() };

            int max = Math.Max(c.R, Math.Max(c.G, c.B));
            int min = Math.Min(c.R, Math.Min(c.G, c.B));

            double hue = c.GetHue();
            double saturation = (max == 0) ? 0 : 1d - (1d * min / max);
            double value = max / 255d;

            double[] f = { hue, saturation * 100, value * 100 };
            return f;
        }