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

朋友实验室要做菌落计数,我用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()

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南七小僧

打赏后,可以添加微信一对一咨询

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值