I am currently using HoughCircles
on computer-vision
but I can’t manage to make it work.
How can this error be solved ?
JavaScript
x
10
10
1
Error :
2
'Traceback (most recent call last):
3
File "F:TIPE ENTROPIEProgrammationIris-Recognition-masterIris Codenorm.py", line 82, in <module>
4
extract_iris(img)
5
File "F:TIPE ENTROPIEProgrammationIris-Recognition-masterIris Codenorm.py", line 53, in extract_iris
6
cord = get_circle(img, 35, 0, 50, 40)
7
File "F:TIPE ENTROPIEProgrammationIris-Recognition-masterIris Codenorm.py", line 14, in get_circle
8
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT,1,20, param1=p1,param2=p2,minRadius=minR,maxRadius=maxR)
9
cv2.error: C:projectsopencv-pythonopencvmodulesimgproc srchough.cpp:1494: error: (-215) !_image.empty() && _image.type() == (((0) & ((1 << 3) - 1)) + (((1)-1) << 3)) && (_image.isMat() || _image.isUMat()) in function cv::HoughCircles`
10
Here is my code :
JavaScript
1
78
78
1
import cv2
2
import numpy as np
3
4
## Definitions
5
6
def get_circle(img, minR, maxR, p1, p2):
7
8
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT,1,20, param1=p1,param2=p2,minRadius=minR,maxRadius=maxR)
9
circles = np.uint16(np.around(circles))
10
11
#return the first circle
12
return circles[0][0]
13
14
def print_img(img,x,y,w,h):
15
if w!=0 and h!=0:
16
cv2.imshow('detected circles',img[x:x+w,y:y+h])
17
cv2.imwrite('app-1/1.jpg',img[x:x+w,y:y+h])
18
else:
19
cv2.imshow('detected circles',img)
20
cv2.imwrite('app-1/1.jpg',img)
21
cv2.waitKey(0)
22
23
cv2.destroyAllWindows()
24
25
def reject_out(img, xc, yc, r):
26
row = len(img)
27
col = len(img[0])
28
29
for x in range(0,row):
30
for y in range(0, col):
31
res = (x-xc)*(x-xc) + (y-yc)*(y-yc)
32
if res > r*r :
33
img[x][y] = 0
34
35
def reject_in(img, xc, yc, r):
36
xs = xc-r
37
ys = yc-r
38
39
for x in range(xs,xs+2*r):
40
for y in range(ys, ys+2*r):
41
res = (x-xc)*(x-xc) + (y-yc)*(y-yc)
42
if res < r*r :
43
img[x][y] = 0
44
45
def extract_iris(img):
46
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
47
cord = get_circle(cimg, 35, 0, 50, 40)
48
# draw the outer circle
49
cv2.circle(cimg,(cord[0],cord[1]),cord[2],(0,255,0),2)
50
# draw the center of the circle
51
cv2.circle(cimg,(cord[0],cord[1]),2,(0,0,255),3)
52
53
54
h = 2*cord[2]
55
w = 2*cord[2]
56
x = cord[1]-cord[2]
57
y = cord[0]-cord[2]
58
nimg = img[x:x+w,y:y+h]
59
reject_out(nimg, h/2, w/2, h/2)
60
print_img(cimg,0,0,0,0)
61
62
cord = get_circle(nimg, 0, cord[2]-1, 50, 30)
63
print (nimg[0:w][cord[0]])
64
# draw the outer circle
65
cv2.circle(nimg,(cord[0],cord[1]),cord[2],(0,255,0),2)
66
# draw the center of the circle
67
cv2.circle(nimg,(cord[0],cord[1]),2,(0,0,255),3)
68
reject_in(nimg, cord[1], cord[0], cord[2])
69
print_img(nimg,0,0,0,0)
70
print (nimg[0:w][cord[0]])
71
72
73
74
img = cv2.imread('test6.jpg',0)
75
img = cv2.medianBlur(img,5)
76
extract_iris(img)
77
`
78
UPDATE : New error :
JavaScript
1
9
1
Traceback (most recent call last):
2
File "F:TIPE ENTROPIEProgrammationessai.py", line 80, in <module>
3
extract_iris(img)
4
File "F:TIPE ENTROPIEProgrammationessai.py", line 49, in extract_iris
5
cord = get_circle(cimg, 35, 0, 50, 40)
6
File "F:TIPE ENTROPIEProgrammationessai.py", line 8, in get_circle
7
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
8
cv2.error: C:projectsopencv-pythonopencvmodulesimgprocsrccolor.cpp:11111: error: (-215) scn == 3 || scn == 4 in function cv::cvtColor
9
Advertisement
Answer
As @jeru-luke has told in comment, you should add img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
just before you call cv2.HoughCircles()
:
JavaScript
1
6
1
def get_circle(img, minR, maxR, p1, p2):
2
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # <------------- Here
3
4
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT,1,20, param1=p1,param2=p2,minRadius=minR,maxRadius=maxR)
5
circles = np.uint16(np.around(circles))
6
COLOR_BGR2GRAY
and not COLOR_GRAY2BGR
UPDATE:
Add nimg = cv2.cvtColor(nimg, cv2.COLOR_GRAY2BGR)
just before calling get_circle
for second time:
UPDATE 2
JavaScript
1
81
81
1
import cv2
2
import numpy as np
3
4
## Definitions
5
6
def get_circle(img, minR, maxR, p1, p2):
7
if len(img.shape)==3:
8
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
9
10
circles = cv2.HoughCircles(img, cv2.HOUGH_GRADIENT,1,20, param1=p1,param2=p2,minRadius=minR,maxRadius=maxR)
11
circles = np.uint16(np.around(circles))
12
13
#return the first circle
14
return circles[0][0]
15
16
def print_img(img,x,y,w,h):
17
if w!=0 and h!=0:
18
cv2.imshow('detected circles',img[x:x+w,y:y+h])
19
cv2.imwrite('app-1/1.jpg',img[x:x+w,y:y+h])
20
else:
21
cv2.imshow('detected circles',img)
22
cv2.imwrite('app-1/1.jpg',img)
23
cv2.waitKey(0)
24
25
cv2.destroyAllWindows()
26
27
def reject_out(img, xc, yc, r):
28
row = len(img)
29
col = len(img[0])
30
31
for x in range(0,row):
32
for y in range(0, col):
33
res = (x-xc)*(x-xc) + (y-yc)*(y-yc)
34
if res > r*r :
35
img[x][y] = 0
36
37
def reject_in(img, xc, yc, r):
38
xs = xc-r
39
ys = yc-r
40
41
for x in range(xs,xs+2*r):
42
for y in range(ys, ys+2*r):
43
res = (x-xc)*(x-xc) + (y-yc)*(y-yc)
44
if res < r*r :
45
img[x][y] = 0
46
47
def extract_iris(img):
48
cimg = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
49
cord = get_circle(cimg, 35, 0, 50, 40)
50
# draw the outer circle
51
cv2.circle(cimg,(cord[0],cord[1]),cord[2],(0,255,0),2)
52
# draw the center of the circle
53
cv2.circle(cimg,(cord[0],cord[1]),2,(0,0,255),3)
54
55
56
h = 2*cord[2]
57
w = 2*cord[2]
58
x = cord[1]-cord[2]
59
y = cord[0]-cord[2]
60
nimg = img[x:x+w,y:y+h]
61
62
reject_out(nimg, h/2, w/2, h/2)
63
print_img(cimg,0,0,0,0)
64
65
nimg = cv2.cvtColor(nimg, cv2.COLOR_GRAY2BGR)
66
cord = get_circle(nimg, 0, cord[2]-1, 50, 30)
67
print (nimg[0:w][cord[0]])
68
# draw the outer circle
69
cv2.circle(nimg,(cord[0],cord[1]),cord[2],(0,255,0),2)
70
# draw the center of the circle
71
cv2.circle(nimg,(cord[0],cord[1]),2,(0,0,255),3)
72
reject_in(nimg, cord[1], cord[0], cord[2])
73
print_img(nimg,0,0,0,0)
74
print (nimg[0:w][cord[0]])
75
76
77
78
img = cv2.imread('test6.jpg',0)
79
img = cv2.medianBlur(img,5)
80
extract_iris(img)
81
Result: