用OPENCV做菌落 数量检测【hough,边缘分割,区域提取】

版权声明:我是南七小僧,微信: to_my_love ,2020年硕士毕业,寻找 自然语言处理,图像处理,软件开发等相关工作,欢迎交流思想碰撞。 https://blog.csdn.net/qq_25439417/article/details/85321584

朋友实验室要做菌落计数,我用opencv做了一下尝试

代码写的比较混乱,只是想试一试结果怎么样,以节约时间为主的。

问题:

光线区域会带来非常大的干扰,所以合理去除光照非常重要。

 

去光照会带来一定的灰度级影响,导致菌落融合或者弥散丢失,故考虑切割圆盘留下原图的关键区域【切割效果如下】:

随后用Hough检测圆形即可:

# -*- coding: utf-8 -*-
"""
Created on Fri Dec 21 15:54:29 2018

@author: Lenovo
"""
import numpy as np
import cv2 as cv
import copy
a = cv.imread('duo1.jpg')
w = int(a.shape[0]/5)
h = int(a.shape[1]/5)
a = cv.resize(a,(h,w))
b = a.copy()
f = a.copy()
a = cv.medianBlur(a,9)
a = cv.cvtColor(a, cv.COLOR_BGR2GRAY)
a = cv.Canny(a,0,255)
cv.imshow('ya',a)
cv.waitKey(0)
cv.destroyAllWindows()
kernel = np.ones((6,6),np.uint8)  
#二值化
a[a>0.3*255] = 255
a[a<=0.3*255] = 0
#a = cv.erode(a,kernel,iterations = 20)
#a = cv.dilate(a,kernel,iterations = 5)


cv.imshow('a',a)
cv.waitKey(0)
cv.destroyAllWindows()
#region limit

#dd = np.where(a==255)
#y_c = int(sum(dd[0])/len(dd[0]))
#x_c = int(sum(dd[1])/len(dd[1]))
#a[y_c,x_c]=0
##a= a[y_c-250:y_c+400,:]
#
#b = a.copy()

temp = np.ones(a.shape,np.uint8)*0

h = cv.findContours(a,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)  
contours = h[1]
#for i in contours:
#    print(len(i))
#np.where()
dddd = cv.drawContours(temp,contours,-1,(255,255,255),3)
hdddd = cv.findContours(dddd,cv.RETR_TREE,cv.CHAIN_APPROX_SIMPLE)  
contours = hdddd[1][0]
dddd = cv.drawContours(temp,contours,-1,(255,255,255),3)

cv.imshow('dddd111111',dddd)
cv.waitKey(0)
cv.destroyAllWindows()
#dddd = cv.dilate(dddd,kernel,iterations = 5)

dd = np.where(dddd==255)
y_c = int(sum(dd[0])/len(dd[0]))
x_c = int(sum(dd[1])/len(dd[1]))

dddd[y_c,x_c]=255
xxxxxxxxxxxxx = np.where(dddd[:,y_c]==255)[0]
yyyyyyyyyyyyy = np.where(dddd[x_c,:]==255)[0]
radius = ((xxxxxxxxxxxxx[-5] - xxxxxxxxxxxxx[5])+(yyyyyyyyyyyyy[-5] - yyyyyyyyyyyyy[5]))//4


#b = b[y_c-radius:y_c+radius,x_c-radius:x_c+radius]

wb = b.shape[0]
hb = b.shape[1]
print(contours)

#tt = np.ones(a.shape,np.uint8);
#(x, y), radius = cv.minEnclosingCircle(contours)
#(x, y, radius) = np.int0((x, y, radius))  # 圆心和半径取整
#print(x, y, radius)
#cv.circle(tt, (x, y), radius, (0, 0, 255), 2)


cv.imshow('bccccccccc',tt)
cv.waitKey(0)
cv.destroyAllWindows()
for i in range(wb):
    for j in range(hb):
        if np.sqrt(np.square(i-y_c)+np.square(j-x_c))>radius:
            b[i,j]=0
cv.imshow('br',b)   
cv.waitKey(0)
cv.destroyAllWindows()



#cv.imshow('br',b)
#cv.waitKey(0)
#cv.destroyAllWindows()

#c = a.copy()
#a = a*b
#for i in range(w):
#    for j in range(h):
#        c[i,j] = a[i,j]*b[i,j]
#
#dd = np.where(a>0)
##c_x = sum(dd[0])/867
##c_y = sum(dd[1])/650
#
##a = a[c_x-200:c_x+200,c_y-200:c_y+200]
#
#cv.imshow('a1',a)
#cv.waitKey(0)
#a = a[:,:,2]
#a = cv.medianBlur(a,5)
#a[a>0.8*255] = 255
#b = cv.cvtColor(b,cv.COLOR_BGR2GRAY)
b = b[:,:,1]
circles = cv.HoughCircles(b,cv.HOUGH_GRADIENT,1,1,
                            param1=100,param2=10,minRadius=0,maxRadius=10)

for i in circles[0,:]:
    # draw the outer circle
    cv.circle(f,(i[0],i[1]),i[2],(255,0,0),2)
    # draw the center of the circle
    cv.circle(f,(i[0],i[1]),2,(255,0,0),3)
    
cv.imshow('detected circles',f)
cv.waitKey(0)
cv.destroyAllWindows()

猜你喜欢

转载自blog.csdn.net/qq_25439417/article/details/85321584