在python opencv中捕获双眼

时间:2017-04-27 10:31:09

标签: python opencv detection haar-classifier

我想从图像中检测出双眼,但是当我使用Haar级联时,它会分别返回每只眼睛。我该怎么做才能从图像中将双眼合在一起? 这是我的代码:

image = cv2.imread('KA.AN1.39.tiff')
eye_cascade = cv2.CascadeClassifier('haarcascade_eye.xml')
eye = eye_cascade.detectMultiScale(image)
for (x,y,w,h) in eye:
    eye = image[y : y+h , x : x+w]
    cv2.imshow('eye', eye)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

for x,y,w,h in eye:
    roi_gray = gray[y:y+h, x:x+w]
    roi_color = img[y:y+h, x:x+w]
    eyes = eyePair_cascade.detectMultiScale(roi_gray)   
    if len(eyes) == 0: return   
    for (ex,ey,ew,eh) in eyes:
        eyes_roi = roi_color[ey: ey+eh, ex:ex + ew]
        #cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

cv2.imwrite(outfile, eyes_roi)

另见Detection of eye pairs in Python OpenCV (Panoptigram)