Detecção de círculo sem sobreposição
Quero fazer a detecção de círculos sob a condição de que: os círculos sobrepostos serão contados como 1 círcul
Particularmente, quando faço a detecção de círculos e coloco a letra "P" em todos os círculos (na verdade, eles são pólen ou objetos semelhantes a círculos) para a imagem abaixo
Tornou-s
(A mesma foto, mas não sei por que ela virou horizontal quando a carreguei aqui)
Mas eu só quero uma letra P para cada círculo. Talvez seja uma boa ideia ajustar o raio, mas ainda tenho muitas outras fotos, então espero que exista um método para ignorar a sobreposição.
Aqui está o meu código:
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)