Example #1
0
//########################################################################################################################################################
        public static void decomprassion2(ref RGBPixel[,] ImageMatrix,
                                          List <string> r_code, List <string> g_code, List <string> b_code, List <int> r_v, List <int> g_v, List <int> b_v)
        {
            char         ch;
            StreamReader reader;
            FileInfo     ff     = new FileInfo("comprass.txt");
            int          gg     = (int)ff.Length;
            BitArray     b2     = new BitArray(gg * 8);
            int          index1 = 0;
            int          index  = -1;

            reader = new StreamReader("comprass.txt");
            while (!reader.EndOfStream)
            {
                ch = (char)reader.Read();
                byte     bn = (byte)ch;
                BitArray bc = new BitArray(new byte[] { bn });
                for (int ii = 0; ii < 8; ii++)
                {
                    b2[index1] = bc[ii];
                    index1++;
                }
            }
            reader.Close();
            for (int i = 0; i < ImageMatrix.GetLength(0); i++)
            {
                for (int f = 0; f < ImageMatrix.GetLength(1); f++)
                {
                    ImageMatrix[i, f].red   = decomprss2(r_code, r_v, b2, ref index);
                    ImageMatrix[i, f].green = decomprss2(g_code, g_v, b2, ref index);
                    ImageMatrix[i, f].blue  = decomprss2(b_code, b_v, b2, ref index);
                }
            }
        }
        public HuffmanTree(RGBPixel[,] Image, long seed, short tap, short size)
        {
            //initializations
            total_memory_bytes = 0;
            RedFreqs           = new int[256];
            GreenFreqs         = new int[256];
            BlueFreqs          = new int[256];
            huffmancodesred    = new string[256];
            huffmancodesgreen  = new string[256];
            huffmancodesblue   = new string[256];
            RedNodes           = new PriorityQueue <int, Node>();
            GreenNodes         = new PriorityQueue <int, Node>();
            BlueNodes          = new PriorityQueue <int, Node>();

            total_memory_bytes = 3 * Image.GetLength(0) * Image.GetLength(1);
            for (int i = 0; i < Image.GetLength(0); i++)
            {
                for (int j = 0; j < Image.GetLength(1); j++)
                {
                    RedFreqs[Image[i, j].red]++;
                    GreenFreqs[Image[i, j].green]++;
                    BlueFreqs[Image[i, j].blue]++;
                }
            }
            ConstructHuffmanTree();
            Print_log(Image, seed, tap, size);
        }
Example #3
0
        /// <summary>
        /// Display the given image on the given PictureBox object
        /// </summary>
        /// <param name="ImageMatrix">2D array that contains the image</param>
        /// <param name="PicBox">PictureBox object to display the image on it</param>
        public static void DisplayImage(RGBPixel[,] ImageMatrix, PictureBox PicBox)
        {
            // Create Image:
            //==============
            int Height = ImageMatrix.GetLength(0);
            int Width  = ImageMatrix.GetLength(1);

            Bitmap ImageBMP = new Bitmap(Width, Height, PixelFormat.Format24bppRgb);

            unsafe
            {
                BitmapData bmd    = ImageBMP.LockBits(new Rectangle(0, 0, Width, Height), ImageLockMode.ReadWrite, ImageBMP.PixelFormat);
                int        nWidth = 0;
                nWidth = Width * 3;
                int   nOffset = bmd.Stride - nWidth;
                byte *p       = (byte *)bmd.Scan0;
                for (int i = 0; i < Height; i++)
                {
                    for (int j = 0; j < Width; j++)
                    {
                        p[2] = ImageMatrix[i, j].red;
                        p[1] = ImageMatrix[i, j].green;
                        p[0] = ImageMatrix[i, j].blue;
                        p   += 3;
                    }

                    p += nOffset;
                }
                ImageBMP.UnlockBits(bmd);
            }
            PicBox.Image = ImageBMP;
        }
