Ejemplo n.º 1
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            total = 0;
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;
                ImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
            }

            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
            //////// measuring time  ////////
            var    watch  = System.Diagnostics.Stopwatch.StartNew();
            double answer = ImageOperations.ConstructGraph(ImageMatrix);

            watch.Stop();
            var elapsedSec = (watch.ElapsedMilliseconds) / 1000.0;

            total += elapsedSec;
            ////////////////////////////////
            Colors.Text = ImageOperations.numColors.ToString();
            Costs.Text  = answer.ToString();
        }
 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];
 }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            distColorsBox1.Text     = "";
            distinctColorsBox2.Text = "";
            ClustersNumber.Text     = "";
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Open the browsed image and display it

                string OpenedFilePath = openFileDialog1.FileName;
                ImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
                WhiteDistButton.Enabled = true;
                MstQtButton.Enabled     = true;
                ClustersNumber.Enabled  = true;
            }
            else
            {
                MessageBox.Show("please open an Image");
                return;
            }
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();

            distColorsBox1.Text = CalculateOriginalDistinctColors(ImageOperations.GetHeight(ImageMatrix), ImageOperations.GetWidth(ImageMatrix)).ToString();
        }
Ejemplo n.º 4
0
        void adjustMatrix(ref RGBPixel[,] ImageMatrix)
        {
            for (int i = 0; i < ImageOperations.GetHeight(ImageMatrix); i++)
            {
                for (int j = 0; j < ImageOperations.GetWidth(ImageMatrix); j++)
                {
                    RGBPixel point;
                    point.red   = ImageMatrix[i, j].red;
                    point.green = ImageMatrix[i, j].green;
                    point.blue  = ImageMatrix[i, j].blue;
                    int      idx = findu(id[point.red, point.green, point.blue]);
                    RGBPixel newColor;
                    if (sz[idx] != 0)
                    {
                        newColor.red      = (byte)(sumOfCh[idx].red / sz[idx]);
                        newColor.green    = (byte)(sumOfCh[idx].green / sz[idx]);
                        newColor.blue     = (byte)(sumOfCh[idx].blue / sz[idx]);
                        ImageMatrix[i, j] = newColor;
                    }

                    /*int x = 0;
                     * x++;*/
                }
            }
        }
Ejemplo n.º 5
0
        /*void SolveDFS()
         * {
         *  int cnt = 1;
         *  //Visted = new int[listOfDistColor.Count];
         *  //cluster = new Dictionary<RGBPixel, int>();
         *  //NodePerCluster = new RGBPixel[k+1];
         *
         *  for (int i = 0; i < listOfDistColor.Count; ++i)
         *  {
         *      if (Visted[i] == 0) {
         *          RGBPixel cntr = new RGBPixel();
         *          long red = 0, green = 0, blue = 0;
         *          int temp=DFS(i,cnt,1, ref red, ref green, ref blue);
         *          red /= temp;
         *          green /= temp;
         *          blue /= temp;
         *          cntr.red = (byte)red;
         *          cntr.green = (byte)green;
         *          cntr.blue = (byte)blue;
         *          NodePerCluster[cnt] = cntr;
         ++cnt;
         *      }
         *  }
         * }
         * int DFS(int parent, int cntt,int dis, ref long red, ref long green, ref long blue)
         * {
         *
         *  Visted[parent] = 1;
         *  cluster.Add(listOfDistColor[parent], cntt);
         *  red += listOfDistColor[parent].red;
         *  green += listOfDistColor[parent].green;
         *  blue += listOfDistColor[parent].blue;
         *  for (int it=0;it<adj[parent].Count;++it)
         *  { KeyValuePair<int, int>pa=new KeyValuePair<int, int>(parent, adj[parent][it]);
         *      if (Visted[adj[parent][it]]==0 &&cutFromTo.ContainsKey(pa)==false)
         *          dis += 1 + DFS(adj[parent][it], cntt,0, ref red, ref green, ref blue);
         *  }
         *  return dis;
         * }*/
        public List <RGBPixel> ExtractDistColors(RGBPixel [,] ImageMatrix)
        {
            listOfDistColor = new List <RGBPixel>();
            id = new int[256, 256, 256];
            int idx = 0;

            byte[,,] isExected = new byte[256, 256, 256];
            for (int i = 0; i < ImageOperations.GetHeight(ImageMatrix); i++)
            {
                for (int j = 0; j < ImageOperations.GetWidth(ImageMatrix); j++)
                {
                    RGBPixel point;
                    point.red   = ImageMatrix[i, j].red;
                    point.green = ImageMatrix[i, j].green;
                    point.blue  = ImageMatrix[i, j].blue;

                    if (isExected[point.red, point.green, point.blue] == 0)
                    {
                        listOfDistColor.Add(point);
                        id[point.red, point.green, point.blue] = idx;
                        idx++;
                        isExected[point.red, point.green, point.blue] = 1;
                    }
                }
            }
            return(listOfDistColor);
        }
