我使用openCV和python进行了鸡蛋计数,我确实从这里得到了帮助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)
.....
然后我得到坐标并在蛋上画一个椭圆。
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)
我创建了一条线并逐个计数鸡蛋。
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
问题从这里开始。根据逻辑,如果经过线和距离
整个代码大致是这样的:
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
在这方面您有什么建议的方法吗?我是否有机会将检测到的轮廓放入数组并对其进行控制?
最佳答案
您需要跟踪每一帧中的鸡蛋。
1)假设您在第1帧中有5个鸡蛋。将鸡蛋的位置和标记存储在数组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)然后在第二帧中找到所有鸡蛋,并将其与egg_list进行比较。如果距离低于某个预定值,则将它们视为同一个鸡蛋。否则,将新鸡蛋添加到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)当一个鸡蛋越过线时,在egg_list中用一个标志标记该鸡蛋并增加计数。然后,当检测到同一卵越过线时,可以将其忽略。
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;
通过跟踪列表中的所有鸡蛋,您将只能计算重要的鸡蛋。
关于python - OpenCV鸡蛋计数python,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52363732/