Example #4
0
        private void btnOpen_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                reset();
                init();

                //Open the browsed image and display it
                string OpenedFilePath = openFileDialog1.FileName;
                ImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
                helper.set_boundries(ImageMatrix.GetLength(0), ImageMatrix.GetLength(1));  // row , col

                /*
                 *  I think this step just have the aim of getting the pixel of where the
                 */
                ImageOperations.DisplayImage(ImageMatrix, pictureBox1);

                int sz = ImageMatrix.GetLength(0) * ImageMatrix.GetLength(1);
                g = new graph(sz);                     // special case of init();

                g.set_width(ImageMatrix.GetLength(1)); // 1 is width = number of columns

                //g.build(ImageMatrix);  // cuz time delay
                ok = true;
            }
            // GUI Stuff
            txtWidth.Text  = ImageOperations.GetWidth(ImageMatrix).ToString();
            txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString();
        }
Example #5
0
        private void computeFrequencies(RGBPixel[,] arr, ref Dictionary <byte, int> temp1, ref Dictionary <byte, int> temp2, ref Dictionary <byte, int> temp3)
        {
            for (int i = 0; i < arr.GetLength(0); i++)
            {
                for (int j = 0; j < arr.GetLength(1); j++)
                {
                    if (temp1.ContainsKey(arr[i, j].red))
                    {
                        temp1[arr[i, j].red]++;
                    }
                    else
                    {
                        temp1.Add(arr[i, j].red, 1);
                    }

                    if (temp2.ContainsKey(arr[i, j].blue))
                    {
                        temp2[arr[i, j].blue]++;
                    }
                    else
                    {
                        temp2.Add(arr[i, j].blue, 1);
                    }

                    if (temp3.ContainsKey(arr[i, j].green))
                    {
                        temp3[arr[i, j].green]++;
                    }
                    else
                    {
                        temp3.Add(arr[i, j].green, 1);
                    }
                }
            }
        }
        public static void get_Distincit(RGBPixel[,] M)//function to get the distincit nodes and take given matrix of image
        {
            /*
             * Total complexty = theta (w*h) = theta (N^2)
             */
            nodes = new List <int>();                     //intitialize the distincit list
            HashSet <int> Vertices = new HashSet <int>(); //hashset to check if node is already exist

            MST = new List <GRAPH>();                     //initialize Minimum spaning tree list
            int h = M.GetLength(0), w = M.GetLength(1);   // get width and height of image

            for (int i = 0; i < h; i++)
            {
                for (int j = 0; j < w; j++)
                {
                    int ind1 = M[i, j].red << 8 | M[i, j].green;
                    int ind  = ind1 << 8 | M[i, j].blue;

                    /*shifting the red and green and blue in integer and work as integer this is much faster than
                     * making it RGBPixel due to much of collision of hashset as it use hash function */
                    if (Vertices.Add(ind)) //if this node is not already taken before then add it to hashset and
                    {
                        nodes.Add(ind);    // add it as it is distincit color
                        MST.Add(new GRAPH(ind, ind, (float)10000000));
                        //initialize itself in Minimum spanning tree as it go from itself to itself with large value
                    } //if
                }     //inner for
            }         //outer for
        }             //get_distincit
Example #7
0
        public static int get_distinct(ref RGBPixel[,] Buffer)
        {
            bool[, ,] visited_buffer = new bool[256, 256, 256];
            RGBPixel new_one;

            distinct = new List <RGBPixel>();

            int rw = Buffer.GetLength(0);
            int cl = Buffer.GetLength(1);

            for (int row = 0; row < rw; row++)
            {
                for (int col = 0; col < cl; col++)
                {
                    new_one = Buffer[row, col];
                    if (visited_buffer[new_one.red, new_one.green, new_one.blue] == false)
                    {
                        visited_buffer[new_one.red, new_one.green, new_one.blue] = true;
                        distinct.Add(new_one);
                    }
                }
            }
            MessageBox.Show(distinct.Count.ToString());
            return(distinct.Count);
        }
