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 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)
        }
        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)
        }
示例#4
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            pictureBox2.Visible = true;
            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, pictureBox2);
            }
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
            ImageOperations.construct(ImageMatrix);
            old = (RGBPixel[, ])ImageMatrix.Clone();

            /*for(int i=0; i< ImageOperations.GetHeight(ImageMatrix); i++)
             * {
             *  for (int j = 0; j < ImageOperations.GetWidth(ImageMatrix); j++)
             *      old[i, j] = ImageMatrix[i, j];
             * }*/
        }
示例#5
0
        /// <summary>
        /// Backtracking the path and color it
        /// </summary>
        /// <param name="xd">pixel x-coordinate for distination</param>
        /// <param name="yd">pixel y-coordinate for distenation</param>
        /// <param name="grid">colored image matrix</param>
        /// <returns>2D RGBPixel array hold the image after coloring</returns>
        public static RGBPixel[,] color_path(int xd, int yd, RGBPixel[,] grid, int fr)
        {
            Pathtime = Stopwatch.StartNew();
            List <KeyValuePair <int, int> > path = new List <KeyValuePair <int, int> >();
            int  i = yd, j = xd;
            bool odd = false;
            // o(1)
            int      h = GetHeight(grid);
            int      w = GetWidth(grid);
            RGBPixel W, B;

            RGBPixel[,] ret = (RGBPixel[, ])grid.Clone();
            W.blue          = 255;
            W.green         = 255;
            W.red           = 255;
            B.red           = 0;
            B.green         = 0;
            B.blue          = 0;
            int c = 0;

            // o (n) (n length of the path)
            while (true)
            {
                c++;
                if (odd == false)
                {
                    ret[i, j] = B;
                }
                else
                {
                    ret[i, j] = W;
                }
                if (c % 5 == 0)
                {
                    odd ^= true;
                }
                path.Add(new KeyValuePair <int, int>(i, j));
                KeyValuePair <int, int> node = par[i, j];
                if (node.Key == -1)
                {
                    break;
                }
                i = node.Key;
                j = node.Value;
            }
            List <KeyValuePair <int, int> > l = new List <KeyValuePair <int, int> >();

            //O(N) (n length of the path)
            if (fr == 0)
            {
                if (Form1.setclick == true)
                {
                    put_anchor(path[path.Count - 1].Key, path[path.Count - 1].Value, grid);
                    Form1.ancY = path[path.Count - 1].Key;
                    Form1.ancX = path[path.Count - 1].Value;
                    for (int g = path.Count - 1; g >= 0; g--)
                    {
                        grid[path[g].Key, path[g].Value] = ret[path[g].Key, path[g].Value];
                    }
                    Form1.setclick = false;
                }
            }
            else
            {
                c = 0;
                //No.steps = 2N - > O(N) (n length of the path)
                for (int f = path.Count - 1, f1 = path.Count - 1; f >= 0; f--, c++)
                {
                    if (c % fr == 0)
                    {
                        //O(1)
                        put_anchor(path[f].Key, path[f].Value, grid);
                        Form1.ancY = path[f].Key;
                        Form1.ancX = path[f].Value;
                        //O(fr)
                        if (File.Exists("ShortestPath.txt"))
                        {
                            File.Delete("ShortestPath.txt");
                        }
                        using (StreamWriter outputfile = new StreamWriter("ShortestPath.txt"))
                        {
                            for (; f1 > f; f1--)
                            {
                                if (Form1.valid == true)
                                {
                                    outputfile.WriteLine("{X=" + path[f1].Value.ToString() + ",Y=" + path[f1].Key.ToString() + "},");
                                }
                                grid[path[f1].Key, path[f1].Value] = ret[path[f1].Key, path[f1].Value];
                            }
                            if (c == 50)
                            {
                                Form1.valid = false;
                                MessageBox.Show("end");
                            }
                        }
                    }
                }
            }
            Pathtime.Stop();
            return(ret);
        }
