1

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

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

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

原始图像 结果

之后,我正在编写代码以仅通过使用红外摄像机来检测相同的事物。

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

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

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()
4

1 回答 1

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以获得更好的效果。完了,走吧 :)

于 2016-08-26T18:58:49.130 回答