Esempio n. 1
0
        /// <summary>
        /// Gets all DistinctColors from 2D array and fills Dictionary of MST with all of them
        /// </summary>
        /// <param name="Buffer">Image array</param>
        /// <param name="distinctColors"> Dictionary that contains MinSpanningTree</param>
        /// <param name="distinctHelper">Helper Dictionary for keeping struct values concatenated as integer (Key)
        /// and struct itself as value</param>
        /// <param name="visited">Dictionary that checks if nodes are visited in MinSpanningTree</param>
        /// <param name="color">Dictionary key</param>
        /// <param name="minVertix">Any node that we start Minimum Spaning from</param>
        /// <returns>number DistinctColors</returns>
        /// Time Complexity: O(N^2)
        /// Space Complexity: N
        public static int FindDistinctColors(RGBPixel[,] Buffer, ref Dictionary <int, KeyValuePair <int, double> > distinctColors,
                                             ref Dictionary <int, RGBPixel> distinctHelper, ref Dictionary <int, bool> visited,
                                             ref int color, ref KeyValuePair <int, KeyValuePair <int, double> > minVertix)
        {
            int noDistinctColors = 0;                                 //O(1)
            int imageWidth       = ImageOperations.GetWidth(Buffer);  //O(1)
            int imageHeight      = ImageOperations.GetHeight(Buffer); //O(1)

            //Outer for loop approaches an O(N) operation, N is the image height
            for (int i = 0; i < imageHeight; ++i)
            {
                //Inner for loop approaches an O(N) operation, N is the image width
                for (int j = 0; j < imageWidth; ++j)
                {
                    RGBPixel node = Buffer[i, j]; //O(1)
                    //gets unique int for each color to be used as dictionary key by using Cantor Pairing
                    color = CantorPairing(node.red, node.green, node.blue);
                    if (!distinctHelper.ContainsKey(color)) //ContainsKey() approaches an O(1) operation
                    {
                        //Add() approaches an O(1) operation if Count is less than the capacity,
                        //guaranteed to run in O(1) as map is initialized with max capacity
                        distinctColors.Add(color, new KeyValuePair <int, double>(0, double.MaxValue)); //node initialized with max distance
                        distinctHelper.Add(color, node);                                               //O(1)
                        visited.Add(color, false);                                                     //node initially not visited O(1)

                        noDistinctColors++;                                                            //O(1)
                    }
                }
            }
            //to avoid using Dic.First(), Vertex to start MST from is generated here
            minVertix = new KeyValuePair <int, KeyValuePair <int, double> >(color, new KeyValuePair <int, double>(0, double.MaxValue)); //O(1)
            return(noDistinctColors);                                                                                                   //O(1)
        }
        public static RGBPixel calculateWeight(List <RGBPixel> palette, RGBPixel myCurrentPixel)
        {
            RGBPixel returnPixel = myCurrentPixel;
            double   weight = 10000000;
            int      r1, g1, b1;
            int      r2, g2, b2;

            for (int i = 0; i < palette.Count; i++)
            {
                r1 = palette[i].red;
                g1 = palette[i].green;
                b1 = palette[i].blue;
                r2 = myCurrentPixel.red;
                g2 = myCurrentPixel.green;
                b2 = myCurrentPixel.blue;
                double eq = Math.Sqrt((r2 - r1) * (r2 - r1) + (g2 - g1) * (g2 - g1) + (b2 - b1) * (b2 - b1));

                if (eq < weight)
                {
                    returnPixel = palette[i];
                    weight      = eq;
                }
            }
            return(returnPixel);
        }
        public int ColorPalette(int node, ref int red, ref int green, ref int blue, ref List <int> ClusterNodes)
        {
            RGBPixel RGBValue = Util.GetRGBPixel(ImageQuantizer.distinctColors[node]);

            red   += Convert.ToInt32(RGBValue.red);
            green += Convert.ToInt32(RGBValue.green);
            blue  += Convert.ToInt32(RGBValue.blue);
            ClusterNodes.Add(node);
            visited[node] = 1;
            int ans = 0;

            for (int i = 0; i < adj[node].Count; i++)
            {
                int v = adj[node][i];

                if (edgeTo[node].from == v && edgeTo[node].cost == -1 || edgeTo[v].from == node && edgeTo[v].cost == -1)
                {
                    continue;
                }

                if (visited[v] == 0)
                {
                    ans += 1 + ColorPalette(adj[node][i], ref red, ref green, ref blue, ref ClusterNodes);
                }
            }
            return(ans);
        }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;
                ImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                Graph constructed = new Graph();

                //constructed = ALL_FUNCTIONS.construct_ALL(ImageMatrix);
                //Graph MST=ALL_FUNCTIONS.set_MST(constructed);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
                int rw = ImageMatrix.GetLength(0), cl = ImageMatrix.GetLength(1);
                ImageMatrix_Tmp = new RGBPixel[rw, cl];
            }
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();

            distinct_colors = ALL_FUNCTIONS.get_distinct(ref ImageMatrix);
            //ALL_FUNCTIONS.test();
            ALL_FUNCTIONS.Set_Mst();
            // to handl if user input more than one cluster in same image and make operation on original image
            ImageMatrix_Tmp = ImageMatrix.Clone() as RGBPixel[, ];
        }
        private void btnGaussSmooth_Click(object sender, EventArgs e)
        {
            int.TryParse(ClustorBox.Text, out K);

            if (string.IsNullOrWhiteSpace(ClustorBox.Text))
            {
                MessageBox.Show("Please enter number of cluster !");
            }
            else if (int.Parse(ClustorBox.Text) > distinct_colors || int.Parse(ClustorBox.Text) < 1)
            {
                MessageBox.Show("Invalid number of clusters !");
            }
            else
            {
                List <List <RGBPixel> > groups = new List <List <RGBPixel> >();
                RGBPixel[, ,] Update = new RGBPixel[256, 256, 256];
                double sigma    = double.Parse(txtGaussSigma.Text);
                int    maskSize = (int)nudMaskSize.Value;
                ALL_FUNCTIONS.Build_tree();
                ALL_FUNCTIONS.buildAdjacencyList();
                ALL_FUNCTIONS.CutEdges(K);
                groups = ALL_FUNCTIONS.cluster(K);
                ALL_FUNCTIONS.GetPalette(ref groups);
                Update = ALL_FUNCTIONS.UpdateColor(ref groups);
                ALL_FUNCTIONS.UpdatedMatrix(Update, ImageMatrix);
                ImageMatrix = ImageOperations.GaussianFilter1D(ImageMatrix, maskSize, sigma);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox2);
                // Get the original image after make edition on it ..
                ImageMatrix = ImageMatrix_Tmp.Clone() as RGBPixel[, ];
            }
        }
        // Calculate average color for cluster
        public static void Calculate_average(List <RGBPixel> clue) // o(d)
        {
            long rr = 0, gg = 0, bb = 0;

            foreach (RGBPixel i in clue)
            {
                rr += i.red;
                gg += i.green;
                bb += i.blue;
            }

            RGBPixel tmp = new RGBPixel(0, 0, 0);

            if (clue.Count != 0)
            {
                rr = rr / clue.Count;
                gg = gg / clue.Count;
                bb = bb / clue.Count;

                tmp.red   = (byte)rr;
                tmp.green = (byte)gg;
                tmp.blue  = (byte)bb;
            }
            for (int i = 0; i < clue.Count; i++)
            {
                GlobalData.arrStore[clue[i].red, clue[i].green, clue[i].blue] = tmp;
            }
        }
 public static void setData()
 {
     height   = ImageOperations.GetHeight(MainForm.ImageMatrix);
     width    = ImageOperations.GetWidth(MainForm.ImageMatrix);
     arrMP    = new bool[256, 256, 256];
     arrStore = new RGBPixel[256, 256, 256];
 }
