title: Opencv , transforming image by affine transformation
body: <p><img src="https://i.stack.imgur.com/qkwWV.jpg" alt="left image" /><img src="https://i.stack.imgur.com/sD2Gp.png" alt="right image" /></p>
  <p>I want to transform the first image to second image, I think maybe it is  affine transformation. My problems and questions are as follows:</p>
  <p>(1). As I have mentioned above, I think the transform is affine transformation. So the first step is to find three pairs of corresponding points by clicking three corner points in the first image along clockwise direction(return coordinates from mouse callback function) and set their corresponding points as specific coordinates(the distances between each corner point are known). And the second step is to use getAffineTransform() and warpAffine() methods to implement affine transformation. But this approach have turned out to be not good enough(see the third image), so is there any idea to improve the result?
  <img src="https://i.stack.imgur.com/8bJXT.jpg" alt="third image" /></p>
  <p>My code is shown as follows:</p>
  <pre><code>#include &lt;opencv2/core/core.hpp&gt;
  #include &lt;opencv2/highgui/highgui.hpp&gt;
  #include &lt;opencv2/imgproc/imgproc.hpp&gt;
  #include &quot;opencv2/features2d/features2d.hpp&quot;
  
  #include &lt;stdio.h&gt;
  #include &lt;vector&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  using namespace cv;
  
  
  Mat org;
  int n=0;
  vector&lt;Point&gt; capturePoint;
  
  void on_mouse(int event,int x,int y,int flags,void *ustc)
  {
      Point pt;
      char coordinateName[16];
  
      if (event == CV_EVENT_LBUTTONDOWN)
      {
          pt = Point(x,y);
          cout&lt;&lt;x&lt;&lt;&quot; &quot;&lt;&lt;y&lt;&lt;endl;
          capturePoint.push_back(pt);
          n++;
  
          circle(org,pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);
          sprintf(coordinateName,&quot;(%d,%d)&quot;,x,y);
          putText(org,coordinateName,pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255),1,8);
  
          //imshow(&quot;org&quot;,org);
  
          if(n&gt;=4)
          {
              imshow(&quot;org&quot;,org);
              cvDestroyAllWindows();
          }
      }
  }
  
  int main()
  {
      org = imread(&quot;1-3.jpg&quot;,1);
  
      namedWindow(&quot;org&quot;,1);
      setMouseCallback(&quot;org&quot;,on_mouse,0);// mouse callback;
  
      imshow(&quot;org&quot;,org);
      waitKey(0);
      //cout&lt;&lt;capturePoint.size()&lt;&lt;endl;
  
      //three pairs of corresponding points;
  
      Point2f srcPoint[3];
      Point2f dstPoint[3];
      srcPoint[0]=capturePoint[0];//mouse click along clockwise direction;
      srcPoint[1]=capturePoint[1];
      srcPoint[2]=capturePoint[2];
      //srcPoint[3]=capturePoint[3];
  
      dstPoint[0]=Point(0,0);//distances between each corner point are known;
      dstPoint[1]=Point(640,0);//width=320,height=220;
      dstPoint[2]=Point(640,440);
      //dstPoint[3]=Point(0,220);
  
      Mat warpDst=Mat(org.rows, org.cols, org.type());
      //Mat warpMat = findHomography( srcPoint, dstPoint, 0 );
      Mat warpMat = getAffineTransform(srcPoint,dstPoint);
      warpAffine(org,warpDst,warpMat,org.size());//affine transformation;
      imshow(&quot;Warp&quot;,warpDst);
  
      waitKey(0);
  
      return 0;
  }
  </code></pre>
  <p>(2). I would like to implement the transformation without manual intervention, i.e. selecting points manually is not required, so I want to use the centers of ellipses in the first image and their corresponding points to implement affine transformation. My question is that if the centers of ellipses can be used to implement affine transformation, and if can be, how to detect the centers of ellipses.</p>
  <p>(3). I have used canny method to get the largest contour, and applied cornerHarris to detect corner points from the largest contour. I found that each corner may have some candidate points that lie close to each other. So how to get the four &quot;ture&quot; corner points?</p>