Ejemplo n.º 6
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            Stopwatch s = new Stopwatch();

            s.Start();
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;
                ImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
                txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
                txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
            }
            else if (ImageMatrix != null)
            {
                txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
                txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
            }
            else
            {
                MessageBox.Show(" Choose Image ");
            }
            s.Stop();
            txtTime.Text = Convert.ToString(s.Elapsed);
        }
Ejemplo n.º 7
0
        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);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);

                long timeBefore = Environment.TickCount;

                Distinct_Colors = ImageUtilities.GetDistinctColors(ImageMatrix);
                MST             = new EagerPrimMST(Distinct_Colors);
                double MST_SUM = MST.GetMst();
                edges = MST.Edge_To;
                txtDistinctColours.Text = Distinct_Colors.ToString();
                txtMSTSum.Text          = MST_SUM.ToString();
                long timeAfter = Environment.TickCount;

                txtTime.Text = (timeAfter - timeBefore).ToString();
            }
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
        }
Ejemplo n.º 8
0
        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[, ];
        }
Ejemplo n.º 9
0
        public void Encryption_Decryption(ref RGBPixel[,] img, long initial, int tap, int length_initialseed)
        {
            int img_Height   = ImageOperations.GetHeight(img);
            int img_Width    = ImageOperations.GetWidth(img);
            int Shift_Length = 1 << length_initialseed;
            int Shift_Tap    = 1 << tap;

            for (int i = 0; i < img_Height; i++)
            {
                for (int j = 0; j < img_Width; j++)
                {
                    long initial_seed_1 = LFSR(initial, tap, length_initialseed, Shift_Tap, Shift_Length);
                    img[i, j].red = (Byte)(img[i, j].red ^ (initial_seed_1 & 255));

                    long initial_seed_2 = LFSR(initial_seed_1, tap, length_initialseed, Shift_Tap, Shift_Length);
                    img[i, j].green = (Byte)(img[i, j].green ^ (initial_seed_2 & 255));

                    long initial_seed_3 = LFSR(initial_seed_2, tap, length_initialseed, Shift_Tap, Shift_Length);
                    img[i, j].blue = (Byte)(img[i, j].blue ^ (initial_seed_3 & 255));

                    initial = initial_seed_3;
                }
            }
            ImageOperations.DisplayImage(img, pictureBox2);
        }
Ejemplo n.º 10
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            // intialization the tools
            ClusterK.Value        = 0;                             // -> O(1)
            FloyedFactorNum.Value = 1;                             // -> O(1)
            NonFloyedNum.Value    = 1;                             // -> O(1)
            MedianNum.Value       = 1;                             // -> O(1)
            SurfaceNum.Value      = 1;                             // -> O(1)
            txtMST.Text           = null;                          // -> O(1)
            txtDisColor.Text      = null;                          // -> O(1)
            txtGaussSigma.Text    = "1";                           // -> O(1)
            nudMaskSize.Value     = 3;                             // -> O(1)
            pictureBox2.Image     = null;                          // -> O(1)
            TimeLable.Text        = "0 Ms";                        // -> O(1)
            OpenFileDialog openFileDialog1 = new OpenFileDialog(); // -> O(1)

            if (openFileDialog1.ShowDialog() == DialogResult.OK)   // -> O(1)
            {
                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;               // -> O(1)
                ImageMatrix = ImageOperations.OpenImage(OpenedFilePath);        // -> O(W*H)
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1, 0);      // -> O(W*H)
            }
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();  // -> O(1)
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString(); // -> O(1)
        }
        private void btnOpen_Click(object sender, EventArgs e)
        {
            textBox1.Text = "0";
            textBox2.Text = "0";
            try
            {
                OpenFileDialog openFileDialog1 = new OpenFileDialog();
                if (openFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    //Open the browsed image and display it
                    string OpenedFilePath = openFileDialog1.FileName;
                    globalpathforimage = OpenedFilePath;
                    ImageMatrix        = ImageOperations.OpenImage(OpenedFilePath);
                    ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
                    pictureBox1.Visible = true;
                }
                txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
                txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
            }

            catch
            {
                MessageBox.Show("Please Select a photo");
            }
        }