Esempio n. 8
0
        public void ColorsOfEachCluster()
        {
            Palette = new List <RGBPixel>();
            RGBPixel color = new RGBPixel();

            for (int i = 0; i < NewColors.Count; i++)
            {
                int red   = 0;
                int blue  = 0;
                int green = 0;

                int groupSize = NewColors[i].Count;
                for (int j = 0; j < groupSize; j++)
                {
                    red   += ImageUtilities.DistinctColours[NewColors[i][j]].red;
                    blue  += ImageUtilities.DistinctColours[NewColors[i][j]].blue;
                    green += ImageUtilities.DistinctColours[NewColors[i][j]].green;
                }
                red         = red / NewColors[i].Count;
                blue        = blue / NewColors[i].Count;
                green       = green / NewColors[i].Count;
                color.red   = (byte)red;
                color.blue  = (byte)blue;
                color.green = (byte)green;
                Palette.Add(color);
            }
        }
 private void CalcDistColorsWithApproximation()
 {
     for (int i = 0; i < height; ++i)
     {
         for (int j = 0; j < width; ++j)
         {
             if (MainForm.reduceColors)
             {
                 quantizedImageMatrix[i, j].red   /= 10;
                 quantizedImageMatrix[i, j].red   *= 10;
                 quantizedImageMatrix[i, j].red   += 5;
                 quantizedImageMatrix[i, j].blue  /= 10;
                 quantizedImageMatrix[i, j].blue  *= 10;
                 quantizedImageMatrix[i, j].blue  += 5;
                 quantizedImageMatrix[i, j].green /= 10;
                 quantizedImageMatrix[i, j].green *= 10;
                 quantizedImageMatrix[i, j].green += 5;
             }
             RGBPixel pix      = quantizedImageMatrix[i, j];
             int      rgbValue = Util.GetRGBInteger(pix.red, pix.green, pix.blue);
             if (!hashDistinctColors.Contains(rgbValue))
             {
                 hashDistinctColors.Add(rgbValue);
             }
         }
     }
     ImageOperations.SaveImage(quantizedImageMatrix);
 }
