【发布时间】:2017-01-03 05:12:11
【问题描述】:
我有一个眼控轮椅项目,我需要检测眼睛的瞳孔并根据其运动轮椅移动。作为对我正在编写的代码的测试,我在静态图像上执行了脚本。图像正是放置相机的位置。相机将是红外相机。
注意:我在 Windows Platfrom 上使用已编译的 OpenCV 3.1.0-dev 和 Python2.7
我想要使用 Houghcircle 变换检测到的圆:
之后,我正在编写代码以仅通过使用 IR 摄像头来检测相同的事物。
静态图像代码的结果对我来说非常可靠,但问题是红外摄像头的代码。
目前我写的代码是:
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()
【问题讨论】:
-
红外热像仪到底有什么问题?它找到任何圆圈还是什么也没有?能否提供样张图片?
-
@PSchn 相机没有问题,问题是如果没有检测到圆圈,我会在 Python 脚本中收到 NoneType 错误。我也应该把这个放在问题中。
-
请也添加错误信息。
-
用相机捕获一堆图像以保存并使用“静态”代码运行是否有意义?这样您可能会发现哪些类型的图像会导致问题...
-
测试你的代码是否正确,是的!
标签: python-2.7 opencv eye-tracking