title: OpenCV egg count python
body: <p>I did an egg counting using openCV and python and I did get help from here <a href="https://stackoverflow.com/questions/26932891/detect-touching-overlapping-circles-ellipses-with-opencv-and-python">egg detection</a></p>
  
  <pre><code>while True:
  
   (grabbed, frame) = cap.read()  
   hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
   th, bw = cv2.threshold(hsv[:, :, 2], 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
   kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
   morph = cv2.morphologyEx(bw, cv2.MORPH_CLOSE, kernel)
   dist = cv2.distanceTransform(morph, cv2.DIST_L2, cv2.DIST_MASK_PRECISE)
   .....
  </code></pre>
  
  <p>And I get the coordinates and draw an ellipse on the egg.</p>
  
  <pre><code>x, y, w, h = cv2.boundingRect(contours[i])
  _, mx, _, mxloc = cv2.minMaxLoc(dist[y:y+h, x:x+w], peaks8u[y:y+h, x:x+w])
  cv2.circle(im, (int(mxloc[0]+x), int(mxloc[1]+y)), int(mx), (255, 0, 0), 2)
  cv2.rectangle(im, (x, y), (x+w, y+h), (0, 255, 255), 2)
  cv2.drawContours(im, contours, i, (0, 0, 255), 2)
  </code></pre>
  
  <p>I created a line and count the eggs one by one.</p>
  
  <pre><code>cv2.line(frame40, (0,coordYEntranceLine), (width,coordYEntranceLine), (255, 0, 0), 2)
  
  def CheckEntranceLineCrossing(coordYContour, coordYEntranceLine):
     absDistance = abs(coordYContour - coordYEntranceLine)
  
     if ((coordYContour &gt;= coordYEntranceLine) and (absDistance &lt;= 3)):
       return 1
     else:
       return 0
  
  if CheckEntranceLineCrossing(coordYContour, coordYEntranceLine, area):
     eggCount += 1
  </code></pre>
  
  <p>The problem begins here. According to the logic if the egg past the line and distance &lt;3 is counting, but the conveyor belt is stopped and the distance &lt;3 is counting the egg again, and will perceive as too many eggs.The event I want is not to detect the detected egg again.</p>
  
  <p>The whole code is roughly in this way:</p>
  
  <pre><code>def CheckEntranceLineCrossing(coordYContour, coordYEntranceLine):
     absDistance = abs(coordYContour - coordYEntranceLine)
  
     if ((coordYContour &gt;= coordYEntranceLine) and (absDistance &lt;= 3)):
        return 1
     else:
        return 0
  
  def getDistance(coordYEgg1,coordYEgg2):
     dist = abs(coordYEgg1 - coordYEgg2)
  
     return dist
  
  cap = cv2.VideoCapture('20180910_144521.mp4')
  
  while True:
  
  (grabbed, frame) = cap.read()
  
  hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
  th, bw = cv2.threshold(hsv[:, :, 2], 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
  kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
  ......
  
  flag = False
  egg_list = [[]]
  egg_index = 0
  
  for i in range(len(contours)):
      (x, y, w, h) = cv2.boundingRect(contours[i])
      #_, mx, _, mxloc = cv2.minMaxLoc(dist[y:y+h, x:x+w], peaks8u[y:y+h, x:x+w])
  
      egg_list.append([x, y, flag])
  
      for i in range(len(egg_list)):
          egg_index = i
  
          egg_new_X = x
          egg_new_Y = y
  
          if len(egg_list[egg_index]) &gt;= 1:
              dist = getDistance(egg_new_Y, egg_list[egg_index][1])
  
              if dist &gt; 50:
                  egg_list.append([egg_new_X, egg_new_Y, flag])
  
  
  
  
      if CheckEntranceLineCrossing(egg_list[i][1], coordYEntranceLine) and not egg_list[i][2]:
          eggCount += 1
          egg_list[i][2] = True
  </code></pre>
  
  <p>Is there a method you can suggest in this regard? Do I have the chance to put the detected contour into an array and control it or something else?</p>