-
Notifications
You must be signed in to change notification settings - Fork 0
/
myHoughCircle1.py
50 lines (47 loc) · 1.59 KB
/
myHoughCircle1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import cv2
import numpy as np
def myHoughCircle1(image,iclose,rangeR,sensitivity,edgeThreshold):
try:
circles1= cv2.HoughCircles(image,cv2.HOUGH_GRADIENT,1,int(2*min(rangeR)),param1=int(edgeThreshold),param2=7,minRadius=int(min(rangeR)),maxRadius=int(max(rangeR)))
circles = np.single(np.around(circles1))
cx = np.empty(0)
cy = np.empty(0)
r = np.empty(0)
img = cv2.imread('test5.jpg')
for i in circles1[0,:,:]:
cx = np.append(cx,i[0])
cy = np.append(cy,i[1])
r = np.append(r,i[2])
r_max = max(r)
r_min = np.mean(r) + np.std(r, ddof = 1)*0.5
newRangeR = range(int(r_min),int(r_max))
centers_x = np.empty(0)
centers_y = np.empty(0)
radii = np.empty(0)
for i in circles1[0,:,:]:
if i[2] >= r_min and i[2] <= r_max:
centers_x = np.append(centers_x,i[0])
centers_y = np.append(centers_y,i[1])
radii = np.append(radii,i[2])
circle_number = len(radii)
minDistance = int(r_min)
circles2= cv2.HoughCircles(image,cv2.HOUGH_GRADIENT,1,minDistance,param1=int(edgeThreshold),param2=7,minRadius=int(r_min),maxRadius=int(r_max))
cx = np.empty(0)
cy = np.empty(0)
r = np.empty(0)
for i in circles1[0,:,:]:
cx = np.append(cx,i[0])
cy = np.append(cy,i[1])
r = np.append(r,i[2])
cv2.circle(img,(i[0],i[1]),i[2],(0,255,0),2)
cv2.imwrite("test8.jpg", img)
centers_x = cx
centers_y = cy
radii = r
except:
centers_x =[]
centers_y =[]
radii = []
newRangeR =[]
#print (centers_x,centers_y,radii)
return centers_x,centers_y,radii