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?