private void button3_Click(object sender, EventArgs e) { List<Phi> DNAseq = new List<Phi>(); // the exact contour map List<Phi> verticies = new List<Phi>(); // the polygon map List<Phi> DNA = new List<Phi>(); // the final contour map with arc length and angle of each vertex /*img2 = imgs[imgs.Count-1].CopyBlank(); Image<Gray, Byte> gray1 = imgs[imgs.Count-1].Convert<Gray, Byte>();*/ img2 = img1.CopyBlank(); Image<Gray, Byte> gray1 = img1.Convert<Gray, Byte>(); imgs_gray.Add(gray1.Clone()); gray1 = gray1.SmoothGaussian(3); gray1 = gray1.ThresholdBinaryInv(new Gray(245), new Gray(255)); gray1 = gray1.MorphologyEx(null, CV_MORPH_OP.CV_MOP_CLOSE, 2); using (MemStorage storage1 = new MemStorage()) { Image<Gray, Byte> temp = gray1.Clone(); Contour<Point> contour = temp.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, RETR_TYPE.CV_RETR_EXTERNAL); if (contour == null) { // if no contour are found, display the image directly so that user will know the input cannot be recognized pictureBox2.Image = gray1.ToBitmap(); return; } double area = Math.Abs(contour.Area); Contour<Point> maxArea = contour; contour = contour.HNext; for (; contour != null; contour = contour.HNext) { double nextArea = Math.Abs(contour.Area); if (area < nextArea) { area = nextArea; maxArea = contour; } } Point[] exactPoints = maxArea.ToArray(); int i = 0; foreach (Point point in exactPoints) { Phi tempPhi = new Phi(); tempPhi.x = point.X; tempPhi.y = point.Y; tempPhi.theta = 0; tempPhi.l = i; DNAseq.Add(tempPhi); i++; } if (radioButton1.Checked) { img2.Draw(maxArea, new Bgr(255, 0, 0), 2); goto display; } Contour<Point> result = maxArea.ApproxPoly(1.0, storage1); Point[] points = result.ToArray(); i = 0; foreach (Point point in points) { Phi tempPhi = new Phi(); tempPhi.x = point.X; tempPhi.y = point.Y; tempPhi.theta = 0; tempPhi.l = 0; verticies.Add(tempPhi); i++; } // interpolate the arc length for (int j = 0, t = 0; j < verticies.Count; ++j) { while (!(verticies[j].x == DNAseq[t].x && verticies[j].y == DNAseq[t].y)) { t++; } Phi vert = verticies[j]; vert.l = t; verticies[j] = vert; t = 0; } // End of Functional codes // Start of Experimental double angle = 0; for (i = 0; i < verticies.Count; ++i) { int next = i + 1; if (next == verticies.Count) { next = 0; }//Bounds check //Turning angle computation //If this is starting vertex theta = 0 if (i == 0) { angle = 0.0; } else { //Compute turning angle double turn_angle = Util.calcAngle(verticies[next].x, verticies[next].y, verticies[i].x, verticies[i].y, verticies[i - 1].x, verticies[i - 1].y); double vector1_x = verticies[i].x - verticies[i - 1].x; double vector1_y = verticies[i].y - verticies[i - 1].y; double vector2_x = verticies[next].x - verticies[i].x; double vector2_y = verticies[next].y - verticies[i].y; int direction = Util.sign((vector1_y * vector2_x) - (vector1_x * vector2_y)); // Cumulate the turning angles angle += (180 - turn_angle) * direction; } //Store this turning function value Phi vert = verticies[i]; vert.theta = Math.Round(angle); verticies[i] = vert; //For all points between vertex[i] and vertex[next] theta will be the same // Theta is the total angle turned from start point if (verticies[next].l > verticies[i].l) { for (int j = verticies[i].l; j <= verticies[next].l; ++j) { Phi v = DNAseq[j]; v.theta = Math.Round(angle); DNAseq[j] = v; } } else { for (int j = verticies[i].l; j <= verticies[next].l + exactPoints.Length; ++j) { int index = j; if (index >= exactPoints.Length) { index -= exactPoints.Length; } Phi vv = DNAseq[index]; vv.theta = Math.Round(angle); DNAseq[index] = vv; } } } for (i = 0; i < DNAseq.Count; i++) { int index = i + verticies[0].l + 1; if (index >= DNAseq.Count) { index -= DNAseq.Count; } DNA.Add(DNAseq[index]); Phi tem = DNA[i]; tem.l = i; DNA[i] = tem; } DNAs.Add(DNA); verts.Add(verticies.Count); order = DNAs.Count; if (order > 1) { button5.Enabled = true; } label1.Text = "Contour:" + order + " of " + DNAs.Count; label2.Text = "Edges:" + DNA.Count; label3.Text = "Verts:" + verticies.Count; img2.Draw(result, new Bgr(255, 0, 0), 2); results.Add(result); } display: //img2=img2.Resize(pictureBox2.Width, pictureBox2.Height, INTER.CV_INTER_LINEAR); pictureBox2.Image = img2.ToBitmap(); }
private void button4_Click(object sender, EventArgs e) { List<List<Phi>> DNAseqs = new List<List<Phi>>(); // the exact contour map List<List<Phi>> verticiess = new List<List<Phi>>(); // the polygon map //List<List<Phi>> DNAs = new List<List<Phi>>(); // the final contour map with arc length and angle of each vertex img2 = img1.CopyBlank(); Image<Gray, Byte> gray1 = img1.Convert<Gray, Byte>(); gray1 = gray1.SmoothGaussian(3); gray1 = gray1.ThresholdBinaryInv(new Gray(245), new Gray(255)); gray1 = gray1.MorphologyEx(null, CV_MORPH_OP.CV_MOP_CLOSE, 2); using (MemStorage storage1 = new MemStorage()) { Image<Gray, Byte> temp = gray1.Clone(); Contour<Point> contour = temp.FindContours(CHAIN_APPROX_METHOD.CV_CHAIN_APPROX_NONE, RETR_TYPE.CV_RETR_EXTERNAL); if (contour == null) { // if no contour are found, display the image directly so that user will know the input cannot be recognized pictureBox2.Image = gray1.ToBitmap(); return; } double area = Math.Abs(contour.Area); Contour<Point> maxArea = contour; // Create a list of valid contours List<Contour<Point>> contours = new List<Contour<Point>>(); List<Point[]> exactPointss = new List<Point[]>(); if (area >= Util.MIN_AREA) { contours.Add(contour); } contour = contour.HNext; for (; contour != null; contour = contour.HNext) { double nextArea = Math.Abs(contour.Area); if (nextArea >= Util.MIN_AREA) { contours.Add(contour); } } foreach (Contour<Point> contr in contours) { exactPointss.Add(contr.ToArray()); } int i = 0; foreach (Point[] points in exactPointss) { List<Phi> DNAseq = new List<Phi>(); foreach (Point point in points) { Phi tempPhi = new Phi(); tempPhi.x = point.X; tempPhi.y = point.Y; tempPhi.theta = 0; tempPhi.l = i; DNAseq.Add(tempPhi); i++; } DNAseqs.Add(DNAseq); } foreach (Contour<Point> contr in contours) { img2.Draw(contr, new Bgr(255, 0, 0), 2); } pictureBox2.Image = img2.ToBitmap(); } }