Ejemplo n.º 12
0
        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);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
            }
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
            string K = TextBoxK.Text;

            k = System.Convert.ToInt32(K);
            long timeBefore = System.Environment.TickCount;

            Nodes    = ImageOperations.FillGraph(ImageMatrix);
            NewEdges = ImageOperations.MST(ImageOperations.Globals.distinct, Nodes);
            long timeAfter = System.Environment.TickCount;

            ImageOperations.Globals.Time = timeAfter - timeBefore;
            MST_txt.Text = ImageOperations.Globals.sum.ToString();
            //k = ImageOperations.NumOfClusters(NewEdges);
            Distinct_txt.Text = ImageOperations.Globals.distinct.ToString();
        }
Ejemplo n.º 13
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)
        }
Ejemplo n.º 14
0
        /*
         * Finding Distinct Colors Function
         * Summary : Calculate the number of distinct colors in an image
         * Parameters : Input image as 2D array of RGPPixel
         * Return : List of distinct colors each distinct color of type RGBPixelD
         */
        public List <RGBPixelD> findingDistinctColors(RGBPixel [,] colorsarr)                           //O(N^2)
        {
            int height = ImageOperations.GetHeight(colorsarr);                                          //O(1)
            int width  = ImageOperations.GetWidth(colorsarr);                                           //O(1)
            List <RGBPixelD>         newDistinct = new List <RGBPixelD>();                              //O(1)
            Dictionary <double, int> checker     = new Dictionary <double, int>();                      //O(1)
            RGBPixelD temp;                                                                             //O(1)

            for (int i = 0; i < height; i++)                                                            //O(N^2)
            {
                for (int j = 0; j < width; j++)                                                         //O(N)
                {
                    temp.blue  = colorsarr[i, j].blue;                                                  //O(1)
                    temp.green = colorsarr[i, j].green;                                                 //O(1)
                    temp.red   = colorsarr[i, j].red;                                                   //O(1)
                    double key = 0.5 * (temp.red + temp.blue) * (temp.red + temp.blue + 1) + temp.blue; //O(1)
                    key = 0.5 * (temp.green + key) * (temp.green + key + 1) + key;                      //O(1)
                    if (!checker.ContainsKey(key))                                                      //O(1)
                    {
                        checker.Add(key, 1);                                                            //O(1)
                        newDistinct.Add(temp);                                                          //O(1)
                    }
                }
            }
            return(newDistinct);//O(1)
        }
Ejemplo n.º 15
0
        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);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
            }
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();

            // Call Get Distincit to get all distincit colors from ImageMatrix
            QuantizationProcess.GetDistinct(ImageMatrix);
            //****************** Test *************************
            QuantizationProcess p = new QuantizationProcess();

            p.TEST();
            //enter K in the Gauss Sigma's textBox
            int k = Convert.ToInt16(txtGaussSigma.Text);

            p.Cluster(k);
            p.replaceWithPaletteColors(ImageMatrix);
            ImageOperations.DisplayImage(ImageMatrix, pictureBox2);

            // ******************** TEST TEST ********************
        }
Ejemplo n.º 16
0
        //Function To Get The Distincit Color From 2d Matrix
        public List <RGBPixel> getDistincitColors()
        {
            check = new int[20000000];
            //Get The Width Of The Image
            int Dcolors_width = ImageOperations.GetWidth(ImageMatrix);
            //Get The Height Of The Image
            int Dcolors_height = ImageOperations.GetHeight(ImageMatrix);

            //List To Save The Distincit Colors
            List <RGBPixel> Distinctcolors = new List <RGBPixel>();

            Distinctcolors.Add(new RGBPixel());
            //Looping Over All Pixels
            for (int i = 0; i < Dcolors_height; i++)
            {
                for (int j = 0; j < Dcolors_width; j++)
                {
                    //Merge The 3 Bytes Of The Red Green and Blue
                    int res = ImageMatrix[i, j].red;
                    res = (res << 8) + ImageMatrix[i, j].green;
                    res = (res << 8) + ImageMatrix[i, j].blue;
                    //Check If The Pixel Taken Befor
                    if (check[res] != 0)
                    {
                        continue;
                    }
                    //Mark The Pixel As Taken
                    check[res] = Distinctcolors.Count;
                    //Add The Pixel as A Distinct Color
                    Distinctcolors.Add(ImageMatrix[i, j]);
                }
            }
            //Return The Distinct Colors
            return(Distinctcolors);
        }
