title: Remove outliers from a list of points in C#
body: <p><strong>Problem</strong></p>
  <p>I have a list of points in C# that I draw on a panel on a Windows Forms object. The lists variables are two <code>listA</code> and <code>listB</code>. The points in <code>listB</code> are the points in <code>listA</code> except that they have gone through some transformation to deform it to resemble the shape formed by points in <code>listA</code> and then added some outliers to make them look different. If you can try these on your visual studio then this is the code...</p>
  <pre><code>class Form1 : Form
  {
      //declare the list to hold points for 
      //shapes
      List&lt;Point&gt; listA = new List&lt;Point&gt;();
      List&lt;Point&gt; listB = new List&lt;Point&gt;();
  
      //this methods transforms,applies outliers and draws the shapes on panel1
      private void button1_click(EventArgs e, object sender)
      {
          //clear the lists for initializing
          listA.Clear();
          listB.Clear();
          Point p1a = new Point(20, 30);
          Point p2a = new Point(120, 50);
          Point p3a = new Point(160, 80);
          Point p4a = new Point(180, 300);
          Point p5a = new Point(100, 220);
          Point p6a = new Point(50, 280);
          Point p7a = new Point(20, 140);
          //Hold the Points in an array
          Point[] mypoints = new Point[] { p1a, p2a, p3a, p4a, p5a, p6a, p7a };
          //add the points to the List with one call
          listA.AddRange(mypoints);
          //define a new Transformation 
          //that will translate shapeA to have a slightly different imageB 
          Transformation t2 = new Transformation();
          t2.A = 1.05; t2.B = 0.05; t2.T1 = 15; t2.T2 = 22;
          //assign the new translated points to listB  
          listB = applytransformation(t2, listA);
          //Add outliers to listb by manipulating the values in the list
          Shape2[2] = new Point(Shape2[2].X + 10, Shape2[2].Y + 3);
          //create a new instance of pen
          //for drawing imageA in blue
          Pen penner = new Pen(Brushes.Blue, 3);
          //Create a new instance of pen for 
          //drawing imageB in red
          Pen mypen = new Pen(Brushes.Red, 3);
          //get the graphic context
          Graphics g = panel1.CreateGraphics();
          //draw both shapes
          DisplayShape(listA, penner, g);
          DisplayShape(listB, mypen, g);
      }
      //the method below does the transformation of imagea into imageb by manipulating the points and the transformation
      List&lt;Point&gt; applytransformation(Transformation x, List&lt;Point&gt; shape)
      {
          List&lt;Point&gt; Tlist = new List&lt;Point&gt;();
          foreach (Point c in shape) {
              double xprime = x.A * c.X + x.B * c.Y + x.T1;
              double yprime = x.B * c.X * -1 + x.A * c.Y + x.T2;
              Point ptrans = new Point((int)xprime, (int)yprime);
  
              Tlist.Add(ptrans);
          }
          //it returns the points that will be used to draw imageB
          return Tlist;
      }
      //this method draws the points on the panel
      void DisplayShape(List&lt;Point&gt; Shp, Pen pen, Graphics G)
      {
          Point? prevPoint = null;//nullable
          foreach (Point pt in Shp) {
              G.DrawEllipse(pen, new Rectangle(pt.X - 2, pt.Y - 2, 4, 4));
              if (prevPoint != null) {
                  G.DrawLine(pen, (Point)prevPoint, pt);
  
              }
              prevPoint = pt;
  
          }
          G.DrawLine(pen, Shp[0], Shp[Shp.Count - 1]);
  
      }
  }
  public class Transformation
  {
      public double A { get; set; }
      public double B { get; set; }
      public double T1 { get; set; }
      public double T2 { get; set; }
  }
  </code></pre>
  <p><strong>Goal</strong></p>
  <p>I want to remove all the outliers in imageB so that it resembles imageA even if it won't be perfect. All methods or algorithms are welcome ie <code>RANSAC,minimum cost function</code>. I have tried to find an authoritative source online that can guide or help me achieve this in C# with zero success. The code I have provided is a minimum reproducible example that can be replicated on any visual studio IDE.Please help, Thank You for your time and contribution.</p>
  <p><strong>Expected Output</strong></p>
  <p>I added an image to make it clear the result I want
  <img src="https://i.stack.imgur.com/Fx8vi.png" alt="enter image description here" /></p>