2

I did an egg counting using openCV and python and I did get help from here egg detection

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)
 .....

And I get the coordinates and draw an ellipse on the egg.

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)

I created a line and count the eggs one by one.

cv2.line(frame40, (0,coordYEntranceLine), (width,coordYEntranceLine), (255, 0, 0), 2)

def CheckEntranceLineCrossing(coordYContour, coordYEntranceLine):
   absDistance = abs(coordYContour - coordYEntranceLine)

   if ((coordYContour >= coordYEntranceLine) and (absDistance <= 3)):
     return 1
   else:
     return 0

if CheckEntranceLineCrossing(coordYContour, coordYEntranceLine, area):
   eggCount += 1

The problem begins here. According to the logic if the egg past the line and distance <3 is counting, but the conveyor belt is stopped and the distance <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.

The whole code is roughly in this way:

def CheckEntranceLineCrossing(coordYContour, coordYEntranceLine):
   absDistance = abs(coordYContour - coordYEntranceLine)

   if ((coordYContour >= coordYEntranceLine) and (absDistance <= 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]) >= 1:
            dist = getDistance(egg_new_Y, egg_list[egg_index][1])

            if dist > 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

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?

1
  • You need to keep track of the eggs in every frame. Commented Sep 17, 2018 at 9:38

1 Answer 1

1

You need to keep track of the eggs in every frame.
1) Lets say you have 5 eggs in frame 1. Store the position and a flag of the eggs in an array, egg_list.

flag = False
egg_list = [[]]
for contour in contours:
   ellipse = cv2.fitEllipse(contour)
   (x, y, w, h) = cv2.boundingRect(contour)
   egg_list.append([x, y , flag])

2) Then find all the eggs in the second frame and compare it with the egg_list. if the distance is below some predefined value, consider them as the same egg. Else add the new egg to the egg_list.

 for contour in contours:
 ellipse = cv2.fitEllipse(contour)
       (x, y, w, h) = cv2.boundingRect(contour)

       ....       

       for i in egg_list:
           dist = getdist(egg_new,egg_list[i])
           if dist > dist_thresh :
              egg_list.append([egg_new[0],egg_new[1],flag])

3) When an egg crosses the line, mark that egg with a flag in the egg_list and increment the count. Then when the same egg is detected to cross the line, you can ignore it.

egg_index = 0
for contour in contours:
   if CheckEntranceLineCrossing(egg_list[egg_index,2], coordYEntranceLine) & ~egg_list[egg_index,2]:
      eggCount += 1
      egg_list[egg_list,2] = true;

By keeping track of all your eggs in a list, you will be able to count only the eggs that matters.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply but I didn't understand getdist () ?
Create a function to calculate the distance between the 2 eggs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.