簡體   English   中英

OpenCV 雞蛋計數 python

[英]OpenCV egg count python

我使用 openCV 和 python 進行了雞蛋計數,我確實從這里得到了雞蛋檢測的幫助

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

問題從這里開始。 根據邏輯,如果雞蛋越過線並且距離<3正在計數,但是傳送帶停止並且距離<3再次計數雞蛋,並且會認為雞蛋太多。 我想要的事件是不檢測再次檢測到雞蛋。

整個代碼大致是這樣的:

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;

通過在列表中跟蹤所有雞蛋,您將能夠只計算重要的雞蛋。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM