使用HoughCircles在眼睛或瞳孔中寻找圆圈

时间:2015-03-01 22:00:26

标签: python opencv numpy

我无法在图像上绘制圆圈。你能看看吗?问题在于绘制圆圈,我无法检索图像上找到的圆的半径和中心。 提前谢谢。

代码:(Python)的

import cv,cv2
import numpy as np

def Eyes_Detect(frame):
    cas = cv2.CascadeClassifier("C:\opencv2.4.10\data\haarcascades\haarcascade_eye.xml")
    gray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    x=1.1
    res=cas.detectMultiScale(gray,scaleFactor=x,minNeighbors=2,minSize=(20,50),flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
    while len(res)>2:
        x+=0.1
        res=cas.detectMultiScale(gray,scaleFactor=x,minNeighbors=2,minSize=(20,50),flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
    if len(res)<2:
        return (False,[])
    else:
        return (True,res)

def Find_Canny_Edges(frame):
    res = cv2.Canny(frame,100,200)
    return res

def Find_Circles(frame):
    cv.Smooth(frame,frame,cv.CV_GAUSSIAN, 7,7)
    storage=cv.CreateMat(frame.width, 1, cv.CV_32FC3)
    cv.HoughCircles(frame,storage,cv.CV_HOUGH_GRADIENT,2,100.0,30,150,100,140)
    return storage

def Draw_Circles(image,storage):
    for circle in storage:
        radius = circle[2]
        center = (circle[0], circle[1])
        cv.Circle(image, center, radius, (0, 0, 255), 3, 8, 0)
    return image
# Main    
cap = cv2.VideoCapture(0)
cap.set(3,1280)
cap.set(4,720)
ct=1
while True:
    ret,frame = cap.read()
    gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
    result = Eyes_Detect(frame)
    if result[0]==True:
        result = Find_Canny_Edges(gray)
        cv2.imwrite("image.jpg",result)
        result = cv.LoadImage('image.jpg',0)
        result = Find_Circles(result)
        result = Draw_Circles(frame,result)
        cv2.imshow("Output",result)
        if cv2.waitKey(5) & 0xFF == ord('q'):
            break
cap.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:3)

你试过tutorial了吗?我提取下面的示例代码:

import cv2
import numpy as np

img = cv2.imread('opencv_logo.png',0)
img = cv2.medianBlur(img,5)
cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)

circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=0,maxRadius=0)

if circles is None:
   print "No circles found"
else:
   circles = np.uint16(np.around(circles))
   for i in circles[0,:]:
      # draw the outer circle
      cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),2)
      # draw the center of the circle
      cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)

   cv2.imshow('detected circles',cimg)
   cv2.waitKey(0)
   cv2.destroyAllWindows()