Ejemplo n.º 17
0
        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";
            }
        }
Ejemplo n.º 18
0
 public void Quantize_the_image(RGBPixel[,] ImageMatrix)
 {
     for (long i = 0; i < ImageOperations.GetHeight(ImageMatrix); i++)
     {
         for (long j = 0; j < ImageOperations.GetWidth(ImageMatrix); j++)
         {
             ImageMatrix[i, j] = colors_centroid[colors_knum[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue]];
         }
     }
 }
        private void button3_Click(object sender, EventArgs e)
        {
            int width  = ImageOperations.GetWidth(ImageMatrix);
            int height = ImageOperations.GetHeight(ImageMatrix);

            compression compress = new compression();

            compress.count_color(ImageMatrix, width, height);
            compress.final_tree();
        }
        private static int r, g, b;             //->O(1)

        /// <summary>
        /// for each pixel an "error" is generated and then distributed to four pixels around
        /// the surrounding the current pixel. Each of the four offset pixels has a different
        /// weight - the error is multiplied by the weight, divided by 16 and then
        /// added to the existing value of the offset pixel
        /// four offset pixels:
        ///     7 for the pixel to the right of the current pixel
        ///     3 for the pixel below and to the left
        ///     5 for the pixel below
        ///      1 for the pixel below and to the right
        /// </summary>
        /// <param name="factor">quant_error </param>
        /// <param name="Filltered"></param>
        /// <returns></returns>
        public static RGBPixel[,] Floyed_Dithering(int factor, RGBPixel[,] Filltered)     // ->>O(H * W)
        {
            int h = ImageOperations.GetHeight(Filltered);                                 //->O(1)
            int w = ImageOperations.GetWidth(Filltered);                                  //->O(1)

            RGBPixel[,] ImageMatrix = Filltered.Clone() as RGBPixel[, ];                  // => O(H * W)
            for (int i = 0; i < w - 1; i++)                                               //horizontal             //->O(W) * O(H)    ->>O(H * W)
            {
                for (int j = 1; j < h - 1; j++)                                           // vertical                //->O(H) *O(1)  ->>O(H)
                {
                    R1 = ImageMatrix[j, i].red;                                           //->O(1)
                    G1 = ImageMatrix[j, i].green;                                         //->O(1)
                    B1 = ImageMatrix[j, i].blue;                                          //->O(1)

                    R2 = Convert.ToInt32(Math.Round(factor * R1 / 255)) * (255 / factor); //->O(1)
                    G2 = Convert.ToInt32(Math.Round(factor * G1 / 255)) * (255 / factor); //->O(1)
                    B2 = Convert.ToInt32(Math.Round(factor * B1 / 255)) * (255 / factor); //->O(1)
                    ImageMatrix[j, i].red   = (byte)R2;                                   //->O(1)
                    ImageMatrix[j, i].green = (byte)G2;                                   //->O(1)
                    ImageMatrix[j, i].blue  = (byte)B2;                                   //->O(1)
                    err_r = (int)R1 - R2;                                                 //->O(1)
                    err_g = (int)G1 - G2;                                                 //->O(1)
                    err_b = (int)B1 - B2;                                                 //->O(1)
                    r     = ImageMatrix[j + 1, i].red + err_r * 7 / 16;                   //->O(1)
                    g     = ImageMatrix[j + 1, i].green + err_g * 7 / 16;                 //->O(1)
                    b     = ImageMatrix[j + 1, i].blue + err_b * 7 / 16;                  //->O(1)
                    Scale();                                                              // ->O(1)
                    ImageMatrix[j + 1, i].red   = (byte)r;                                // ->O(1)
                    ImageMatrix[j + 1, i].green = (byte)g;                                // ->O(1)
                    ImageMatrix[j + 1, i].blue  = (byte)b;                                // ->O(1)
                    r = ImageMatrix[j - 1, i + 1].red + err_r * 3 / 16;                   // ->O(1)
                    g = ImageMatrix[j - 1, i + 1].green + err_g * 3 / 16;                 // ->O(1)
                    b = ImageMatrix[j - 1, i + 1].blue + err_b * 3 / 16;                  // ->O(1)
                    Scale();                                                              // ->O(1)
                    ImageMatrix[j - 1, i + 1].red   = (byte)r;                            // ->O(1)
                    ImageMatrix[j - 1, i + 1].green = (byte)g;                            // ->O(1)
                    ImageMatrix[j - 1, i + 1].blue  = (byte)b;                            // ->O(1)
                    r = (ImageMatrix[j, i + 1].red + err_r * 5 / 16);                     // ->O(1)
                    g = (ImageMatrix[j, i + 1].green + err_g * 5 / 16);                   // ->O(1)
                    b = (ImageMatrix[j, i + 1].blue + err_b * 5 / 16);                    // ->O(1)
                    Scale();                                                              // ->O(1)
                    ImageMatrix[j, i + 1].red   = (byte)r;                                // ->O(1)
                    ImageMatrix[j, i + 1].green = (byte)g;                                // ->O(1)
                    ImageMatrix[j, i + 1].blue  = (byte)b;                                // ->O(1)
                    r = ImageMatrix[j + 1, i + 1].red + err_r * 1 / 16;                   // ->O(1)
                    g = ImageMatrix[j + 1, i + 1].green + err_g * 1 / 16;                 // ->O(1)
                    b = ImageMatrix[j + 1, i + 1].blue + err_b * 1 / 16;                  // ->O(1)
                    Scale();                                                              // ->O(1)
                    ImageMatrix[j + 1, i + 1].red   = (byte)r;                            // ->O(1)
                    ImageMatrix[j + 1, i + 1].green = (byte)g;                            // ->O(1)
                    ImageMatrix[j + 1, i + 1].blue  = (byte)b;                            // ->O(1)
                }
            }
            return(ImageMatrix); // ->O(1)
        }