Example #8
0
        /// <summary>
        /// Gets the distinct colors from the matrix of colors
        /// </summary>
        /// <returns>Set of colors</returns>
        public static void GetDistinct(RGBPixel[,] M)
        {
            width             = M.GetLength(0);
            height            = M.GetLength(1);
            distinctHashtable = new Hashtable();

            distinctColors = new RGBPixel[width * height];

            vertixNumber = new int[width, height];

            int hashFunctionKey = 0;
            int Counter         = 0;

            for (int i = 0; i < M.GetLength(0); i++)
            {
                for (int j = 0; j < M.GetLength(1); j++)
                {
                    // Calculates the Key of the hashtable from three integers (red,blue,green)
                    hashFunctionKey = 8 * (M[i, j].green + M[i, j].blue) * (M[i, j].green + M[i, j].blue + 1) + M[i, j].blue;
                    hashFunctionKey = 5 * (hashFunctionKey + M[i, j].red) * (hashFunctionKey + M[i, j].red + 1) + M[i, j].red;

                    if (!distinctHashtable.ContainsKey(hashFunctionKey))
                    {
                        distinctHashtable.Add(hashFunctionKey, Counter);
                        distinctColors[Counter] = M[i, j];
                        Counter++;
                    }
                    vertixNumber[i, j] = (int)distinctHashtable[hashFunctionKey];
                }
            }
        }
        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[, ];
        }
        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);
        }
Example #11
0
 public static void make_freq(RGBPixel[,] ImageMatrix, ref List <node> fred, ref List <node> fgreen, ref List <node> fblue)
 {
     for (int i = 0; i < ImageMatrix.GetLength(0); i++)
     {
         for (int g = 0; g < ImageMatrix.GetLength(1); g++)
         {
             bool k = false;
             int  x = (int)ImageMatrix[i, g].red;
             for (int y = 0; y < fred.Count; y++)
             {
                 int ww = (int)fred[y].value;
                 if (ww == x)
                 {
                     fred[y].freq++;
                     k = true;
                     break;
                 }
             }
             if (k == false)
             {
                 node temp = new node(x, 1);
                 fred.Add(temp);
             }
             k = false;
             x = (int)ImageMatrix[i, g].green;
             for (int y = 0; y < fgreen.Count; y++)
             {
                 if (fgreen[y].value == x)
                 {
                     fgreen[y].freq++;
                     k = true;
                     break;
                 }
             }
             if (k == false)
             {
                 node temp = new node(x, 1);
                 fgreen.Add(temp);
             }
             k = false;
             x = (int)ImageMatrix[i, g].blue;
             for (int y = 0; y < fblue.Count; y++)
             {
                 if (fblue[y].value == x)
                 {
                     fblue[y].freq++;
                     k = true;
                     break;
                 }
             }
             if (k == false)
             {
                 node temp = new node(x, 1);
                 fblue.Add(temp);
             }
         }
     }
 }
Example #12
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)
        }
Example #13
0
 /// <summary>
 /// Get the width of the image
 /// </summary>
 /// <param name="ImageMatrix">2D array that contains the image</param>
 /// <returns>Image Width</returns>
 public static int GetWidth(RGBPixel[,] ImageMatrix)
 {
     try
     {
         return(ImageMatrix.GetLength(1));
     }
     catch
     {
         MessageBox.Show("please enter the photo");
     }
     return(ImageMatrix.GetLength(1));
 }
Example #14
0
 private void computeFrequencies(RGBPixel[,] arr)
 {
     for (int i = 0; i < arr.GetLength(0); i++)
     {
         for (int j = 0; j < arr.GetLength(1); j++)
         {
             redFrequencies[arr[i, j].red]++;
             blueFrequencies[arr[i, j].blue]++;
             greenFrequencies[arr[i, j].green]++;
         }
     }
 }
Example #15
0
 public static void Gausssmooth(RGBPixel[,] Matrix)
 {
     for (int i = 0; i < Matrix.GetLength(0); i++)
     {
         for (int j = 0; j < Matrix.GetLength(1); j++)
         {
             double ky = 0.5 * (Matrix[i, j].red + Matrix[i, j].green) * (Matrix[i, j].red + Matrix[i, j].green + 1) + Matrix[i, j].green;
             ky           = 0.5 * (Matrix[i, j].blue + ky) * (Matrix[i, j].blue + ky + 1) + ky;
             Matrix[i, j] = colorDic[ky];
         }
     }
 }
