How does the Opencv library detect the number of eggs in a picture?

OpencvLibrary detects the number of eggs in the picture

Since I needed to write a program to detect the number of eggs, I used several classic methods in opencv to detect the eggs in the picture. While implementing it step by step, the purpose of each method is also explained. I hope it can give some help to friends who are learning opencv. The picture below shows the original picture and the detected border after implementation.

Insert image description here

Insert image description here

import cv2
import numpy as np
I=cv2.imread(r"C:\Users\ZFG\PycharmProjects\tensorflow_pytorch\egg_test001.jpg") #这里输入绝对路径,读取图像
I=cv2.resize(I,(500,500)) #因为图片太大,所以改变了图片的大小
img_Guassian=cv2.GaussianBlur(I,(5,5),0)  #高斯滤波一下,去除图片中的噪声
gray=cv2.cvtColor(img_Guassian,cv2.COLOR_BGR2GRAY)  #灰度化图像
thre,bw=cv2.threshold(gray,165,255,cv2.THRESH_BINARY)  #对图片进行二值化操作,使图片只有黑色和白色
#二值化后,进行形态学操作(开运算,腐蚀+膨胀),去除细小干扰
kernel=np.ones((7,7),np.uint8)  #设置形态学操作卷积核大小
opening=cv2.morphologyEx(bw,cv2.MORPH_OPEN,kernel)
cv2.imshow('opening',opening)  #查看形态学操作后的结果并展示
#下面操作得到轮廓contours
contours,hir=cv2.findContours(opening,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE) #opencv2.版本需要接收三个返回值
# 检测所有轮廓,所有轮廓建立一个等级树结构。外层轮廓包含内层轮廓,内层轮廓还可以继续包含内嵌轮廓。
sum=0  #sum记录鸡蛋个数
result = cv2.drawContours(I, contours, -1, (0, 255, 0), 3)  #利用得到的轮廓信息,将轮廓绘制出
str='the number of eggs: '+str(len(contours))  #想在图片上显示鸡蛋个数信息,这是需要显示的字符串
img=cv2.putText(result,str,(0,20),cv2.FONT_HERSHEY_TRIPLEX,0.6,(0,0,255),1)  #将文字显示到图片上
# print('鸡蛋个数为:',sum)
cv2.imshow('I',result)  #展示图片
cv2.waitKey(0)

The implementation of the code is relatively simple, so the disadvantage is that it cannot detect eggs in some complex situations, such as when the background color and the color of the egg are not much different, and the color of the interference and the egg in the background color are the same. When the picture changes, the binarization threshold (165 in the picture) can be appropriately adjusted according to the egg pixel value. When there are many impurities in the picture, the convolution kernel size can be adjusted.

Of course, if you think the outline of the egg is not good-looking, you can use the following code to add a border after getting the contours:

cnt=contours[0] #有n个轮廓,则有n个轮廓信息,这里只画一个轮廓
x,y,w,h=cv2.boundingRect(cnt) 
img_1=cv2.rectangle(I,(x,y),(x+w,y+h),(0,0,255),2) 

The renderings are as follows:

Insert image description here

Finally, all the code is shown and the results are shown.

import numpy as np
I=cv2.imread(r"C:\Users\ZFG\PycharmProjects\tensorflow_pytorch\egg_test001.jpg")
I=cv2.resize(I,(500,500))
img_Guassian=cv2.GaussianBlur(I,(5,5),0)
gray=cv2.cvtColor(img_Guassian,cv2.COLOR_BGR2GRAY)
thre,bw=cv2.threshold(gray,165,255,cv2.THRESH_BINARY)
kernel=np.ones((7,7),np.uint8)
opening=cv2.morphologyEx(bw,cv2.MORPH_OPEN,kernel)
cv2.imshow('opening',opening)
contours,hir=cv2.findContours(opening,cv2.RETR_TREE,cv2.CHAIN_APPROX_NONE)
sum=0
result = cv2.drawContours(I, contours, -1, (0, 255, 0), 3)
for i in range(len(contours)):
    cnt=contours[i]
    x,y,w,h=cv2.boundingRect(cnt)
    img_1=cv2.rectangle(I,(x,y),(x+w,y+h),(0,0,255),2)
str='the number of eggs: '+str(len(contours))
img=cv2.putText(result,str,(0,20),cv2.FONT_HERSHEY_TRIPLEX,0.6,(0,0,255),1)
cv2.imshow('I',result)
cv2.waitKey(0)

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45277161/article/details/134737145