EGG COUNTER - HELP WITH IDEAS

asked 2018-10-22 19:28:01 -0600

Danilo Sotto gravatar image

I'm doing a project for school, and I started to study opencv. The goal of the project is to perform egg counting, where eggs can be of various colors.

I implemented a code, but I have a problem where the eggs come very close together. I used the distance algorithm, but I did not make much progress.

An example of how it is: image description

CODE:

 while(video.isOpened()):
    ret,imagem = video.read()

    cv2.line(imagem,(linhax,0),(linhax,height),(0,255,0),2)#tamanho 3 troquei pra 2 para teste
    cv2.putText(imagem,'Contador: %r' %contador,(10,30),cv2.FONT_HERSHEY_SIMPLEX,1,(0,255,0),2)


    #primeiro passo e aplicar o tom de cinza no frame(foto) capturada
    imgCinza = cv2.cvtColor(imagem,cv2.COLOR_BGR2GRAY) #

    #segundo passo e tirar os borres da imagem cinza, deixando ela suave
    imgSuave = cv2.GaussianBlur(imgCinza,(5,5),0)

    #terceiro passo e aplicar a binarizacao
    ret, imgBinarizada = cv2.threshold(imgCinza,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)


    # noise removal
    kernel = np.ones((3,3),np.uint8)#(3,3)
    opening = cv2.morphologyEx(imgBinarizada,cv2.MORPH_OPEN,kernel, iterations = 2)#2
    # sure background area
    sure_bg = cv2.dilate(opening,kernel,iterations=3)#3
    # Finding sure foreground area
    dist_transform = cv2.distanceTransform(opening,cv2.DIST_L2,5)#5


    ret, sure_fg = cv2.threshold(dist_transform,0.7*dist_transform.max(),255,0) #

    # Finding unknown region
    sure_fg = np.uint8(sure_fg)


    contorno = cv2.findContours(sure_fg.copy(),cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    contorno = contorno[1]#retorna varios contornos, e posiciona no primeiro que encontrou

    for c in contorno:
        M  = cv2.moments(c)
        if  M["m00"] != 0:
            cx = int(M["m10"] / M["m00"])
            cy = int(M["m01"] / M["m00"])       

            if verificaEntradaLinha(cx, linhax):
                contador += 1


        else:
            cx,cy = 0, 0

        cv2.circle(imagem,(cx,cy),2,(255,0,0),-1)



    cv2.imshow("Binarizada",sure_fg)
    cv2.imshow("Imagem",imagem)



    key = cv2.waitKey(30)
    if key == ord("q"):
        print "Fechando a camera."
        break

He needed ideas on how to recognize the eggs with their colored colors and how to pull the eggs together to count correctly.

Thank you in advance for your attention.

edit retag flag offensive close merge delete

Comments

1
  • To recognize the two colors of eggs, do two thresholding operations. One for the white (on the luminosity channel) and another for the brown eggs (inRange on the HSV image)
  • Detect the local maxima on the distance transform image to get the position of the eggs (the points that have no stronger neighbors)
  • As the eggs touch, doing a contour detection won't help (the touching eggs will be counted as one).
kbarni gravatar imagekbarni ( 2018-10-23 04:16:04 -0600 )edit