Ejemplo n.º 21
0
        //public static void comp(Node node, string code) //O(n)
        //{

        //    if (node.left == null && node.right == null) //ϴ(1)
        //    {
        //        l = code.Length - 1; //ϴ(1)
        //        c = long.Parse((code)); //ϴ(1)
        //        p.Len = l; //ϴ(1)
        //        p.Path = c; //ϴ(1)
        //        red.Add(node.Value, p); //O(1)
        //        return;
        //    }
        //    if (node.right != null) //ϴ(1)
        //    {
        //        comp(node.right, code + "1"); //O(n)

        //    }
        //    if (node.left != null) //ϴ(1)
        //    {
        //        comp(node.left, code + "0"); //O(n)
        //    }
        //}
        //public static void toComp() //O(n)
        //{
        //    comp(root, " "); //O(n)
        //}
        public static void compress(RGBPixel[,] m) //ϴ(n^2)
        {
            long y = 0;
            long x;
            int  l;
            int  acc           = 0;
            int  diff          = 0;
            int  bitsremaining = 0;
            int  width         = ImageOperations.GetWidth(m);           //ϴ(1)
            int  height        = ImageOperations.GetHeight(m);          //ϴ(1)

            for (int i = 0; i < height; i++)                            //ϴ(n^2)
            {
                for (int j = 0; j < width; j++)                         //ϴ(n)
                {
                    if (red.ContainsKey(m[i, j].red))                   //ϴ(1)
                    {
                        bool c = red.TryGetValue((m[i, j].red), out p); //ϴ(1)
                        if (c)                                          //ϴ(1)
                        {
                            x = p.Path;                                 //ϴ(1)

                            l             = p.Len;                      //ϴ(1)
                            bitsremaining = 64 - acc;                   //ϴ(1)

                            if (l == bitsremaining)                     //ϴ(1)
                            {
                                y = (y | x);                            //ϴ(1)
                                s.Write(y);                             //ϴ(1)
                                acc = 0;                                //ϴ(1)
                                y   = 0;                                //ϴ(1)
                            }

                            else if (l < bitsremaining)       //ϴ(1)
                            {
                                acc += l;                     //ϴ(1)

                                y = (y | x) << bitsremaining; //ϴ(1)
                            }
                            else if (l > bitsremaining)       //ϴ(1)
                            {
                                diff = l - bitsremaining;     //ϴ(1)
                                y    = y | (x >> diff);       //ϴ(1)
                                s.Write(y);                   //ϴ(1)
                                y    = 0;                     //ϴ(1)
                                acc  = 0;                     //ϴ(1)
                                y    = x << (64 - diff);      //ϴ(1)
                                acc += diff;                  //ϴ(1)
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 22
0
 public static RGBPixel[,] ImageQuantization(RGBPixel[,] ImageMatrix, int[] indices, RGBPixel[] Nodes)
 {
     for (int j = 0; j < ImageOperations.GetHeight(ImageMatrix); j++)
     {
         for (int l = 0; l < ImageOperations.GetWidth(ImageMatrix); l++)
         {
             ImageMatrix[j, l] = Globals.Representative[indices[Globals.Image[j, l]]];
         }
     }
     return(ImageMatrix);
 }
Ejemplo n.º 23
0
        /// <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();
        }
Ejemplo n.º 24
0
        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);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
            }
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
        }
Ejemplo n.º 25
0
        public static int[] CountBlueFrequency(RGBPixel[,] image) //ϴ(n^2)
        {
            int[] BlueArr = new int[256];                         //ϴ(1)
            int   width   = ImageOperations.GetWidth(image);      //ϴ(1)
            int   height  = ImageOperations.GetHeight(image);     //ϴ(1)

            for (int i = 0; i < height; i++)                      //ϴ(n^2)
            {
                for (int j = 0; j < width; j++)                   //ϴ(n)
                {
                    BlueArr[image[i, j].blue] += 1;               //ϴ(1)
                }
            }
            return(BlueArr);
        }
        private static int err_r, err_g, err_b; //->O(1)
        /// <summary>
        ///  for each pixel an "error" is generated and then distributed to four pixels around
        /// the surrounding the current pixel. Each of the four offset pixels has a different
        /// weight - the error is multiplied by the weight, divided by 16 and then
        /// added to the existing value of the offset pixel
        /// four offset pixels:
        ///     7 for the pixel to the right of the current pixel
        ///     3 for the pixel below and to the left
        ///     5 for the pixel below
        ///      1 for the pixel below and to the right
        /// </summary>
        /// <param name="ImageMatrix"></param>
        /// <param name="factor">quant_error </param>
        /// <returns></returns>
        public static RGBPixel[,] Atkinson_Dithering(RGBPixel[,] ImageMatrix, int factor)                                    // ->>O(H * W)
        {
            h = ImageOperations.GetHeight(ImageMatrix);                                                                      //->O(1)
            w = ImageOperations.GetWidth(ImageMatrix);                                                                       //->O(1)
            RGBPixel[,] Buffer = ImageMatrix.Clone() as RGBPixel[, ];                                                        // => O(H * W)

            for (int i = 0; i < w - 1; i++)                                                                                  //horizontal             //->O(W) * O(H)    ->>O(H * W)
            {
                for (int j = 1; j < h - 1; j++)                                                                              // vertical                //->O(H) *O(1)  ->>O(H)
                {
                    R1 = Buffer[j, i].red;                                                                                   //->O(1)
                    G1 = Buffer[j, i].green;                                                                                 //->O(1)
                    B1 = Buffer[j, i].blue;                                                                                  //->O(1)

                    R2 = Convert.ToInt32(Math.Round(factor * R1 / 255)) * (255 / factor);                                    //->O(1)
                    G2 = Convert.ToInt32(Math.Round(factor * G1 / 255)) * (255 / factor);                                    //->O(1)
                    B2 = Convert.ToInt32(Math.Round(factor * B1 / 255)) * (255 / factor);                                    //->O(1)
                    Buffer[j, i].red   = (byte)R2;                                                                           //->O(1)
                    Buffer[j, i].green = (byte)G2;                                                                           //->O(1)
                    Buffer[j, i].blue  = (byte)B2;                                                                           //->O(1)
                    err_r = (int)R1 - R2;                                                                                    //->O(1)
                    err_g = (int)G1 - G2;                                                                                    //->O(1)
                    err_b = (int)B1 - B2;                                                                                    //->O(1)
                    // right
                    Buffer[j + 1, i].red   = (byte)(Buffer[j + 1, i].red + err_r * 7 / 16);                                  //->O(1)
                    Buffer[j + 1, i].green = (byte)(Buffer[j + 1, i].green + err_g * 7 / 16);                                //->O(1)
                    Buffer[j + 1, i].blue  = (byte)(Buffer[j + 1, i].blue + err_b * 7 / 16);                                 //->O(1)
                    gray_scale(ref Buffer[j + 1, i].red, ref Buffer[j + 1, i].green, ref Buffer[j + 1, i].blue);             //->O(1)
                                                                                                                             //below and to the left
                    Buffer[j - 1, i + 1].red   = (byte)(Buffer[j - 1, i + 1].red + err_r * 3 / 16);                          //->O(1)
                    Buffer[j - 1, i + 1].green = (byte)(Buffer[j - 1, i + 1].green + err_g * 3 / 16);                        //->O(1)
                    Buffer[j - 1, i + 1].blue  = (byte)(Buffer[j - 1, i + 1].blue + err_b * 3 / 16);                         //->O(1)
                    gray_scale(ref Buffer[j - 1, i + 1].red, ref Buffer[j - 1, i + 1].green, ref Buffer[j - 1, i + 1].blue); //->O(1)
                    // below
                    Buffer[j, i + 1].red   = (byte)(Buffer[j, i + 1].red + err_r * 5 / 16);                                  //->O(1)
                    Buffer[j, i + 1].green = (byte)(Buffer[j, i + 1].green + err_g * 5 / 16);                                //->O(1)
                    Buffer[j, i + 1].blue  = (byte)(Buffer[j, i + 1].blue + err_b * 5 / 16);                                 //->O(1)
                    gray_scale(ref Buffer[j, i + 1].red, ref Buffer[j, i + 1].green, ref Buffer[j, i + 1].blue);             //->O(1)
                    // below and to right
                    Buffer[j + 1, i + 1].red   = (byte)(Buffer[j + 1, i + 1].red + err_r * 1 / 16);                          //->O(1)
                    Buffer[j + 1, i + 1].green = (byte)(Buffer[j + 1, i + 1].green + err_g * 1 / 16);                        //->O(1)
                    Buffer[j + 1, i + 1].blue  = (byte)(Buffer[j + 1, i + 1].blue + err_b * 1 / 16);                         //->O(1)
                    gray_scale(ref Buffer[j + 1, i + 1].red, ref Buffer[j + 1, i + 1].green, ref Buffer[j + 1, i + 1].blue); //->O(1)
                }
            }

            return(Buffer); //->O(1)
        }
Ejemplo n.º 27
0
        private void button5_Click(object sender, EventArgs e)
        {
            OpenFileDialog op = new OpenFileDialog();

            if (op.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string fileName = op.FileName;

                // create instance of video reader
                VideoFileReader reader = new VideoFileReader();
                // open video file
                reader.Open(fileName);
                // read 100 video frames out of it
                for (int i = 0; i <= 1312; i++)
                {
                    Bitmap videoFrame = reader.ReadVideoFrame();
                    Program.images_name[Program.index_vedio] = "C:\\Users\\JOE\\Desktop\\[TEMPLATE] ImageEncryptCompress\\Take Pictures\\" + i.ToString();
                    Program.index_vedio++;
                    videoFrame.Save(@"C:\\Users\\JOE\\Desktop\\[TEMPLATE] ImageEncryptCompress\Take Pictures\\" + i.ToString() + ".bmp");
                    // dispose the frame when it is no longer required
                    videoFrame.Dispose();
                }
                reader.Close();
            }

            Huffman_Tree tree = new Huffman_Tree();

            Huffman_Tree.Comp   = new FileStream("Compressed_Picture.bin", FileMode.Append);
            Huffman_Tree.Comp_r = new BinaryWriter(Huffman_Tree.Comp);
            Huffman_Tree.Comp_r.Write(Program.index_vedio);
            Huffman_Tree.Comp_r.Close();
            Huffman_Tree.Comp.Close();
            for (int i = 0; i < Program.index_vedio; i++)
            {
                Huffman_Tree.Comp   = new FileStream("Compressed_Picture.bin", FileMode.Append);
                Huffman_Tree.Comp_r = new BinaryWriter(Huffman_Tree.Comp);
                string OpenedFilePath = Program.images_name[i];
                Program.OriginalImage = ImageOperations.OpenImage(OpenedFilePath + ".bmp");
                Width  = ImageOperations.GetWidth(Program.OriginalImage);
                Height = ImageOperations.GetHeight(Program.OriginalImage);
                tree.FreqMatrix(Program.OriginalImage);
                Compress c = new Compress();
                c.convert_Image_to_binary();
            }
            MessageBox.Show("DONE");
        }
Ejemplo n.º 28
0
        private void metroButton1_Click(object sender, EventArgs e)
        {
            g = new Graph();
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;
                ImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);
                txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
                txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
                g.Find_Distinct_colors(ImageMatrix);
                DistinctColors.Text = g.Distinct.Count.ToString();
            }
        }