Esempio n. 10
0
 public void GetClustsers()
 {
     for (int i = costs.Length - 1; i >= (costs.Length - (K - 1)); i--)
     {
         int destination = costs[i].to;
         edgeTo[destination].cost = -1;
     }
     for (int i = 0; i < edgeTo.Length; i++)
     {
         int start = i, end = i;
         int red           = 0;
         int green         = 0;
         int blue          = 0;
         int NumberOfNodes = 1;
         do
         {
             RGBPixel RGBValue = Util.GetRGBPixel(ImageQuantizer.distinctColors[i]);
             red   += Convert.ToInt32(RGBValue.red);
             green += Convert.ToInt32(RGBValue.green);
             blue  += Convert.ToInt32(RGBValue.blue);
             i++;
             NumberOfNodes++;
             end++;
         } while (i < edgeTo.Length && edgeTo[i].cost != -1);
         i--;
         NumberOfNodes--;
         end--;
         for (int j = start; j <= end; j++)
         {
             Palette.Add(ImageQuantizer.distinctColors[j], new RGBPixel((byte)(red / NumberOfNodes), (byte)(green / NumberOfNodes), (byte)(blue / NumberOfNodes)));
         }
     }
 }
Esempio n. 11
0
      public void Color_Pallette()  //4-Find Representative color of each Cluster Time:O(D)D-->Clusters(distinct Colors)
      {
          colors_centroid = new RGBPixel[clusters.Length];
          colors_knum     = new int[265, 265, 265];
          for (int i = 0; i < clusters.Length; i++)
          {
              int R = 0; int g = 0; int b = 0;

              List <RGBPixel> c = new List <RGBPixel>();

              c = clusters[i];

              for (int j = 0; j < c.Count; j++)
              {
                  R += c[j].red;
                  g += c[j].green;
                  b += c[j].blue;
                  colors_knum[c[j].red, c[j].green, c[j].blue] = i;
              }
              RGBPixel color = new RGBPixel();
              color.red          = (byte)(R / c.Count);
              color.green        = (byte)(g / c.Count);
              color.blue         = (byte)(b / c.Count);
              colors_centroid[i] = color;
          }
      }