示例#6
0
        public static List <KeyValuePair <RGBPixel[, ], KeyValuePair <string, int> > > Hack(RGBPixel[,] ImageMatrix, string seed, int size) // complexity = O(N^2 * 2^size * size).
        {
            int hight = GetHeight(ImageMatrix);
            int width = GetWidth(ImageMatrix);

            int    avg = 0;
            string ss  = "";
            List <KeyValuePair <RGBPixel[, ], KeyValuePair <string, int> > > t = new List <KeyValuePair <RGBPixel[, ], KeyValuePair <string, int> > >();
            int n = Convert.ToInt16(seed, 2);

            RGBPixel[,] imagecpy = new RGBPixel[hight, width];



            for (int u = n; u <= Math.Pow(2, size) - 1; u++)
            {
                for (int a = 0; a < size; a++)
                {
                    RGBPixel[,] image = new RGBPixel[hight, width];
                    //seed = Convert.ToString(u, 2);
                    string after  = Convert.ToString(u, 2);
                    string before = "";
                    for (int k = 0; k < size - after.Length; k++)
                    {
                        before += "0";
                    }

                    seed = before + after;
                    ss   = seed;
                    int tab = a;
                    int coun1 = 0, coun2 = 0, coun3 = 0;
                    imagecpy = (RGBPixel[, ])ImageMatrix.Clone();
                    // int temp = 0;
                    //for (int y = 0; y < hight; y++)
                    //    for (int g = 0; g < width; g++)
                    //        imagecpy[y, g] = ImageMatrix[y, g];

                    Dictionary <int, int> Rvalues = new Dictionary <int, int>();
                    Dictionary <int, int> Gvalues = new Dictionary <int, int>();
                    Dictionary <int, int> Bvalues = new Dictionary <int, int>();

                    encrypt_image(imagecpy, seed, tab);
                    //if (u == 2)
                    //    return ImageMatrix;
                    for (int i = 0; i < hight; i++)
                    {
                        for (int j = 0; j < width; j++)
                        {
                            Rvalues[imagecpy[i, j].red]   = 0;
                            Gvalues[imagecpy[i, j].green] = 0;
                            Bvalues[imagecpy[i, j].blue]  = 0;
                        }
                    }

                    for (int i = 0; i < hight; i++)
                    {
                        for (int j = 0; j < width; j++)
                        {
                            //when exist
                            Rvalues[imagecpy[i, j].red]++;
                            Gvalues[imagecpy[i, j].green]++;
                            Bvalues[imagecpy[i, j].blue]++;
                        }
                    }

                    foreach (KeyValuePair <int, int> kvp in Rvalues)
                    {
                        coun1 += kvp.Value;
                    }
                    coun1 /= Rvalues.Count;
                    foreach (KeyValuePair <int, int> kvp in Gvalues)
                    {
                        coun2 += kvp.Value;
                    }
                    coun2 /= Gvalues.Count;
                    foreach (KeyValuePair <int, int> kvp in Bvalues)
                    {
                        coun3 += kvp.Value;
                    }
                    coun3 /= Bvalues.Count;
                    //   seed = Convert.ToString(f, 2);
                    //if (seed == "10001111")
                    //    MessageBox.Show("found");

                    if (coun1 + coun2 + coun3 == avg)
                    {
                        //if (seed == "1001" && tab == 1)
                        //{

                        //    MessageBox.Show("found");
                        //    //for (int y = 0; y < hight; y++)
                        //    //    for (int g = 0; g < width; g++)
                        //    //        image[y, g] = imagecpy[y, g];

                        //    //t.Add(image);
                        //    //return t;
                        //    //return ImageMatrix;
                        //}

                        //avg = coun1 + coun2 + coun3;
                        //for (int y = 0; y < hight; y++)
                        //    for (int g = 0; g < width; g++)
                        image = (RGBPixel[, ])imagecpy.Clone();
                        // if(!t.Contains(image))

                        KeyValuePair <RGBPixel[, ], KeyValuePair <string, int> > b = new KeyValuePair <RGBPixel[, ], KeyValuePair <string, int> >(image, new KeyValuePair <string, int>(seed, tab));
                        t.Add(b);
                        //ss = seed;
                    }
                    else if (coun1 + coun2 + coun3 > avg)
                    {
                        avg = coun1 + coun2 + coun3;
                        //for (int y = 0; y < hight; y++)
                        //    for (int g = 0; g < width; g++)
                        image = (RGBPixel[, ])imagecpy.Clone();


                        KeyValuePair <RGBPixel[, ], KeyValuePair <string, int> > b = new KeyValuePair <RGBPixel[, ], KeyValuePair <string, int> >(image, new KeyValuePair <string, int>(ss, tab));
                        t = new List <KeyValuePair <RGBPixel[, ], KeyValuePair <string, int> > >();
                        t.Add(b);
                    }
                }
            }

            return(t);
        }