gpt4 book ai didi

python - 不重叠的圆检测

转载 作者:太空宇宙 更新时间:2023-11-03 23:09:42 27 4
gpt4 key购买 nike

我想在以下条件下进行圆检测:重叠圆将被计为 1 个圆。

特别是,当我进行圆检测并将字母“P”放在下图中的每个圆(实际上它们是花粉或类似圆的物体)时 enter image description here

变成了

enter image description here

(同样的照片,不知道为什么上传的时候变成横的了)

但我只想为每个圆圈添加 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 中的第 4 个参数。 .改变这个,我可以摆脱重叠。此外,我在 HoughCircles 函数中稍微调整了 canny threshold 的参数,直到获得所需的结果。我建议在得出结论之前充分了解这些参数。

代码:

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)

结果:

Result

关于python - 不重叠的圆检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52535609/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com