Esempio n. 12
0
        /// <summary>
        /// Traverses the clusteredGraph to get each cluster individually and calculates its
        /// representative color to ceate the palette
        /// </summary>
        /// <param name="clusteredGraphh">Graph after removal of k-1 edges, contains clusters</param>
        /// <param name="discovered">Checks if node is visited</param>
        /// <param name="maxDistinctColors">The max number of distinict colors that could occur
        /// to intialize dictionaries and lists with</param>
        /// <param name="k">Required number of clusters</param>
        /// <param name="represntativeColor">Dictionary that carries every distinct node with its new color</param>
        /// <param name="distinctHelper">Helper Dictionary for keeping struct values concatenated as int (Key)
        /// and the struct as Value</param>
        /// <returns>The palette</returns>
        /// Time Complexity: E+V =V = O(D)
        public static List <RGBPixel> DFS(Dictionary <int, List <int> > clusteredGraphh, Dictionary <int, bool> discovered,
                                          int maxDistinctColors, int k, ref Dictionary <int, RGBPixel> represntativeColor,
                                          Dictionary <int, RGBPixel> distinctHelper)
        {
            clusteredGraph = clusteredGraphh;                                                                                               //O(1)
            MSTHelper      = distinctHelper;                                                                                                //O(1)
            List <RGBPixel> pallette = new List <RGBPixel>(k);                                                                              // initialize the size by clus //O(1)
            int             clusterRed = 0, clusterGreen = 0, clusterBlue = 0;                                                              //O(1)
            // traversing each cluster
            List <KeyValuePair <int, List <int> > > clusters = clusteredGraph.ToList();                                                     //o clusters size, //O(D)

            foreach (KeyValuePair <int, List <int> > vertex in clusters)                                                                    //E+V //V
            {
                if (!discovered[vertex.Key])                                                                                                //o1
                {
                    clusterRed = clusterGreen = clusterBlue = 0;                                                                            //O(1)
                    DFSHelper(vertex.Key, ref clusterRed, ref clusterGreen, ref clusterBlue);                                               //O(adj(vertex.key))
                    int      clusterNodesCount = clusterColors.Count;                                                                       //O(1)
                    RGBPixel newColor          = paletteGeneration(clusterRed, clusterGreen, clusterBlue, ref pallette, clusterNodesCount); //O(1)
                    FindRepresentativeColor(newColor, ref represntativeColor);                                                              //O(1)
                }
                else
                {
                    continue; //O(1)
                }
            }
            return(pallette); //O(1)
        }
