title: Convert ellipse in image to circle (warp ellipse to circle like a polygon warping to rectangle)
body: <p>I have an image of an ellipse and if an image has ellipse i am finding it using findcontours() and then i want to convert this ellipse to a circle.</p>
  
  <p>see the example</p>
  
  <p><a href="https://i.stack.imgur.com/9EyHT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9EyHT.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/hLpCV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hLpCV.png" alt="enter image description here"></a></p>
  
  <p>and i want transform each of them to like this</p>
  
  <p><a href="https://i.stack.imgur.com/MbaVm.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MbaVm.png" alt="enter image description here"></a></p>
  
  <p>First I have applied canny edge detection.Then on this image findcontour() is applied.</p>
  
  <p>I have found the ellipse using findcontours() to get all contours and get the required elliptical contour and then the i am using fitellipse() to get center, rotation angle and major and minor axis of ellipse. </p>
  
  <p>I have then tried to rotate the image by the rotated angle and then scale height and width of image w.r.t minor and major axis(i.e. making major axis and minor axis length same) but also I am not getting the proper circular object image as above. There will be some rotation left/it will still be like an ellipse which will be near to circle or so.</p>
  
  <pre><code>     _, contours, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  
      for c in contours:
          if len(c) &gt;= 5:
              a = cv2.fitEllipse(c)
              (x, y), (MA, ma), angle = a
              area = (math.pi * MA * ma)/4
              if abs(x-image.shape[0]/2) &lt;= 2 and abs(y-image.shape[1]/2) &lt;= 2 and (area - cv2.contourArea(c)) &lt; 50:
                  screenCount = c
                  width, height = MA, ma
                  centerX, centerY = x, y
                  ellipseAngle = angle
                  print(width, height, centerX, centerY, ellipseAngle)
                  # cv2.drawContours(img, c, -1, (0, 255, 0), 4)
                  cv2.ellipse(img, a, (0, 0, 255), 2, 8)
                  cv2.imshow("ellipse", img)
                  break
  
      img = image.copy()
      if ellipseAngle &lt; 90:
          rotatedImg = imutils.rotate(img, ellipseAngle)
      else:
          rotatedImg = imutils.rotate(img, -(ellipseAngle - 90))
  </code></pre>
  
  <p>Then i have scaled as per major and minor axis</p>
  
  <p>after applying findcontour() i got these 2 contours for 1st image in the post</p>
  
  <p><a href="https://i.stack.imgur.com/yMuzT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yMuzT.png" alt="enter image description here"></a>
  <a href="https://i.stack.imgur.com/zwiRu.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zwiRu.png" alt="enter image description here"></a></p>
  
  <p>from these any contour is fine right? i am using the first contour from countour as per the code and fitellipse() gives me this ellipse </p>
  
  <p><a href="https://i.stack.imgur.com/jVKPV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jVKPV.png" alt="enter image description here"></a></p>
  
  <p>EDITED - If there are any better approach to solve this problem it would be helpful.</p>