圆检测不重叠 [英] Circle detection without overlapping
问题描述
我想在以下条件下进行圆检测:重叠的圆将被计为1个圆.
I want to do circle detection under the condition that: overlap circles will be count as 1 circle.
特别是,当我进行圆检测并将下面的图像的每个圆(实际上是花粉或类似圆的物体)上加上字母"P"时
Particularly, when I do circle detection and put the letter "P" to every circle (actually they are pollen, or circle-like objects) for the image below
它变成了
(同一张照片,但我不知道为什么在这里上传时它变成水平的)
(The same photo but I don't know why it turned to horizontal when I uploaded it here)
但是我只希望每个圆圈1个字母P.调整半径也许是个好主意,但是我还有很多其他照片要去,所以我希望有一种方法可以忽略重叠.
But I just want 1 letter P for each circle. Adjusting the radius maybe a good idea, but I still have lot of other photos to go, so I hope there is a method to ignore overlapping.
这是我的代码:
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,请查看函数中的第4个参数.改变这一点,我可以摆脱重叠.此外,我在HoughCircles函数中对canny threshold的参数进行了一些调整,直到获得所需的结果.我建议您在得出结论之前先了解一下参数.
I would suggest using contours instead. However, if you do want to use HoughCircles, look at the 4th parameter in the function. Changing this, I could get rid of the overlappings. Additionally, I tweaked a bit the parameters for canny threshold in the HoughCircles function until I got the desired results. I'd suggest understanding the parameters well before coming up with a conclusion.
代码:
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)
结果:
这篇关于圆检测不重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!