Esempio n. 13
0
        /// <summary>
        /// Get The Pixels From an Image and Write it to a Text File
        /// </summary>
        /// <param name="img"></param>
        public static RGBPixel[,] GetPixelsTo_TheFile(string imgPath)
        {
            FileStream   FS = new FileStream(@"C:\Users\ibrahim\Desktop\ImagePixels2.txt", FileMode.Create);
            StreamWriter SW = new StreamWriter(FS);
            Bitmap       bm = new Bitmap(imgPath);

            SW.WriteLine(bm.Width.ToString());
            SW.WriteLine(bm.Height.ToString());

            RGBPixel[,] pixelsArr = new RGBPixel[bm.Height, bm.Width];


            for (int i = 0; i < bm.Height; i++)
            {
                for (int j = 0; j < bm.Width; j++)
                {
                    Color pixel = bm.GetPixel(j, i);
                    pixelsArr[i, j].red   = pixel.R;
                    pixelsArr[i, j].green = pixel.G;
                    pixelsArr[i, j].blue  = pixel.B;

                    SW.WriteLine(pixel.R.ToString());
                    SW.WriteLine(pixel.B.ToString());
                    SW.WriteLine(pixel.G.ToString());
                }
            }

            SW.Close();
            MessageBox.Show("Image had Wrote to the file 2 Successfuly ");
            return(pixelsArr);
        }
        public static RGBPixel[,] Encrypt_No_cycles(RGBPixel[,] ImageMatrix, short seed_size, short tap_pos, long seed)
        {
            RGBPixel[,] ret = new RGBPixel[ImageMatrix.GetLength(0), ImageMatrix.GetLength(1)];
            for (int i = 0; i < seed_size - 1; i++)
            {
                msk |= (1 << i);
            }
            int N = ImageMatrix.GetLength(0);
            int M = ImageMatrix.GetLength(1);
            int j = 0;

            for (int i = 0; i < N; i++)
            {
                for (j = 0; j < M; j++)
                {
                    ret[i, j].red    = ImageMatrix[i, j].red;
                    ret[i, j].red   ^= get_key_no_cycles(ref seed, tap_pos, seed_size, 8);
                    ret[i, j].green  = ImageMatrix[i, j].green;
                    ret[i, j].green ^= get_key_no_cycles(ref seed, tap_pos, seed_size, 8);
                    ret[i, j].blue   = ImageMatrix[i, j].blue;
                    ret[i, j].blue  ^= get_key_no_cycles(ref seed, tap_pos, seed_size, 8);
                }
            }
            return(ret);
        }
        public static RGBPixel[] constructGraph(RGBPixel[,] imageMatrix)
        {
            int height = GetHeight(imageMatrix), width = GetWidth(imageMatrix);
            Dictionary <string, int> distinctColors = new Dictionary <string, int>();
            int    counter = 0;
            string s       = "";

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    s  = imageMatrix[i, j].blue.ToString() + '-';
                    s += imageMatrix[i, j].green.ToString() + '+';
                    s += imageMatrix[i, j].red.ToString();
                    if (!(distinctColors.ContainsKey(s)))
                    {
                        distinctColors[s] = counter;
                        counter++;
                    }
                }
            }
            Console.WriteLine("The Output is:");
            Console.Write("Number of Distinct colors: ");
            Console.WriteLine(distinctColors.Count);
            int ss = distinctColors.Count;

            RGBPixel[] Graph = new RGBPixel[ss];



            foreach (var it in distinctColors)
            {
                s = it.Key;
                string red = "", green = "", blue = "";
                int    length = s.Length;
                for (int i = 0; i < length - 2; i++)
                {
                    if (s[0] == '-' && s[1] == '+')
                    {
                        red += s[2];
                        s    = s.Remove(2, 1);
                    }
                    else if (s[0] == '-')
                    {
                        green += s[1];
                        s      = s.Remove(1, 1);
                    }
                    else
                    {
                        blue += s[0];
                        s     = s.Remove(0, 1);
                    }
                }
                Graph[it.Value].blue  = Convert.ToByte(blue);
                Graph[it.Value].green = Convert.ToByte(green);
                Graph[it.Value].red   = Convert.ToByte(red);
            }
            return(Graph);
        }
Esempio n. 16
0
        /// <summary>
        /// Open an image and load it into 2D array of colors (size: Height x Width)
        /// </summary>
        /// <param name="ImagePath">Image file path</param>

        /// <returns>2D array of colors</returns>
        private static double euclideanDistance(RGBPixel first, RGBPixel second)    //O(1)
        {
            double x = (first.red - second.red) * (first.red - second.red);         //O(1)
            double y = (first.green - second.green) * (first.green - second.green); //O(1)
            double z = (first.blue - second.blue) * (first.blue - second.blue);     //O(1)

            return(Math.Sqrt(x + y + z));                                           //~O(1)
        }
        public static double GetDistance(int i, int j)
        {
            RGBPixel Cluster1 = ImageUtilities.DistinctColours[i];
            RGBPixel Cluster2 = ImageUtilities.DistinctColours[j];
            double   Result   = (double)Math.Sqrt((Cluster1.red - Cluster2.red) * (Cluster1.red - Cluster2.red) + (Cluster1.green - Cluster2.green) * (Cluster1.green - Cluster2.green) + (Cluster1.blue - Cluster2.blue) * (Cluster1.blue - Cluster2.blue));

            return(Result);
        }
Esempio n. 18
0
        public static double generate_dif(ref int i, ref int j)
        {
            double   dif;
            RGBPixel new_one1 = distinct[i], new_one2 = distinct[j];

            dif = (double)Math.Sqrt((new_one1.red - new_one2.red) * (new_one1.red - new_one2.red) + (new_one1.blue - new_one2.blue) * (new_one1.blue - new_one2.blue) + (new_one1.green - new_one2.green) * (new_one1.green - new_one2.green));
            return(dif);
        }
