我想这样做的条件下,圆检测:重叠的圈子将会被算作1圈。
特别是,当我做圆检测,并把字母“P”,以每圈(实际上他们是花粉,或圆圈状物体)下面图像
它成为
(同一张照片,但我不知道为什么它转到水平,当我上载这里)
但我只想每个圆圈1个字母P。调整半径也许是个好主意,但我仍然有很多其他的照片去了,所以我希望有忽略重叠的方法。
这里是我的代码:
import cv2
import numpy as np
path = "./sample.JPG"
font = cv2.FONT_HERSHEY_COMPLEX
def image_resize(image, width = None, height = None, inter = cv2.INTER_AREA):
# initialize the dimensions of the image to be resized and
# grab the image size
dim = None
(h, w) = image.shape[:2]
# if both the width and height are None, then return the
# original image
if width is None and height is None:
return image
# check to see if the width is None
if width is None:
# calculate the ratio of the height and construct the
# dimensions
r = height / float(h)
dim = (int(w * r), height)
# otherwise, the height is None
else:
# calculate the ratio of the width and construct the
# dimensions
r = width / float(w)
dim = (width, int(h * r))
# resize the image
resized = cv2.resize(image, dim, interpolation = inter)
# return the resized image
return resized
# In[22]:
iml = cv2.imread(path,cv2.IMREAD_COLOR)
img = image_resize(iml,width=960)
cimg = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
cimg = cv2.medianBlur(cimg,5)
#Circle detection to detect pollen in big images, return the center's coordinates and radius of circles in array
circles = cv2.HoughCircles(cimg,cv2.HOUGH_GRADIENT,1,10,param1=15,param2=20,minRadius=10,maxRadius=25)
circles = np.uint16(np.around(circles))[0,:]
for i in circles:
cv2.putText(img,'P',(i[0],i[1]), font, 0.5,(0,0,255),1,cv2.LINE_AA)
cv2.imwrite("./output.jpg",img)
我建议使用轮廓代替。但是,如果你想使用HoughCircles,看在function的第四个参数。改变这个,我可以摆脱交迭。此外,我调整比特的参数在HoughCircles功能精明阈直到我得到所期望的结果。我建议来作出结论之前,了解这些参数井。
码:
import cv2
import numpy as np
arr = cv2.imread("U:/SO/032OR.jpg")
print(arr.shape)
imggray = cv2.cvtColor(arr, cv2.COLOR_BGR2GRAY)
# Not median blur
imggray = cv2.GaussianBlur(imggray, (9,9),3)
circles_norm = cv2.HoughCircles(imggray, cv2.HOUGH_GRADIENT, 1, imggray.shape[0]/16,
param1=20, param2=8, minRadius=15, maxRadius=30)
circles_norm = np.uint16(np.around(circles_norm))[0,:]
for i in circles_norm:
center = (i[0], i[1])
cv2.putText(arr, 'P', (i[0], i[1]), cv2.FONT_HERSHEY_COMPLEX, 0.5,
(0,0,255),1,cv2.LINE_AA)
结果:
如果我这样做,我不会用HoughCircles而是尝试:
1)光滑,摆脱一些噪音 2)阈值,以产生二进制掩模 3)轮廓,其中每个轮廓是检测到的花粉。
简单,但我认为它应该工作。