title: Find the red circle in video
body: <p>I want to track a moving cap of a coke bottle in the webcam Feed with OpenCV in Python (or C++). I tried to search for all the red in the frame and then I used some kind of <code>HOUGH TRANSFORM</code> to search for circles.
  I cant find the right radius for the circle and fix it so it doesn't change every frame.the process time is not so important I dont want a real time detection but I do want a precise red circle detection.</p>
  
  <p>This is what I have so far:</p>
  
  <pre><code>import cv2
  import numpy as np
  import cv2.cv as cv
  cap = cv2.VideoCapture(0)
  while(1):
  
      # Take each frame
      _, frame = cap.read()
  
      # Convert BGR to HSV
  
      hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  
      # define range of blue color in HSV 
      lower_red = np.array([160,140,50]) 
      upper_red = np.array([180,255,255])
  
      imgThreshHigh = cv2.inRange(hsv, lower_red, upper_red)
  
      imgray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
      thresh = 18
      edges = cv2.Canny(imgray,thresh,thresh*3)
  
      circles = cv2.HoughCircles(imgThreshHigh, cv.CV_HOUGH_GRADIENT, 1, 500, 25, 75, 5, 15)
      maxRadius= 0
      xc = 0.00
      yc = 0.00
      found = False 
      if circles is not None:
          found = True
          for i in circles[0,:3]:
              if i[2] &lt; 100:
                  if i[2] &gt; maxRadius:
                      maxRadius = i[2]
                      if maxRadius &gt; 1.0:
                          # draw the outer circle
                          cv2.circle(frame,(i[0],i[1]),maxRadius,(0,0,255),2)
                          # draw the center of the circle
                          cv2.circle(frame,(i[0],i[1]),1,(0,0,255),3)
                          xc = i[0]
                          yc = i[1] 
      if found: 
          print "ball detected at position:",xc, ",", yc, " with radius:", maxRadius
      else: 
          print "no ball" 
      cv2.imshow('frame',frame)
      cv2.imshow('edges',edges)
      k = cv2.waitKey(5) &amp; 0xFF
      if k == 27:
          break
  cv2.destroyAllWindows()
  </code></pre>
  
  <p>I dont think HOUGH TRANSFORM works for this. So I want to use the edges.
  How can I use an equation like   <code>(X-Xc)^2 + (Y-Yc)^2 =R^2</code> and the contours to find circles?</p>
  
  <p>Also if there is an improvement for Hough Transform I will appreciate it if you share with me</p>
  
  <p>Contours:</p>
  
  <pre><code>contours,hierarchy=cv2.findContours(edges,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
  </code></pre>
  
  <p>My example image: ---> this is not my example Image.this is the object I want to find in videos.</p>
  
  <p><a href="https://i.stack.imgur.com/iPyCW.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/iPyCW.jpg" alt="enter image description here"></a></p>