title: CV2 Draw ellipse only on masked area
body: <p>I'm trying to make a circle under the player on soccer field, similar to this:</p>
  <p><a href="https://i.stack.imgur.com/RIjPW.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/RIjPW.png" alt="enter image description here" /></a></p>
  <p>If I just do a circle around the player's feet, it looks bad:</p>
  <p><a href="https://i.stack.imgur.com/SOftT.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SOftT.png" alt="enter image description here" /></a></p>
  <p>I'm trying to draw the circle only on the green part of the field (to make it more 3D).</p>
  <p>First I masked only the green part on the field, using the following code:</p>
  <pre><code>def mask(img):
      ## convert to hsv
      hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
      ## mask of green (36,25,25) ~ (86, 255,255)
      lower = (36, 25, 25)
      upper = (86, 255, 255)
      mask = cv2.inRange(hsv, lower, upper)
      ## slice the green
      imask = mask == 0
      green = np.zeros_like(img, np.uint8)
      green[imask] = img[imask]
      img = green
      return img
  </code></pre>
  <p>This gives the following result:</p>
  <p><a href="https://i.stack.imgur.com/MTOQk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/MTOQk.png" alt="enter image description here" /></a></p>
  <p>How can I draw the ellipse only on the green (after the masking - black) part?</p>
  <p><strong>P.S. This is the full code:</strong></p>
  <pre><code>import cv2
  import numpy as np
  
  
  def mask(img):
      ## convert to hsv
      hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
      ## mask of green (36,25,25) ~ (86, 255,255)
      lower = (36, 25, 25)
      upper = (86, 255, 255)
      mask = cv2.inRange(hsv, lower, upper)
      ## slice the green
      imask = mask == 0
      green = np.zeros_like(img, np.uint8)
      # green.fill(255)
      green[imask] = img[imask]
      img = green
      return img
  
  
  def player_ellipse(img, player_point):
      axesLength = (45, 20)
      angle = 0
      startAngle = 0
      endAngle = 360
      # Red color in BGR
      color = (0, 0, 255)
      # Line thickness of 5 px
      thickness = 5
      img = cv2.ellipse(img, player_point, axesLength, angle, startAngle, endAngle, color, thickness)
      return img
  
  
  def main():
      img = cv2.imread('img.png')
      point = (160, 665)
  
      img = player_ellipse(img, point)
      img = mask(img)
  
      cv2.imshow(&quot;img&quot;, img)
      cv2.waitKey(0)
      cv2.destroyAllWindows()
  
  
  main()
  </code></pre>
  <p>And this is player without any editing:
  <a href="https://i.stack.imgur.com/rsd1j.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/rsd1j.png" alt="cccccccc" /></a></p>