Esempio n. 19
0
 /// <summary>
 /// Constructs the represntativeColor dictionary
 /// </summary>
 /// <param name="newColor">The Cluster representative color</param>
 /// <param name="represntativeColor">Dictionary that has each original color as key
 /// and rep color of each cluster as value</param>
 /// Time Complexity: O(D)
 static void FindRepresentativeColor(RGBPixel newColor, ref Dictionary <int, RGBPixel> represntativeColor) //max D
 {
     while (clusterColors.Count != 0)                                                                      //O(stackSize) -> max O(D)
     {
         int basicColor = clusterColors.Pop();                                                             //O(1)
         represntativeColor.Add(basicColor, newColor);                                                     //O(1)
     }
 }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                MST_value.Text = "";
                li.Clear();                   // Delete Old Distict Color
                for (int i = 0; i < 256; i++) // Set All Color False (255 , 255 , 255)
                {
                    for (int j = 0; j < 256; j++)
                    {
                        for (int k = 0; k < 256; k++)
                        {
                            RGB[i, j, k] = new RGBPixel(255, 255, 255);
                        }
                    }
                }

                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;
                ImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                int      height          = ImageOperations.GetHeight(ImageMatrix);
                int      width           = ImageOperations.GetWidth(ImageMatrix);
                RGBPixel False_cmp       = new RGBPixel(255, 255, 255);
                bool     check_false_cmp = false;

                for (int i = 0; i < height; i++)
                {
                    for (int j = 0; j < width; j++)
                    {
                        // if Image Have Color (255 ,255,255)
                        if (ImageMatrix[i, j].red == 255 && ImageMatrix[i, j].green == 255 && ImageMatrix[i, j].blue == 255)
                        {
                            if (!check_false_cmp)
                            {
                                check_false_cmp = true;
                                RGB[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue] =
                                    new RGBPixel(ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue);
                                li.Add(ImageMatrix[i, j]);
                            }
                        }
                        else if (RGB[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue] == False_cmp)
                        {
                            RGB[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue] =
                                new RGBPixel(ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue);
                            li.Add(ImageMatrix[i, j]);
                        }
                    }
                }
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1, RGB);

                txtWidth.Text         = ImageOperations.GetWidth(ImageMatrix).ToString();
                txtHeight.Text        = ImageOperations.GetHeight(ImageMatrix).ToString();
                txtDistinctColor.Text = li.Count.ToString();
                txtGaussSigma.Text    = "1";
            }
        }
Esempio n. 21
0
        /// <summary>
        ///  converts and integer containing Red Green Blue color combination into three sperated integers
        ///   using bit manipulation
        /// </summary>

        public static RGBPixel GetRGBPixel(int rgb)
        {
            RGBPixel RGB = new RGBPixel();

            RGB.blue  = Convert.ToByte(rgb & 255);
            RGB.green = Convert.ToByte((rgb >> 8) & 255);
            RGB.red   = Convert.ToByte((rgb >> 16) & 255);
            return(RGB);
        }
Esempio n. 22
0
        public static double CalculateEdgeValue(int currentNode, int otherNode)
        {
            RGBPixel rgb1 = Util.GetRGBPixel(ImageQuantizer.distinctColors[currentNode]);
            RGBPixel rgb2 = Util.GetRGBPixel(ImageQuantizer.distinctColors[otherNode]);

            return(Math.Sqrt((rgb1.red - rgb2.red) * (rgb1.red - rgb2.red) +
                             (rgb1.green - rgb2.green) * (rgb1.green - rgb2.green) +
                             (rgb1.blue - rgb2.blue) * (rgb1.blue - rgb2.blue)));
        }
