使用霍夫圆变换的眼睛瞳孔跟踪

时间:2016-08-26 13:43:54

标签: python-2.7 opencv eye-tracking

我有一个眼控轮椅项目,我需要检测眼睛的瞳孔,并根据其运动轮椅移动。作为我正在编写的代码的测试,我在静态图像上执行了脚本。图像正好放在相机的位置。相机将是红外线。

注意:我在Windows Platfrom上使用已编译的OpenCV 3.1.0-dev和Python2.7

我想要使用Houghcircle变换检测到的圆圈:

original image result

之后我正在研究一种代码,只能使用红外摄像机来检测同一件事。

静态图像代码的结果对我来说非常可靠,但问题是红外摄像机的代码。

到目前为止我写的代码是:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:

    ## Read Image
    ret, image = cap.read()
    ## Convert to 1 channel only grayscale image
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    ## CLAHE Equalization
    cl1 = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
    clahe = cl1.apply(gray)
    ## medianBlur the image to remove noise
    blur = cv2.medianBlur(clahe, 7)
    ## Detect Circles
    circles = cv2.HoughCircles(blur ,cv2.HOUGH_GRADIENT,1,20,
                                param1=50,param2=30,minRadius=7,maxRadius=21)

    if circles != None:
        circles = np.round(circles[0,:]).astype("int")

    for circle in circles[0,:]:
        # draw the outer circle
        cv2.circle(image,(circle[0],circle[1]),circle[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(image,(circle[0],circle[1]),2,(0,0,255),3)

    if cv2.waitKey(1) in [27, ord('q'), 32]:
        break

cap.release()
cv2.destroyAllWindows()
  

我总是收到这个错误:

**if circles != None:
FutureWarning: comparison to `None` will result in an elementwise object comparison in the future.

Traceback (most recent call last):
    cv2.circle(image,(circle[0],circle[1]),circle[2],(0,255,0),2)
IndexError: invalid index to scalar variable.**

有关静态图片代码的任何问题,代码为:

import cv2
import numpy as np

## Read Image
image = cv2.imread('eye.tif')
imageBackup = image.copy()
## Convert to 1 channel only grayscale image
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
## CLAHE Equalization
cl1 = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe = cl1.apply(gray)
## medianBlur the image to remove noise
blur = cv2.medianBlur(clahe, 7)

## Detect Circles
circles = cv2.HoughCircles(blur ,cv2.HOUGH_GRADIENT,1,20,
                            param1=50,param2=30,minRadius=7,maxRadius=21)

for circle in circles[0,:]:
    # draw the outer circle
    cv2.circle(image,(circle[0],circle[1]),circle[2],(0,255,0),2)
    # draw the center of the circle
    cv2.circle(image,(circle[0],circle[1]),2,(0,0,255),3)

cv2.imshow('Final', image)
cv2.imshow('imageBackup', imageBackup)

cv2.waitKey(0)
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:0)

所以我尝试了自己,我也有同样的错误。所以我像我已经提出的那样修改了代码。这是剪辑:

if circles != None:
    for circle in circles[0,:]:
        # draw the outer circle
        cv2.circle(image,(circle[0],circle[1]),circle[2],(0,255,0),2)
        # draw the center of the circle
        cv2.circle(image,(circle[0],circle[1]),2,(0,0,255),3)

此外,您可以尝试使用cv2.Canny以获得更好的效果。过来和出:)