Example #16
0
        // change group's coloros to its average color

        public static void UpdatedMatrix(RGBPixel[, ,] Update, RGBPixel[,] Matrix)
        {
            int row = Matrix.GetLength(0);
            int col = Matrix.GetLength(1);

            for (int i = 0; i < row; i++)
            {
                for (int j = 0; j < col; j++)
                {
                    Matrix[i, j] = Update[Matrix[i, j].red, Matrix[i, j].blue, Matrix[i, j].green];
                }
            }
        }
        //find distnict colors in image matrix
        public static void Diffcolors(RGBPixel[,] ImageMatrix)
        {
            int length = ImageMatrix.GetLength(0);
            int width  = ImageMatrix.GetLength(1);

            bool[,,] vis = new bool[257, 257, 257];
            for (int i = 0; i < length; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    vis[ImageMatrix[i, j].blue, ImageMatrix[i, j].green, ImageMatrix[i, j].red] = true;
                }
            }

            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    for (int k = 0; k < 256; k++)
                    {
                        if (vis[i, j, k])
                        {
                            colorsNum++;
                        }
                    }
                }
            }


            colors = new RGBPixel[colorsNum];
            int count = 0;

            for (int i = 0; i < 256; i++)
            {
                for (int j = 0; j < 256; j++)
                {
                    for (int k = 0; k < 256; k++)
                    {
                        if (vis[i, j, k])
                        {
                            var p = new RGBPixel();
                            p.blue          = (byte)i;
                            p.green         = (byte)j;
                            p.red           = (byte)k;
                            colors[count++] = p;
                        }
                    }
                }
            }
        }
Example #18
0
        public static YCbCrPixel[,] toYCCMatrix(RGBPixel[,] source)
        {
            int rows    = source.GetLength(0);
            int columns = source.GetLength(1);

            YCbCrPixel[,] result = new YCbCrPixel[rows, columns];
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    result[i, j] = source[i, j].ToYCC();
                }
            }
            return(result);
        }
        public static RGBPixel[,] QuantizedImage(RGBPixel[,] imageMatrix, List <RGBPixel> palette)
        {
            int height = imageMatrix.GetLength(0);
            int width  = imageMatrix.GetLength(1);

            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width; j++)
                {
                    imageMatrix[i, j] = calculateWeight(palette, imageMatrix[i, j]);
                }
            }


            return(imageMatrix);
        }
Example #20
0
        public object FastEncryptionWithCompression(ref KeyValuePair <string, int> key, ref RGBPixel[,] imageSource,
                                                    ref long[,] mFrequencies)
        {
            int imageHeight = imageSource.GetLength(0),
                imageWidth  = imageSource.GetLength(1);

            Int64 shiftKey    = ConvertToInt(key.Key);
            int   tapPosition = key.Value;
            // contains the length of {0 and 1} in the key to know the last bit index
            int lastPosition = key.Key.Length;

            RGBPixel[,] targetImage = new RGBPixel[imageHeight, imageWidth];
            for (int i = 0; i < imageHeight; i++)
            {
                for (int j = 0; j < imageWidth; j++)
                {
                    if (imageSource != null)
                    {
                        byte redValue   = imageSource[i, j].Red;
                        byte greenValue = imageSource[i, j].Green;
                        byte blueValue  = imageSource[i, j].Blue;

                        // get new shifted key
                        shiftKey = FastShiftKey(shiftKey, lastPosition, tapPosition);
                        // XOR the new value of key with the color values
                        redValue ^= (byte)(shiftKey & 0xff);

                        shiftKey    = FastShiftKey(shiftKey, lastPosition, tapPosition);
                        greenValue ^= (byte)(shiftKey & 0xff);

                        shiftKey   = FastShiftKey(shiftKey, lastPosition, tapPosition);
                        blueValue ^= (byte)(shiftKey & 0xff);

                        // set the new pixel colors
                        targetImage[i, j] = new RGBPixel(redValue, greenValue, blueValue);
                        if (mFrequencies.GetLength(0) > 1)
                        {
                            mFrequencies[0, redValue]++;
                            mFrequencies[1, greenValue]++;
                            mFrequencies[2, blueValue]++;
                        }
                    }
                }
            }

            return(targetImage);
        }