Esempio n. 23
0
        private void btnGaussSmooth_Click(object sender, EventArgs e)
        {
            int    tap  = Int16.Parse(txtGaussSigma.Text);                                         //ϴ(1)
            string seed = textBox1.Text;                                                           //ϴ(1)
            // start stopwatch
            Stopwatch stopWatch = new Stopwatch();                                                 // ϴ(1)

            stopWatch.Start();                                                                     // ϴ(1)

            TimeSpan ts;                                                                           // ϴ(1)
            string   elapsedTime;                                                                  // ϴ(1)

            RGBPixel[,] image3 = new RGBPixel[ImageMatrix.GetLength(0), ImageMatrix.GetLength(1)]; // ϴ(1)
            Array.Copy(ImageMatrix, image3, ImageMatrix.GetLength(0) * ImageMatrix.GetLength(1));  // ϴ(N^2) where n is the largest between width and height
            ImageMatrix = ImageOperations.encrypt(ImageMatrix, tap, seed);                         // ϴ(N^2) where n is the largest between width and height

            ImageOperations.DisplayImage(ImageMatrix, pictureBox2);                                // ϴ(N^2) where n is the largest between width and height
            ts = stopWatch.Elapsed;                                                                // ϴ(1)
            // Format and display the TimeSpan value.
            elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                        ts.Hours, ts.Minutes, ts.Seconds,
                                        ts.Milliseconds / 10);                                                                              // ϴ(1)
            label10.Text = elapsedTime;                                                                                                     // ϴ(1)

            PriorityQueue redQ   = new PriorityQueue();                                                                                     // ϴ(1)
            PriorityQueue greenQ = new PriorityQueue();                                                                                     // ϴ(1)
            PriorityQueue blueQ  = new PriorityQueue();                                                                                     // ϴ(1)

            ImageOperations.getFrequency(ImageMatrix, redQ, greenQ, blueQ);                                                                 // ϴ(N^2) where n is the largest between width and height
            Huffman huffmanRed       = new Huffman(redQ);                                                                                   // ϴ(n log n) where n is the largest between width and height
            Huffman huffmanGreen     = new Huffman(greenQ);                                                                                 // ϴ(n log n) where n is the largest between width and height
            Huffman huffmanBlue      = new Huffman(blueQ);                                                                                  // ϴ(n log n) where n is the largest between width and height
            double  originalSize     = ImageMatrix.GetLength(0) * ImageMatrix.GetLength(1) * 8 * 3;                                         // ϴ(1)
            double  compressedSize   = huffmanRed.getCompressedSize() + huffmanGreen.getCompressedSize() + huffmanBlue.getCompressedSize(); // ϴ(1)
            double  compressionRatio = (compressedSize / originalSize) * 100;                                                               // ϴ(1)

            compressRatio.Text = compressionRatio + "%";                                                                                    // ϴ(1)
            Compression.compress(ImageMatrix, huffmanRed, huffmanGreen, huffmanBlue, seed, short.Parse(txtGaussSigma.Text));                // ϴ(N^2) where n is the largest between width and height
            ts = stopWatch.Elapsed;                                                                                                         // ϴ(1)
            // Format and display the TimeSpan value.
            elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                        ts.Hours, ts.Minutes, ts.Seconds,
                                        ts.Milliseconds / 10);                       // ϴ(1)

            label12.Text = elapsedTime;                                              // ϴ(1)

            RGBPixel[,] ImageMatrix2 = Compression.decompress("compressEncode.bin"); // ϴ(N^2) where n is the largest between width and height
            ImageOperations.DisplayImage(ImageMatrix2, pictureBox3);                 // ϴ(N^2) where n is the largest between width and height
            ts = stopWatch.Elapsed;                                                  // ϴ(1)
            // Format and display the TimeSpan value.
            elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                                        ts.Hours, ts.Minutes, ts.Seconds,
                                        ts.Milliseconds / 10); // ϴ(1)

            label11.Text = elapsedTime;                        // ϴ(1)
            stopWatch.Stop();                                  // ϴ(1)
        }
        /// <summary>
        ///  converts and integer containing Red Green Blue color combination into three sperated integers
        ///   using bit manipulation
        /// </summary>

        public static RGBPixel GetRGBPixel(int rgb)
        {
            // return new RGBPixel((byte)(((1<<8)-1)&(rgb)), (byte)(((1<<8)-1)&(rgb>>8)),(byte)(((1<<8)-1)&(rgb>>16)));
            RGBPixel RGB = new RGBPixel();

            RGB.blue  = Convert.ToByte(rgb & 255);
            RGB.green = Convert.ToByte((rgb >> 8) & 255);
            RGB.red   = Convert.ToByte((rgb >> 16) & 255);
            return(RGB);
        }
 private static void DFS(RGBPixel currentColor, ref HashSet <RGBPixel> visited, ref List <RGBPixel> cluster)
 {
     visited.Add(currentColor);
     cluster.Add(currentColor);
     foreach (var neighbour in neighbours[currentColor])
     {
         if (!visited.Contains(neighbour))
         {
             DFS(neighbour, ref visited, ref cluster);
         }
     }
 }