Ejemplo n.º 29
0
        private void btnDisplay_Click(object sender, EventArgs e)
        {
            var stopwatch      = Stopwatch.StartNew();
            int imageWidth     = ImageOperations.GetWidth(ImageMatrix);  //O(1)
            int imageHeight    = ImageOperations.GetHeight(ImageMatrix); //O(1)
            int maxDistinctNum = 60000;                                  //Maximum number of distinic in the given test cases //O(1)

            //Dictionary that contains MinSpanningTree
            Dictionary <int, KeyValuePair <int, double> > MSTree = new Dictionary <int, KeyValuePair <int, double> >(maxDistinctNum);//O(1)

            //Helper Dictionary for keeping struct values concatenated as integer (Key) and the struct as Value
            Dictionary <int, RGBPixel> distinctHelper = new Dictionary <int, RGBPixel>(maxDistinctNum);                        //O(1)
            //Checks visited nodes in minimum spanning tree
            Dictionary <int, bool> visited = new Dictionary <int, bool>(maxDistinctNum);                                       //O(1)
            int color = 0;                                                                                                     //O(1)
            //Any node that we start from the Minimum Spanning
            KeyValuePair <int, KeyValuePair <int, double> > minVertix = new KeyValuePair <int, KeyValuePair <int, double> >(); //O(1)
            //O(D^2)
            int noDistinctColors = MST.FindDistinctColors(ImageMatrix, ref MSTree, ref distinctHelper, ref visited, ref color, ref minVertix);
            //List that contains mimimum spanning edges sorted
            List <KeyValuePair <double, int> > edges = new List <KeyValuePair <double, int> >(maxDistinctNum);              //O(1)
            double MST_Sum = MST.FindMinimumSpanningTree(ref MSTree, distinctHelper, visited, color, minVertix, ref edges); //O(D^2)

            int k = int.Parse(txtGetK.Text);                                                                                //O(N)
            //Dictionary carries each original color as key and representative color of each cluster as value
            Dictionary <int, RGBPixel> represntativeColor = new Dictionary <int, RGBPixel>(maxDistinctNum);
            //O(K+D) = O(D)
            Dictionary <int, List <int> > clusteredGraph = Clustering.ProduceKClusters(k, edges, MSTree, maxDistinctNum, visited);

            //O(E+V) =O(V) = O(D)
            Clustering.DFS(clusteredGraph, visited, maxDistinctNum, k, ref represntativeColor, distinctHelper);
            //O(N^2)
            MST.Coloring(ref ImageMatrix, represntativeColor);

            ImageOperations.DisplayImage(ImageMatrix, pictureBox2);
            stopwatch.Stop();
            double Miliseconds = (stopwatch.Elapsed.TotalMilliseconds);

            txtMili.Text = Miliseconds.ToString(); //O(1)
            double Seconds = (stopwatch.Elapsed.TotalSeconds);

            txtSec.Text      = Seconds.ToString();          //O(1)
            txtDistinct.Text = noDistinctColors.ToString(); //O(1)
            txtMST.Text      = MST_Sum.ToString();          //O(1)
        }
Ejemplo n.º 30
0
 public ImageQuantizer(RGBPixel[,] imageMatrix)
 {
     hashDistinctColors = new HashSet <int>();
     distanceFromWhite  = new List <KeyValuePair <int, int> >();
     MstGraph           = new List <List <KeyValuePair <int, double> > >();
     distinctColors     = new List <int>();
     height             = ImageOperations.GetHeight(imageMatrix);
     width = ImageOperations.GetWidth(imageMatrix);
     quantizedImageMatrix = new RGBPixel[height, width];
     for (int i = 0; i < height; ++i)
     {
         for (int j = 0; j < width; ++j)
         {
             RGBPixel pix = imageMatrix[i, j];
             quantizedImageMatrix[i, j] = new RGBPixel(pix.red, pix.green, pix.blue);
         }
     }
 }