Example #21
0
        public void Find_Distinct_colors(RGBPixel[,] ImageMatrix)
        {
            bool[,,] Histogram = new bool[256, 256, 256];

            for (int i = 0; i < ImageMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < ImageMatrix.GetLength(1); j++)
                {
                    if (!Histogram[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue])
                    {
                        Distinct.Add(ImageMatrix[i, j]);
                    }
                    Histogram[ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue] = true;
                }
            }
            MessageBox.Show(Distinct.Count.ToString());
        }
Example #22
0
        // total function's complexity --> Exact(E + D) --> E = number of edges in cluster , D = number of Distinct colors in this cluster



        /// <summary>
        /// replace the color of each pixal in the image matrix array
        /// with its represented color in the pallete
        /// </summary>
        /// <param name="ImageMatrix"></param>
        /// <param name="pallete"></param>
        public static void  Quntization(RGBPixel[,] ImageMatrix, RGBPixelD[,,] pallete)
        {
            int width  = ImageMatrix.GetLength(1);                           // O(1)
            int height = ImageMatrix.GetLength(0);                           // O(1)

            for (int i = 0; i < height; i++)                                 // E(N) --> N = height
            {
                for (int j = 0; j < width; j++)                              // E(N) --> N = width
                {
                    var index = ImageMatrix[i, j];                           // O(1)
                    var col   = pallete[index.red, index.blue, index.green]; // O(1)
                    ImageMatrix[i, j].red   = (byte)col.red;                 // O(1)
                    ImageMatrix[i, j].green = (byte)col.green;               // O(1)
                    ImageMatrix[i, j].blue  = (byte)col.blue;                // O(1)
                }
            }
        }//Total_complexity = E(N^2) --> N = width or height
Example #23
0
 public static int distinctColor(RGBPixel[,] Matrix)
 {
     for (int i = 0; i < Matrix.GetLength(0); i++)
     {
         for (int j = 0; j < Matrix.GetLength(1); j++)
         {
             double ky = 0.5 * (Matrix[i, j].red + Matrix[i, j].green) * (Matrix[i, j].red + Matrix[i, j].green + 1) + Matrix[i, j].green;
             ky = 0.5 * (Matrix[i, j].blue + ky) * (Matrix[i, j].blue + ky + 1) + ky;
             if (my_hashtable.ContainsKey(ky) != true)
             {
                 my_hashtable.Add(ky, 1);
                 distinct.Add(Matrix[i, j]);
             }
         }
     }
     return(distinct.Count);
 }
Example #24
0
        public static int GetDistinctColors(RGBPixel[,] Buffer)
        {
            bool[,,] Visited_Buffer = new bool[256, 256, 256];

            DistinctColours = new List <RGBPixel>();

            for (int i = 0; i < Buffer.GetLength(0); i++)
            {
                for (int j = 0; j < Buffer.GetLength(1); j++)
                {
                    if (Visited_Buffer[Buffer[i, j].red, Buffer[i, j].green, Buffer[i, j].blue] == false)
                    {
                        Visited_Buffer[Buffer[i, j].red, Buffer[i, j].green, Buffer[i, j].blue] = true;
                        DistinctColours.Add(Buffer[i, j]);
                    }
                }
            }
            return(DistinctColours.Count);
        }