Esempio n. 26
0
        private void btnGaussSmooth_Click(object sender, EventArgs e)
        {
            double sigma    = double.Parse(Seed.Text);
            int    maskSize = (int)nudMaskSize.Value;

            ImageMatrix = ImageOperations.GaussianFilter1D(ImageMatrix, maskSize, sigma);
            RGBPixel asd = ImageMatrix[1, 1];

            txtWidth.Text  = asd.red.ToString();
            txtHeight.Text = asd.green.ToString();
            ImageOperations.DisplayImage(ImageMatrix, pictureBox2);
        }
 /// <summary>
 /// set color in 3d RGB volor Values
 /// </summary>
 private void setClusters()                     // O(K*D)
 {
     for (int i = 0; i < Clustering.Count; i++) // o(K*D)
     {
         RGBPixel res = Avrge(i);
         Pallete.Add(res);
         for (int j = 0; j < Clustering[i].Count; j++)
         {
             RGB[Clustering[i][j].red, Clustering[i][j].green, Clustering[i][j].blue] = res;
         }
     }
 }
        /// <summary>
        /// Extracts the Distinct Colours from the image
        /// <param name="ImageMatrix">Colored image matrix</param>
        /// <returns>List of all the distinct colours </returns>
        /// </summary>
        public static void DistinctColours(RGBPixel[,] ImageMatrix)
        {
            RGBPixel[,,] flagColour = new RGBPixel[256, 256, 256];

            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    for (int k = 0; k < 256; k++)
                    {
                        flagColour[i, j, k] = new RGBPixel(255, 255, 255);
                    }
                }
            }

            List <RGBPixel> DistinctColours = new List <RGBPixel>();
            int             w = ImageOperations.GetWidth(ImageMatrix), h = ImageOperations.GetHeight(ImageMatrix), cnt = 0;

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    byte r = ImageMatrix[i, j].red, g = ImageMatrix[i, j].green, b = ImageMatrix[i, j].blue;
                    if (ImageMatrix[i, j].red != 255 && ImageMatrix[i, j].green != 255 && ImageMatrix[i, j].green != 255)
                    {
                        if (flagColour[r, g, b].red != 255 && flagColour[r, g, b].green != 255 && flagColour[r, g, b].blue != 255)
                        {
                            continue;
                        }
                        else
                        {
                            flagColour[r, g, b] = new RGBPixel(r, g, b);
                            DistinctColours.Add(new RGBPixel(r, g, b));
                        }
                    }
                    else if (r == 255 && g == 255 && g == 255 && cnt == 0)
                    {
                        flagColour[r, g, b] = new RGBPixel(r, g, b);
                        DistinctColours.Add(new RGBPixel(r, g, b));
                        cnt++;
                    }
                }
            }
            //string file_name = "C:\\Users\\Rolla\\Desktop\\colour_test.txt";
            //System.IO.StreamWriter objWriter;
            //objWriter = new System.IO.StreamWriter(file_name);
            //foreach (var clr in DistinctColours)
            //{
            //    objWriter.Write("(" + clr.red + "," + clr.green + "," + clr.blue + ")");
            //}
            //objWriter.Close();
        }
Esempio n. 29
0
        // get the average color of each group colors

        public static RGBPixel[, ,] UpdateColor(ref List <List <RGBPixel> > groups)
        {
            RGBPixel[, ,] Update = new RGBPixel[256, 256, 256];
            for (int i = 0; i < groups.Count; i++)
            {
                int groupSize = groups[i].Count;
                for (int j = 0; j < groupSize; j++)
                {
                    Update[groups[i][j].red, groups[i][j].blue, groups[i][j].green] = PaletteColor[i];
                }
            }
            return(Update);
        }
Esempio n. 30
0
 //Depth First Search
 public static void DFS(RGBPixel start, List <RGBPixel> tmpGroup)
 {
     nodes.Remove(start);
     tmpGroup.Add(start);
     foreach (var item in adjList[start])
     {
         if (nodes.Contains(item))
         {
             nodes.Remove(item);
             DFS(item, tmpGroup);
         }
     }
 }