private void transformation_btn_Click(object sender, EventArgs e) { Transformation T = ICPTransformation.ComputeTransformation(Shape1, Shape2); List <Point> Shape2T = ApplyTransformation(T, Shape2); Pen pBlue = new Pen(Brushes.Blue, 1); Pen pRed = new Pen(Brushes.Red, 1); Graphics g = transformed_panel.CreateGraphics(); DisplayShape(Shape1, pBlue, g); DisplayShape(Shape2T, pRed, g); MessageBox.Show("Cost = " + ICPTransformation.ComputeCost(Shape1, Shape2, T).ToString()); }
Ransac_Algo ApplyRansac() { Ransac_Algo rans = new Ransac_Algo(); int k = 20; //iterations for the algorithm int t = 7; //treshold to determine if we accept the new point or not, if it adds less than 7 to the cost it is valid int n = 3; //number of initial random point int d = 4; //minimum data points to accept the model as valid int iterations = 0; //counter of iterations done /* * Transformation best_model = new Transformation(); * List<Point> best_consensus_set1 = new List<Point>(); * List<Point> best_consensus_set2 = new List<Point>(); * Ransac_Algo rans = new Ransac_Algo(); */ double best_error = int.MaxValue; Random rand = new Random(); //used to select random indexes while (iterations < k) { List <int> random_index = new List <int>(); //list that will contain random indexes from shapes List <Point> maybe_inliners1 = new List <Point>(); List <Point> maybe_inliners2 = new List <Point>(); Transformation maybe_model = new Transformation(); double maybe_cost = 0; //cost from the random points //selecting random points (using indexes) from data for (int i = 0; i < n; i++) { int index = rand.Next(Shape1.Count); while (random_index.Contains(index)) { index = rand.Next(Shape1.Count); } random_index.Add(index); maybe_inliners1.Add(Shape1[index]); maybe_inliners2.Add(Shape2[index]); } maybe_model = ICPTransformation.ComputeTransformation(maybe_inliners1, maybe_inliners2); //computing model for random chosen points maybe_cost = ICPTransformation.ComputeCost(maybe_inliners1, maybe_inliners2, maybe_model); //computing cost for random chosen points List <Point> consensus_set1 = new List <Point>(maybe_inliners1); List <Point> consensus_set2 = new List <Point>(maybe_inliners2); for (int i = 0; i < Shape1.Count; i++) { if (!random_index.Contains(i)) { maybe_inliners1.Add(Shape1[i]); maybe_inliners2.Add(Shape2[i]); consensus_set1.Add(Shape1[i]); consensus_set2.Add(Shape2[i]); Transformation temp_model = ICPTransformation.ComputeTransformation(consensus_set1, consensus_set2); double temp_cost = ICPTransformation.ComputeCost(consensus_set1, consensus_set2, temp_model); double dif = Math.Abs(maybe_cost - temp_cost); if (dif >= t) { consensus_set1.RemoveAt(consensus_set1.Count - 1); consensus_set2.RemoveAt(consensus_set2.Count - 1); } } } if (consensus_set1.Count > d) //checking if consensus has enough points to make the model valid { Transformation better_model = ICPTransformation.ComputeTransformation(consensus_set1, consensus_set2); double this_error = ICPTransformation.ComputeCost(consensus_set1, consensus_set2, better_model); if (this_error < best_error) { best_error = this_error; rans.best_model = better_model; rans.best_consensus1 = consensus_set1; rans.best_consensus2 = consensus_set2; rans.best_error = this_error; } } iterations++; } return(rans); }