Example #25
0
        /// <summary>
        /// Display the given image on the given PictureBox object
        /// </summary>
        /// <param name="ImageMatrix">2D array that contains the image</param>
        /// <param name="PicBox">PictureBox object to display the image on it</param>
        public static void DisplayImage(RGBPixel[,] ImageMatrix, PictureBox PicBox)
        {
            // Create Image:
            //==============
            int Height = ImageMatrix.GetLength(0);
            int Width  = ImageMatrix.GetLength(1);

            Bitmap ImageBMP = new Bitmap(Width, Height, PixelFormat.Format32bppArgb);

            for (int i = 0; i < Height; i++)
            {
                for (int j = 0; j < Width; j++)
                {
                    Color Pixel = Color.FromArgb(ImageMatrix[i, j].red, ImageMatrix[i, j].green, ImageMatrix[i, j].blue);
                    ImageBMP.SetPixel(j, i, Pixel);
                }
            }

            PicBox.Image = ImageBMP;
        }
 public void QuantizeImage(List <RGBPixel> palette, RGBPixel[,] ImageMatrix)
 {
     Quantized_Image = new RGBPixel[ImageMatrix.GetLength(0), ImageMatrix.GetLength(1)];
     Updated_Colours = new RGBPixel[256, 256, 256];
     for (int i = 0; i < NewColors.Count; i++)
     {
         int groupSize = NewColors[i].Count;
         for (int j = 0; j < groupSize; j++)
         {
             Updated_Colours[ImageUtilities.DistinctColours[NewColors[i][j]].red, ImageUtilities.DistinctColours[NewColors[i][j]].blue, ImageUtilities.DistinctColours[NewColors[i][j]].green] = Palette[i];
         }
     }
     for (int i = 0; i < ImageMatrix.GetLength(0); i++)
     {
         for (int j = 0; j < ImageMatrix.GetLength(1); j++)
         {
             Quantized_Image[i, j] = Updated_Colours[ImageMatrix[i, j].red, ImageMatrix[i, j].blue, ImageMatrix[i, j].green];
         }
     }
 }
        }//Clustring

        public static RGBPixel[,] Refill_Mtrx(RGBPixel[,] M)//refill the matrix again to get it back to image
        {
            int h = M.GetLength(0), w = M.GetLength(1), d1, d2; //get width and height of matrix

            for (int i = 0; i < h; i++)                         //height
            {
                for (int j = 0; j < w; j++)                     //width
                {
                    /*shift this cell to know its cluster*/
                    d1 = M[i, j].red << 8 | M[i, j].green;
                    d2 = d1 << 8 | M[i, j].blue;
                    byte[] b = BitConverter.GetBytes(nodes[Node_Cluster[d2]]);//split this cluster average into red and green and blue
                    //regive this value with its cluster average
                    M[i, j].red   = b[2];
                    M[i, j].green = b[1];
                    M[i, j].blue  = b[0];
                }
            }

            return(M);// return this matrix
        }
Example #28
0
 /// <summary>
 /// Get the width of the image
 /// </summary>
 /// <param name="ImageMatrix">2D array that contains the image</param>
 /// <returns>Image Width</returns>
 public static int GetWidth(RGBPixel[,] ImageMatrix)
 {
     try
     {
         return(ImageMatrix.GetLength(1));
     }
     catch
     {
         MessageBox.Show("No Image were added.");
         return(0);
     }
 }
Example #29
0
        // build the graph
        public void build(RGBPixel [,] image)
        {
            int r = image.GetLength(0);
            int c = image.GetLength(1);

            for (int i = 0; i < r; i++)
            {
                for (int j = 0; j < c; j++)
                {
                    Vector2D w = ImageOperations.CalculatePixelEnergies(j, i, image); // energy (right , bellow)
                    if (i + 1 < r)                                                    // down child
                    {
                        add_edge(j, i, j, i + 1, w.Y);
                    }
                    if (j + 1 < c) // right child
                    {
                        add_edge(j, i, j + 1, i, w.X);
                    }
                }
            }
        }
        public void ChangeMatrixColor(Hashtable FinalColor)
        {
            for (int i = 0; i < ImageMatrix.GetLength(0); i++)
            {
                for (int j = 0; j < ImageMatrix.GetLength(1); j++)
                {
                    String key;                                   //O(1)
                    String C1 = ImageMatrix[i, j].red.ToString(); //O(1)
                    if (C1.Length == 1)
                    {
                        C1 = "00" + C1;
                    }                                       //O(1)
                    if (C1.Length == 2)
                    {
                        C1 = "0" + C1;
                    }                                              //O(1)
                    String C2 = ImageMatrix[i, j].blue.ToString(); //O(1)
                    if (C2.Length == 1)
                    {
                        C2 = "00" + C2;
                    }                                       //O(1)
                    if (C2.Length == 2)
                    {
                        C2 = "0" + C2;
                    }                                               //O(1)
                    String C3 = ImageMatrix[i, j].green.ToString(); //O(1)
                    if (C3.Length == 1)
                    {
                        C3 = "00" + C3;
                    }                                       //O(1)
                    if (C3.Length == 2)
                    {
                        C3 = "0" + C3;
                    }                                      //O(1)
                    key = C1 + C2 + C3;

                    ImageMatrix[i, j] = (RGBPixel)FinalColor[key]; //O(1)
                }
            }
        }