public static RGBPixel[,] fill(List<Point> selected_points, RGBPixel[,] ImageMatrix) { Boundary bondry = GET_Boundary(selected_points); // Boundary of the main selection selected_image = Helper.COPY_Segment(ImageMatrix,bondry); // get croped image block_border(selected_points,bondry); // assign the selected point as blocks Flood_Fill(ImageOperations.GetWidth(selected_image) - 1, ImageOperations.GetHeight(selected_image) - 1); return selected_image; }
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); reset(); } txtWidth.Text = ImageOperations.GetWidth(ImageMatrix).ToString(); txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString(); }
public static void Build_Graph() { RGBPixel[,] Matrix = MainForm.ImageMatrix; int height = ImageOperations.GetHeight(Matrix); int width = ImageOperations.GetWidth(Matrix); Console.WriteLine(height + " " + width); //List<List<Tuple<int, double>>> adjlist = new List <List <Tuple <int, double> > >(new List <Tuple <int, double> > [width * height + 10]); for (int i = 0; i < adjlist.Count; ++i) { adjlist[i] = new List <Tuple <int, double> >(5); } //Building Graph for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { Vector2D energies = ImageOperations.CalculatePixelEnergies(j, i, Matrix); double Gx = 1 / energies.X, Gy = 1 / energies.Y; // creating node for every pixel int cur_node = generate_id(i, j, width); int X_next = generate_id(i, j + 1, width); int Y_next = generate_id(i + 1, j, width); //not border pixels if (i < height - 1 && j < width - 1) { adjlist.ElementAt(cur_node).Add(new Tuple <int, double>(X_next, Gx)); adjlist.ElementAt(cur_node).Add(new Tuple <int, double>(Y_next, Gy)); adjlist.ElementAt(X_next).Add(new Tuple <int, double>(cur_node, Gx)); adjlist.ElementAt(Y_next).Add(new Tuple <int, double>(cur_node, Gy)); } else { //border pixels if (i == height - 1 && j == width - 1) { //do nothing last pixel in photo } else if (i == height - 1) { adjlist[cur_node].Add(new Tuple <int, double>(X_next, Gx)); adjlist[X_next].Add(new Tuple <int, double>(cur_node, Gx)); } else { adjlist[cur_node].Add(new Tuple <int, double>(Y_next, Gy)); adjlist[Y_next].Add(new Tuple <int, double>(cur_node, Gy)); } } } } for (int i = 0; i < adjlist.Count; ++i) { Console.Write(i + " "); for (int j = 0; j < adjlist[i].Count; ++j) { Console.Write(adjlist[i][j].Item1 + " " + adjlist[i][j].Item2 + " "); } Console.WriteLine(); } }
private void btnOpen_Click(object sender, EventArgs e) { //PriorityQueue<KeyValuePair<double, KeyValuePair<RGBPixel, RGBPixel>>> P = //new PriorityQueue<KeyValuePair<double, KeyValuePair<RGBPixel, RGBPixel>>>((int)1e8); 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); List <RGBPixel> listOfDistColor = ExtractDistColors(ImageMatrix); } /* while (P.Size() > 0) * { * MessageBox.Show(P.Dequeue().Key.ToString()); * }*/ System.Diagnostics.Stopwatch sw = System.Diagnostics.Stopwatch.StartNew(); double ans = prim(listOfDistColor.Count, listOfDistColor); find_cal(); adjustMatrix(ref ImageMatrix); sw.Stop(); long time = sw.ElapsedMilliseconds; //OpenedFilePath = openFileDialog1.FileName; //ImageMatrix = ImageOperations.OpenImage(OpenedFilePath); ImageOperations.DisplayImage(ImageMatrix, pictureBox2); MessageBox.Show(ans.ToString() + " && " + listOfDistColor.Count.ToString()); MessageBox.Show("Time elapsed: " + time + "ms "); /*int k=3; * List<RGBPixel> centers = new List<RGBPixel>(); * Dictionary<int, int> dec = new Dictionary<int, int>(); * while (centers.Count < k) * { * Random rnd = new Random(); * int rd = rnd.Next(0, 1000000000)% listOfDistColor.Count; * if (dec[rd] == 0) * { * centers.Add(listOfDistColor[rd]); * dec[rd] = 1; * } * } * dec.Clear(); * List<int> myList = new List<int>(listOfDistColor.Count); * for(int ii = 0; ii < listOfDistColor.Count; ++ii) * { * myList[ii] = -1; * } * double Last_error = 0; * while (true) * { * for(int ii = 0; ii < listOfDistColor.Count; ++ii) * { * int cid = find_cal(listOfDistColor, listOfDistColor[ii]); * myList[ii]=cid; * * } * * }*/ txtWidth.Text = ImageOperations.GetWidth(ImageMatrix).ToString(); txtHeight.Text = ImageOperations.GetHeight(ImageMatrix).ToString(); }
public CropedImage(RGBPixel[,] cropedImage) { InitializeComponent(); ImageOperations.DisplayImage(cropedImage, pictureBox1); }
public static void huffman_encoding(RGBPixel[,] ImageMatrix) { Dictionary <int, int> Rvalues = new Dictionary <int, int>(); Dictionary <int, int> Gvalues = new Dictionary <int, int>(); Dictionary <int, int> Bvalues = new Dictionary <int, int>(); int hight = GetHeight(ImageMatrix); int width = GetWidth(ImageMatrix); ///initiallize dictionary with 0 for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { Rvalues[ImageMatrix[i, j].red] = 0; Gvalues[ImageMatrix[i, j].green] = 0; Bvalues[ImageMatrix[i, j].blue] = 0; } } for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { //when exist Rvalues[ImageMatrix[i, j].red]++; Gvalues[ImageMatrix[i, j].green]++; Bvalues[ImageMatrix[i, j].blue]++; } } HuffmanTree tree1 = new HuffmanTree(Rvalues); HuffmanTree tree2 = new HuffmanTree(Gvalues); HuffmanTree tree3 = new HuffmanTree(Bvalues); Dictionary <int, string> Redtree = tree1.CreateEncodings(); Dictionary <int, string> Greentree = tree2.CreateEncodings(); Dictionary <int, string> bluetree = tree3.CreateEncodings(); long Rcount = 0; long Gcount = 0; long Bcount = 0; FileStream fs = new FileStream("huffman_output.txt", FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine("--R--"); sw.WriteLine("Color - Frequency - Huffman Representation - Total Bits"); //red foreach (KeyValuePair <int, string> kvp in Redtree) { sw.Write(kvp.Key + " - " + Rvalues[kvp.Key] + " - " + kvp.Value + " - " + kvp.Value.Length * Rvalues[kvp.Key]); sw.WriteLine(); Rcount += kvp.Value.Length * Rvalues[kvp.Key]; } sw.WriteLine("*Total = " + Rcount); sw.WriteLine(); sw.WriteLine("--G--"); //green foreach (KeyValuePair <int, string> kvp in Greentree) { sw.Write(kvp.Key + " - " + Gvalues[kvp.Key] + " - " + kvp.Value + " - " + kvp.Value.Length * Gvalues[kvp.Key]); sw.WriteLine(); Gcount += kvp.Value.Length * Gvalues[kvp.Key]; } sw.WriteLine("*Total = " + Gcount); sw.WriteLine(); sw.WriteLine("--B--"); //blue foreach (KeyValuePair <int, string> kvp in bluetree) { sw.Write(kvp.Key + " - " + Bvalues[kvp.Key] + " - " + kvp.Value + " - " + kvp.Value.Length * Bvalues[kvp.Key]); sw.WriteLine(); Bcount += kvp.Value.Length * Bvalues[kvp.Key]; } double comp_output = (double)(Rcount + Gcount + Bcount) / 8; sw.WriteLine("*Total = " + Bcount); sw.WriteLine(); sw.WriteLine("**Compression Output**"); sw.WriteLine(comp_output + " bytes"); sw.WriteLine(); sw.WriteLine("**Compression Ratio**"); sw.WriteLine(Math.Round(comp_output / (hight * width * 3) * 100, 1) + "%"); sw.Close(); fs.Close(); MessageBox.Show("Huffman tree has been saved successflly !"); }
/// <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); }
public Cluster(List <RGBPixel> l, RGBPixel[,] matrix) { Colors = l; ImageMatrix = matrix; }
public static List <Edge> Get_neighbours(int Node_Index, RGBPixel[,] ImageMatrix) { List <Edge> neighbours = new List <Edge>(); int Height = ImageOperations.GetHeight(ImageMatrix); int Width = ImageOperations.GetWidth(ImageMatrix); //get x , y indices of the node var unflat = Helper.Unflatten(Node_Index, Width); int X = (int)unflat.X, Y = (int)unflat.Y; // calculate the gradient with right and bottom neighbour var Gradient = ImageOperations.CalculatePixelEnergies(X, Y, ImageMatrix); if (X < Width - 1) // have a right neighbour ? { //add to neighbours list with cost 1/G if (Gradient.X == 0) { neighbours.Add(new Edge(Node_Index, Helper.Flatten(X + 1, Y, Width), 10000000000000000)); } else { neighbours.Add(new Edge(Node_Index, Helper.Flatten(X + 1, Y, Width), 1 / (Gradient.X))); } } if (Y < Height - 1) // have a Bottom neighbour ? { //add to neighbours list with cost 1/G if (Gradient.Y == 0) { neighbours.Add(new Edge(Node_Index, Helper.Flatten(X, Y + 1, Width), 10000000000000000)); } else { neighbours.Add(new Edge(Node_Index, Helper.Flatten(X, Y + 1, Width), 1 / (Gradient.Y))); } } if (Y > 0) // have a Top neighbour ? { // calculate the gradient with top neighbour Gradient = ImageOperations.CalculatePixelEnergies(X, Y - 1, ImageMatrix); //add to neighbours list with cost 1/G if (Gradient.Y == 0) { neighbours.Add(new Edge(Node_Index, Helper.Flatten(X, Y - 1, Width), 10000000000000000)); } else { neighbours.Add(new Edge(Node_Index, Helper.Flatten(X, Y - 1, Width), 1 / (Gradient.Y))); } } if (X > 0) // have a Left neighbour ? { // calculate the gradient with left neighbour Gradient = ImageOperations.CalculatePixelEnergies(X - 1, Y, ImageMatrix); //add to neighbours list with cost 1/G if (Gradient.X == 0) { neighbours.Add(new Edge(Node_Index, Helper.Flatten(X - 1, Y, Width), 10000000000000000)); } else { neighbours.Add(new Edge(Node_Index, Helper.Flatten(X - 1, Y, Width), 1 / (Gradient.X))); } } return(neighbours); // return nei }
static int countB = 0; //Θ(1) public static void compress(RGBPixel[,] ImageMatrix, Huffman huffmanRed, Huffman huffmanGreen, Huffman huffmanBlue, string seed, short tap) { List <bool> tempRed = new List <bool>(ImageMatrix.GetLength(0) * ImageMatrix.GetLength(1) * 3); //Θ(1) List <bool> tempBlue = new List <bool>(ImageMatrix.GetLength(0) * ImageMatrix.GetLength(1)); //Θ(1) List <bool> tempGreen = new List <bool>(ImageMatrix.GetLength(0) * ImageMatrix.GetLength(1)); //Θ(1) BinaryWriter b = new BinaryWriter(File.Open(fileName, FileMode.Create)); //Θ(1) //Θ(N^2) for (int i = 0; i < ImageMatrix.GetLength(0); i++) { for (int j = 0; j < ImageMatrix.GetLength(1); j++) { tempRed.AddRange(huffmanRed.ColorsMap[ImageMatrix[i, j].red].Value); //Θ(1) tempGreen.AddRange(huffmanGreen.ColorsMap[ImageMatrix[i, j].green].Value); //Θ(1) tempBlue.AddRange(huffmanBlue.ColorsMap[ImageMatrix[i, j].blue].Value); //Θ(1) } } tempRed.AddRange(tempGreen); //Θ(N) n is size of tempGreen tempRed.AddRange(tempBlue); //Θ(N) n is size of tempBlue byte[] RedTree = Encoding.ASCII.GetBytes(huffmanRed.writeHuffman()); //Θ(N) N is length of String of "color,freq" byte[] greenTree = Encoding.ASCII.GetBytes(huffmanGreen.writeHuffman()); //Θ(N) N is length of String of "color,freq" byte[] blueTree = Encoding.ASCII.GetBytes(huffmanBlue.writeHuffman()); //Θ(N) N is length of String of "color,freq" byte[] redTreeLength = BitConverter.GetBytes(RedTree.Length); // length of int is 4 bytes so Θ(1) byte[] greenTreeLength = BitConverter.GetBytes(greenTree.Length); // length of int is 4 bytes so Θ(1) byte[] blueTreeLength = BitConverter.GetBytes(blueTree.Length); // length of int is 4 bytes so Θ(1) byte[] width = BitConverter.GetBytes(ImageMatrix.GetLength(0)); // length of int is 4 bytes so Θ(1) byte[] hight = BitConverter.GetBytes(ImageMatrix.GetLength(1)); // length of int is 4 bytes so Θ(1) byte[] seed2Length = BitConverter.GetBytes(seed.Length); // length of int is 4 bytes so Θ(1) byte[] seed2 = Encoding.ASCII.GetBytes(seed); //Θ(N) N is length of String of "color,freq"//Θ(N) N is length of String of "color,freq" byte[] tap1 = BitConverter.GetBytes(tap); // length of short is 2 bytes so Θ(1) byte[] redL = BitConverter.GetBytes(tempRed.Count); // length of int is 4 bytes so Θ(1) byte[] greenL = BitConverter.GetBytes(tempGreen.Count); // length of int is 4 bytes so Θ(1) byte[] blueL = BitConverter.GetBytes(tempBlue.Count); // length of int is 4 bytes so Θ(1) int fileLength = (RedTree.Length) + greenTree.Length + blueTree.Length + redTreeLength.Length + greenTreeLength.Length + blueTreeLength.Length + width.Length + hight.Length + seed2.Length + tap1.Length + seed2Length.Length + redL.Length + greenL.Length + blueL.Length; //Θ(1) List <Byte> bytes2 = new List <Byte>(fileLength); //Θ(1) bytes2.AddRange(redTreeLength); //Θ(1) n=4 bytes2.AddRange(RedTree); //Θ(N) n is size of redTree array bytes2.AddRange(greenTreeLength); //Θ(1) n=4 bytes2.AddRange(greenTree); //Θ(N) n is size of greenTree array bytes2.AddRange(blueTreeLength); //Θ(1) n=4 bytes2.AddRange(blueTree); //Θ(N) n is size of blueTree array bytes2.AddRange(width); //Θ(1) n=4 bytes2.AddRange(hight); //Θ(1) n=4 bytes2.AddRange(seed2Length); //Θ(N) n is size of tempGreen bytes2.AddRange(seed2); //Θ(1) n=4 bytes2.AddRange(tap1); //Θ(1) n=4 bytes2.AddRange(redL); //Θ(1) n=4 bytes2.AddRange(greenL); //Θ(1) n=4 bytes2.AddRange(blueL); //Θ(1) n=4 Byte[] bytes = new byte[tempRed.Count / 8 + (tempRed.Count % 8 == 0 ? 0 : 1)]; //Θ(1) BitArray d = new BitArray(tempRed.ToArray()); //Θ(n) n is size of tempRed d.CopyTo(bytes, 0); //Θ(N) n is size of Bytes array b.Write(bytes2.ToArray()); //Θ(N^2) n is size of Bytes array b.Write(bytes.ToArray()); //Θ(N) n is size of Bytes array b.Close(); //Θ(1) }
public static object FastCompressionWithPreEncryption(ref RGBPixel[,] sourceImage, ref long[,] mFrequencies) { // TODO throw new NotImplementedException(); }
private static RGBPixel[,] CalculateHarrisCorener(RGBPixel[,] Ix2, RGBPixel[,] Iy2, RGBPixel[,] IxIy, out RGBPixel Rmax) { int Height = ImageOperations.GetHeight(Ix2); int Width = ImageOperations.GetWidth(Ix2); RGBPixel[,] Result = new RGBPixel[Height, Width]; double[,] A = new double[2, 2]; Rmax = new RGBPixel(0, 0, 0); for (int i = 0; i < Height; i++) { for (int j = 0; j < Width; j++) { //red value A[0, 0] = Ix2[i, j].red; A[0, 1] = IxIy[i, j].red; A[1, 0] = IxIy[i, j].red; A[1, 1] = Iy2[i, j].red; double detA = Accord.Math.Matrix.Determinant(A); double tracA = Accord.Math.Matrix.Trace(A); Result[i, j].red = (byte)(detA - 0.01 * (tracA * tracA)); if (Result[i, j].red > Rmax.red) { Rmax.red = Result[i, j].red; } //green value A[0, 0] = Ix2[i, j].green; A[0, 1] = IxIy[i, j].green; A[1, 0] = IxIy[i, j].green; A[1, 1] = Iy2[i, j].green; detA = Accord.Math.Matrix.Determinant(A); tracA = Accord.Math.Matrix.Trace(A); Result[i, j].green = (byte)(detA - 0.01 * (tracA * tracA)); if (Result[i, j].green > Rmax.green) { Rmax.green = Result[i, j].green; } //blue value A[0, 0] = Ix2[i, j].blue; A[0, 1] = IxIy[i, j].blue; A[1, 0] = IxIy[i, j].blue; A[1, 1] = Iy2[i, j].blue; detA = Accord.Math.Matrix.Determinant(A); tracA = Accord.Math.Matrix.Trace(A); Result[i, j].blue = (byte)(detA - 0.01 * (tracA * tracA)); if (Result[i, j].blue > Rmax.blue) { Rmax.blue = Result[i, j].blue; } } } return(Result); }
public static MST MST_Weight(RGBPixel[,] ImageMatrix) { long size; //O(1) List <RGBPixel> color_list = List_of_Dstinected_Color(ImageMatrix); //O(1) List <Color> index_color = list_of_indexed_color(color_list); //O(1) List <Node> tree = new List <Node>(); //Exact(1) heap heap = new heap(); //O(1) size = color_list.Count; //O(1) bool[] k = new bool[size]; //O(1) int[] p = new int[size]; //O(1) double[] d = new double[size]; //O(1) for (int i = 1; i < size; i++) //O(N) --> N = number of distinct colors { k[i] = false; //O(1) d[i] = double.MaxValue; //O(1) p[i] = -1; //O(1) } //total loop -> O(N) --> N = number of distinct colors p[0] = -1; //O(1) d[0] = 0; //O(1) //node Node node0 = new Node(); //O(1) node0.index = -1; //O(1) node0.weight = 0; //O(1) node0.to = 0; //O(1) heap.insert(node0); //O(log(V)) while (!heap.empty()) //O(V) { //node Node node = heap.extract_Min(); //O(Log(V)) int index = node.to; //O(1) if (k[index] == true) //O(1) { continue; //O(1) } k[index] = true; //O(1) RGBPixel color1 = index_color[index].val; //O(1) int i = 0; //O(1) tree.Add(node); //O(1) foreach (var color2 in color_list) //O(V) --> V = Number of distinct colors { if (i != index) //O(1) { double Red_Diff = (color1.red - color2.red) * (color1.red - color2.red); //O(1) double Green_Diff = (color1.green - color2.green) * (color1.green - color2.green); //O(1) double Blue_Diff = (color1.blue - color2.blue) * (color1.blue - color2.blue); //O(1) double Total_Diff = Math.Sqrt(Red_Diff + Green_Diff + Blue_Diff); //O(1) if (k[i] == false && Total_Diff < d[i]) //O(1) { d[i] = Total_Diff; //O(1) p[i] = index; //O(1) Node node1 = new Node(); //O(1) node1.weight = Total_Diff; //O(1) node1.index = index; //O(1) node1.to = i; //O(1) heap.insert(node1); //O(Log(V)) } } i++; } //total loop --> O(V * Log(V)) } double weight = 0; //O(1) for (int i = 0; i < size; i++) //O(N) --> N = number of distinct colors { weight += d[i]; //O(1) } //total loop --> O(N) MST mST = new MST(weight, tree, p, index_color); //O(1) return(mST); //O(1) //total function's complexity ---> O(E * log V) }
public static long Get_Number_of_color(RGBPixel[,] ImageMatrix) { return(List_of_Dstinected_Color(ImageMatrix).Count);//O(1) //Total function's complexity = E(1) }
public static void Compress_image(RGBPixel[,] ImageMatrix, string seed, int tap) //O(N^2) { FileStream fs = new FileStream("Encrypted_image.bin", FileMode.Create); BinaryWriter bw = new BinaryWriter(fs); int hight = GetHeight(ImageMatrix); int width = GetWidth(ImageMatrix); bw.Write(Convert.ToUInt16(hight)); bw.Write(Convert.ToUInt16(width)); bw.Write(seed); bw.Write(Convert.ToUInt16(tap)); Dictionary <int, int> Rvalues = new Dictionary <int, int>(); Dictionary <int, int> Gvalues = new Dictionary <int, int>(); Dictionary <int, int> Bvalues = new Dictionary <int, int>(); ///initiallize dictionary with 0 for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { Rvalues[ImageMatrix[i, j].red] = 0; Gvalues[ImageMatrix[i, j].green] = 0; Bvalues[ImageMatrix[i, j].blue] = 0; } } for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { //when exist Rvalues[ImageMatrix[i, j].red]++; Gvalues[ImageMatrix[i, j].green]++; Bvalues[ImageMatrix[i, j].blue]++; } } bw.Write(Convert.ToUInt16(Rvalues.Count)); foreach (KeyValuePair <int, int> K in Rvalues) { byte[] bb = BitConverter.GetBytes(K.Value); bw.Write(Convert.ToByte(K.Key)); bw.Write(Convert.ToByte(bb[0])); bw.Write(Convert.ToByte(bb[1])); bw.Write(Convert.ToByte(bb[2])); } bw.Write(Convert.ToUInt16(Gvalues.Count)); foreach (KeyValuePair <int, int> K in Gvalues) { byte[] bb = BitConverter.GetBytes(K.Value); bw.Write(Convert.ToByte(K.Key)); bw.Write(Convert.ToByte(bb[0])); bw.Write(Convert.ToByte(bb[1])); bw.Write(Convert.ToByte(bb[2])); } bw.Write(Convert.ToUInt16(Bvalues.Count)); foreach (KeyValuePair <int, int> K in Bvalues) { byte[] bb = BitConverter.GetBytes(K.Value); bw.Write(Convert.ToByte(K.Key)); bw.Write(Convert.ToByte(bb[0])); bw.Write(Convert.ToByte(bb[1])); bw.Write(Convert.ToByte(bb[2])); } HuffmanTree tree1 = new HuffmanTree(Rvalues); HuffmanTree tree2 = new HuffmanTree(Gvalues); HuffmanTree tree3 = new HuffmanTree(Bvalues); Dictionary <int, string> Redtree = tree1.CreateEncodings(); Dictionary <int, string> Greentree = tree2.CreateEncodings(); Dictionary <int, string> Bluetree = tree3.CreateEncodings(); List <string> R_binstream = new List <string>(); List <string> G_binstream = new List <string>(); List <string> B_binstream = new List <string>(); for (int i = 0; i < hight; i++) { for (int j = 0; j < width; j++) { R_binstream.Add(Redtree[ImageMatrix[i, j].red]); G_binstream.Add(Greentree[ImageMatrix[i, j].green]); B_binstream.Add(Bluetree[ImageMatrix[i, j].blue]); } } byte[] b = ConvertStringByte(R_binstream).ToArray(); bw.Write(Convert.ToInt32(b.Length)); bw.Write(b); b = ConvertStringByte(G_binstream).ToArray(); bw.Write(Convert.ToInt32(b.Length)); bw.Write(b); b = ConvertStringByte(B_binstream).ToArray(); bw.Write(Convert.ToInt32(b.Length)); bw.Write(b); bw.Close(); fs.Close(); }
private void btnGaussSmooth_Click(object sender, EventArgs e) { double sigma = double.Parse(txtGaussSigma.Text); int maskSize = (int)nudMaskSize.Value; ImageMatrix = ImageOperations.GaussianFilter1D(ImageMatrix, maskSize, sigma); ImageOperations.DisplayImage(ImageMatrix, pictureBox2); }
private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { if (pictureBox1.Image != null) { var clicked_node = Helper.Flatten(e.X, e.Y, ImageOperations.GetWidth(ImageMatrix)); if (curr_source != clicked_node) { if (curr_source == -1) // in the first click save frist clicked anchor main_source = clicked_node; else Helper.AppendToList<Point>(Mainselction, curr_path); curr_source = clicked_node; AnchorPts.Add(e.Location); SB = new Boundary(); SB = ShortestPath_Operations.Square_Boundary(curr_source, ImageOperations.GetWidth(ImageMatrix) - 1, ImageOperations.GetHeight(ImageMatrix) - 1); //make a square segment Square_segment = Helper.COPY_Segment(ImageMatrix, SB); // currsrc in segment int newsrc = Helper.crosspond(curr_source, SB, ImageOperations.GetWidth(ImageMatrix), ImageOperations.GetWidth(Square_segment)); parent_list = ShortestPath_Operations.Dijkstra(newsrc, Square_segment); Autoancor.reset(); } } }
//######################################################################################################################################################## public static void comprassion(RGBPixel[,] ImageMatrix, StreamWriter writetext1) { using (StreamWriter writetext = new StreamWriter("width_high.txt")) { writetext.WriteLine(ImageMatrix.GetLength(0)); writetext.WriteLine(ImageMatrix.GetLength(1)); } List <int> red_v = new List <int>(); List <int> green_v = new List <int>(); List <int> blue_v = new List <int>(); List <string> red_code = new List <string>(); List <string> green_code = new List <string>(); List <string> blue_code = new List <string>(); string line; //************************************************* StreamReader file = new StreamReader("code_red.txt"); while ((line = file.ReadLine()) != null) { red_v.Add(int.Parse(line)); line = file.ReadLine(); red_code.Add(line); } file.Close(); //************************************************* StreamReader file1 = new StreamReader("code_green.txt"); while ((line = file1.ReadLine()) != null) { green_v.Add(int.Parse(line)); line = file1.ReadLine(); green_code.Add(line); } file1.Close(); //************************************************* StreamReader file2 = new StreamReader("code_blue.txt"); while ((line = file2.ReadLine()) != null) { blue_v.Add(int.Parse(line)); line = file2.ReadLine(); blue_code.Add(line); } file2.Close(); //************************************************* int idex = 0; BitArray b1 = new BitArray(1000000000); for (int i = 0; i < ImageMatrix.GetLength(0); i++) { for (int f = 0; f < ImageMatrix.GetLength(1); f++) { byte b = ImageMatrix[i, f].red; for (int o = 0; o < red_v.Count; o++) { if (red_v[o] == (int)b) { int y = 0; string temp = red_code[o]; while (y < temp.Length) { if (temp[y] == '0') { b1[idex] = false; } else { b1[idex] = true; } y++; idex++; } break; } } //************************************************* b = ImageMatrix[i, f].green; for (int o = 0; o < green_v.Count; o++) { if (green_v[o] == (int)b) { int y = 0; string temp = green_code[o]; while (y < temp.Length) { if (temp[y] == '0') { b1[idex] = false; } else { b1[idex] = true; } y++; idex++; } break; } } //************************************************* b = ImageMatrix[i, f].blue; for (int o = 0; o < blue_v.Count; o++) { if (blue_v[o] == (int)b) { int y = 0; string temp = blue_code[o]; while (y < temp.Length) { if (temp[y] == '0') { b1[idex] = false; } else { b1[idex] = true; } y++; idex++; } break; } } } } //************************************************* int k = 0; int g = 0; BitArray ch = new BitArray(8); while (k < idex) { while (g < 8 && k < idex) { ch[g] = b1[k]; g++; k++; } g = 0; byte[] b = new byte[1]; ch.CopyTo(b, 0); char v = (char)b[0]; writetext1.Write(v); ch = new BitArray(8); } }
public void update(MouseEventArgs e) { var g = pictureBox1.CreateGraphics(); if (clr > W8interval * 2) { if (ImageMatrix != null) { var mouseNode = Helper.Flatten(e.X, e.Y, ImageOperations.GetWidth(ImageMatrix)); if (curr_source != -1 && Prev_mouse_pos != mouseNode) { Prev_mouse_pos = mouseNode; if (Helper.IN_Boundary(mouseNode, SB, ImageOperations.GetWidth(ImageMatrix))) { int Segment_mouse = Helper.crosspond(mouseNode, SB, ImageOperations.GetWidth(ImageMatrix), ImageOperations.GetWidth(Square_segment)); List<Point> segmentpath = new List<Point>(); segmentpath = ShortestPath_Operations.Backtracking(parent_list, Segment_mouse, ImageOperations.GetWidth(Square_segment)); List<Point> Curpath = Helper.crosspond(segmentpath, SB); curr_path = Curpath.ToArray(); if (AutoAnchor_WORK) { double freq = (double)frequancy / 1000; Autoancor.Update(Curpath, freq); List<Point> cooledpath = Autoancor.anchor_path(); if (cooledpath.Count > 0) { Point anchor = cooledpath[cooledpath.Count - 1]; AnchorPts.Add(anchor); curr_source = Helper.Flatten(anchor.X, anchor.Y, ImageOperations.GetWidth(ImageMatrix)); //curr_path = cooledpath.ToArray(); Helper.AppendToList<Point>(Mainselction, cooledpath); SB = new Boundary(); SB = ShortestPath_Operations.Square_Boundary(curr_source, ImageOperations.GetWidth(ImageMatrix) - 1, ImageOperations.GetHeight(ImageMatrix) - 1); //make a square segment Square_segment = Helper.COPY_Segment(ImageMatrix, SB); // currsrc in segment int newsrc = Helper.crosspond(curr_source, SB, ImageOperations.GetWidth(ImageMatrix), ImageOperations.GetWidth(Square_segment)); parent_list = ShortestPath_Operations.Dijkstra(newsrc, Square_segment); Autoancor.reset(); } } } else { curr_path = ShortestPath_Operations.GenerateShortestPath(curr_source, mouseNode, ImageMatrix).ToArray(); } } } clr = 0.0f; } if (clr > W8interval) { pictureBox1.Refresh(); g.Dispose(); } clr += .019f; }
public static void sum(int N, RGBPixel[,] image, string crack, int tapPosition) { long sz = 0; int[] array_red = new int[256]; int[] array_green = new int[256]; int[] array_blue = new int[256]; int Height = ImageOperations.GetHeight(image); int Width = ImageOperations.GetWidth(image); for (int l = 0; l < Height; l++) { for (int j = 0; j < Width; j++) { if (array_red[image[l, j].red] == 0) { sz++; } if (array_green[image[l, j].green] == 0) { sz++; } if (array_blue[image[l, j].blue] == 0) { sz++; } array_red[image[l, j].red]++; array_green[image[l, j].green]++; array_blue[image[l, j].blue]++; } } lock (all) { TMP = sz; TMP = 768 - TMP; if (TMP > CNTA) { all_sz = 0; all[all_sz].S = crack; all[all_sz].T = tapPosition; all_sz++; //FinalCrack = string.Copy(crack).ToCharArray(); CNTA = TMP; //for (int i = 0; i < N; i++) // FinalCrack[i] = Crack[i]; //FinalTapPosition = tapPosition; } else if (TMP == CNTA) { all[all_sz].S = crack; all[all_sz].T = tapPosition; all_sz++; //FinalCrack = string.Copy(crack).ToCharArray(); CNTA = TMP; //for (int i = 0; i < N; i++) // FinalCrack[i] = Crack[i]; //FinalTapPosition = tapPosition; } } }
/// <summary> /// get the three histograms for red, blue and green and put it in dictionaries /// store the histograms in queues /// </summary> /// <param name="ImageMatrix"></param> /// <returns>list of three histograms</returns> public static List <SimplePriorityQueue <Node, int> > getHistogram(RGBPixel[,] ImageMatrix) { List <SimplePriorityQueue <Node, int> > histrogramlist = new List <SimplePriorityQueue <Node, int> >(); //Dictionaries Dictionary <byte, int> RedHistogram = new Dictionary <byte, int>(); Dictionary <byte, int> blueHistogram = new Dictionary <byte, int>(); Dictionary <byte, int> GreenHistogram = new Dictionary <byte, int>(); //Queues SimplePriorityQueue <Node, int> _RedHistogram = new SimplePriorityQueue <Node, int>(); SimplePriorityQueue <Node, int> _BlueHistogram = new SimplePriorityQueue <Node, int>(); SimplePriorityQueue <Node, int> _GreenHistogram = new SimplePriorityQueue <Node, int>(); //getting histograms from the matrix and store it in dictionaries int X = ImageMatrix.GetLength(0); int Y = ImageMatrix.GetLength(1); for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { // Red byte redCol = ImageMatrix[i, j].red; if (RedHistogram.ContainsKey(redCol)) { RedHistogram[redCol] += 1; } else { RedHistogram.Add(redCol, 1); } // Blue byte blueCol = ImageMatrix[i, j].blue; if (blueHistogram.ContainsKey(blueCol)) { blueHistogram[blueCol] += 1; } else { blueHistogram.Add(blueCol, 1); } // Green byte GreenCol = ImageMatrix[i, j].green; if (GreenHistogram.ContainsKey(GreenCol)) { GreenHistogram[GreenCol] += 1; } else { GreenHistogram.Add(GreenCol, 1); } } } // puting the histograms in queues foreach (var node in RedHistogram) { Node colNode = new Node(); colNode.value = node.Key; colNode.Priority = node.Value; _RedHistogram.Enqueue(colNode, colNode.Priority); } foreach (var node in blueHistogram) { Node colNode = new Node(); colNode.value = node.Key; colNode.Priority = node.Value; _BlueHistogram.Enqueue(colNode, colNode.Priority); } foreach (var node in GreenHistogram) { Node colNode = new Node(); colNode.value = node.Key; colNode.Priority = node.Value; _GreenHistogram.Enqueue(colNode, colNode.Priority); } histrogramlist.Add(_RedHistogram); histrogramlist.Add(_BlueHistogram); histrogramlist.Add(_GreenHistogram); return(histrogramlist); }
public static List <Point> DetermineShortestPath(int Source, int Destination, RGBPixel [,] ImageMatrix) { List <int> Path = Dijkstra(Source, Destination, ImageMatrix); return(BackPath(Path, Destination, ImageOperations.GetHeight(ImageMatrix))); }
// it does everything :D public static void Get_file() //Loads the compressed file to get the image from binarystreams { Node Root = Red_Root; Node source = Root; hight_comp = Huffman_Tree.Comp_w.ReadInt32(); width_comp = Huffman_Tree.Comp_w.ReadInt32(); OUTPUT = new RGBPixel[hight_comp, width_comp]; int length_of_red = Huffman_Tree.Comp_w.ReadInt32() / 8; int length_of_green = Huffman_Tree.Comp_w.ReadInt32() / 8; int length_of_blue = Huffman_Tree.Comp_w.ReadInt32() / 8; int idx = 0, x, row = 0, col = 0; while (length_of_red != 0) //iterate over the binary stream of red value and reads a byte each time { byte val = Huffman_Tree.Comp_w.ReadByte(); //read byte from the stream, it doesn't have to be a specific value BitArray bits = new BitArray(8); bits = Convert_Byte_To_Bit(val); // converts the read byte into array of bits int f = 0; //iterate over the 8 bits and check if the current bit is 0, moves left in tree else move right //if we reached a leaf node, we retrive its value and start traversing from the source -root- again //else we continue traversing from the previous node with new 8 bits while (bits.Count > f) { if (source.Left == null || source.Right == null) { x = source.Value; OUTPUT[row, col].red = (byte)x; col++; if (col == width_comp) { row++; if (row == hight_comp) { row = 0; } col = 0; } source = Root; } if (bits[f] == false) { source = source.Left; idx++; } else if (bits[f] == true) { source = source.Right; idx++; } f++; } length_of_red--; } //---------------------------Greeeen-----------------------------// //Same thing happens here Root = Green_Root; source = Root; idx = 0; row = 0; col = 0; while (length_of_green != 0) { byte val = Huffman_Tree.Comp_w.ReadByte(); BitArray bits = new BitArray(8); bits = Convert_Byte_To_Bit(val); int f = 0; while (bits.Count > f) { if (source.Left == null || source.Right == null) { x = source.Value; OUTPUT[row, col].green = (byte)x; col++; if (col == width_comp) { row++; if (row == hight_comp) { row = 0; } col = 0; } source = Root; } if (bits[f] == false) { source = source.Left; idx++; } else if (bits[f] == true) { source = source.Right; idx++; } f++; } length_of_green--; } //---------------------------Blueeeee-----------------------------// //Same thing happens here Root = Blue_Root; source = Root; idx = 0; row = 0; col = 0; while (length_of_blue != 0) { byte val = Huffman_Tree.Comp_w.ReadByte(); BitArray bits = new BitArray(8); bits = Convert_Byte_To_Bit(val); int f = 0; while (bits.Count > f) { if (source.Left == null || source.Right == null) { x = source.Value; OUTPUT[row, col].blue = (byte)x; col++; if (col == width_comp) { row++; if (row == hight_comp) { row = 0; } col = 0; } source = Root; } if (bits[f] == false) { source = source.Left; idx++; } else if (bits[f] == true) { source = source.Right; idx++; } f++; } length_of_blue--; } Program.seed1 = Huffman_Tree.Comp_w.ReadInt64(); Program.tap2 = Huffman_Tree.Comp_w.ReadInt64(); int H = OUTPUT.GetLength(0); int W = OUTPUT.GetLength(1); Bitmap ImageBMP = new Bitmap(W, H, PixelFormat.Format24bppRgb); unsafe { BitmapData bmd = ImageBMP.LockBits(new Rectangle(0, 0, W, H), ImageLockMode.ReadWrite, ImageBMP.PixelFormat); int nWidth = 0; nWidth = W * 3; int nOffset = bmd.Stride - nWidth; byte *p = (byte *)bmd.Scan0; for (int i = 0; i < H; i++) { for (int j = 0; j < W; j++) { p[2] = OUTPUT[i, j].red; p[1] = OUTPUT[i, j].green; p[0] = OUTPUT[i, j].blue; p += 3; } p += nOffset; } ImageBMP.UnlockBits(bmd); } ImageBMP.Save(@"C:\Users\JOE\Desktop\[TEMPLATE] ImageEncryptCompress\Decompress Video/" + cnt.ToString() + ".bmp"); cnt++; }
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); }