title: How to detect image gradient or normal using OpenCV
body: <p>I wanted to detect ellipse in an image. Since I was learning Mathematica at that time, I asked a question <a href="https://mathematica.stackexchange.com/questions/25589/how-can-i-detect-an-ellipse-in-a-photo">here</a> and got a satisfactory result from the answer below, which used the RANSAC algorithm to detect ellipse.</p>
  
  <p>However, recently I need to port it to OpenCV, but there are some functions that only exist in Mathematica. One of the key function is the "GradientOrientationFilter" function.</p>
  
  <p>Since there are five parameters for a general ellipse, I need to sample five points to determine one. Howevere, the more sampling points indicates the lower chance to have a good guess, which leads to the lower success rate in ellipse detection. Therefore, the answer from Mathematica add another condition, that is the gradient of the image must be parallel to the gradient of the ellipse equation. Anyway, we'll only need three points to determine one ellipse using least square from the Mathematica approach. The result is quite good.</p>
  
  <p>However, when I try to find the image gradient using Sobel or Scharr operator in OpenCV, it is not good enough, which always leads to the bad result.</p>
  
  <p>How to calculate the gradient or the tangent of an image accurately? Thanks!</p>
  
  <hr>
  
  <p>Result with gradient, three points
  <img src="https://i.stack.imgur.com/I6AQf.png" alt="Result with gradient, three points"></p>
  
  <p>Result without gradient, five points
  <img src="https://i.stack.imgur.com/Kzind.png" alt="Result without gradient, five points"></p>
  
  <p>----------updated----------</p>
  
  <p>I did some edge detect and median blur beforehand and draw the result on the edge image. My original test image is like this:</p>
  
  <p><img src="https://i.stack.imgur.com/q6n7y.png" alt="original test image"></p>
  
  <p>In general, my final goal is to detect the ellipse in a scene or on an object. Something like this:</p>
  
  <p><img src="https://i.stack.imgur.com/jrn2P.jpg" alt="coffee cup"></p>
  
  <p>That's why I choose to use RANSAC to fit the ellipse from edge points.</p>