【发布时间】:2016-04-30 14:44:52
【问题描述】:
我正在为我的学校项目进行瞳孔检测。这是我第一次使用 OpenCV 和 Python,使用 Python 版本 3.4.2 和 OpenCV 3.1.0。
我正在使用 Raspberry Pi NoIR 相机,并且我得到了很好的图像。
但我无法很好地检测到瞳孔(因为闪烁、睫毛和阴影。 我参考了网上的一些代码,以下是该代码的一部分。
...
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
image = frame.array
cv2.imshow("image", image)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
retval, thresholded = cv2.threshold(gray, 80, 255, 0)
cv2.imshow("threshold", thresholded)
closed = cv2.erode(cv2.dilate(thresholded, kernel, iterations=1), kernel, iterations=1)
#closed = cv2.morphologyEx(close, cv2.MORPH_CLOSE, kernel)
cv2.imshow("closed", closed)
thresholded, contours, hierarchy = cv2.findContours(closed, cv2.RETR_LIST, cv2.CHAIN_APPROX_NONE)
drawing = np.copy(image)
cv2.drawContours(drawing, contours, -1, (255, 0, 0), 2)
for contour in contours:
area = cv2.contourArea(contour)
bounding_box = cv2.boundingRect(contour)
extend = area / (bounding_box[2] * bounding_box[3])
# reject the contours with big extend
if extend > 0.8:
continue
# calculate countour center and draw a dot there
m = cv2.moments(contour)
if m['m00'] != 0:
center = (int(m['m10'] / m['m00']), int(m['m01'] / m['m00']))
cv2.circle(drawing, center, 3, (0, 255, 0), -1)
# fit an ellipse around the contour and draw it into the image
try:
ellipse = cv2.fitEllipse(contour)
cv2.ellipse(drawing, box=ellipse, color=(0, 255, 0))
except:
pass
# show the frame
cv2.imshow("Drawing", drawing)
...
输入图片:
输出图像:
如何去除图片中与瞳孔无关的部分,如上图?
除了答案,也欢迎任何提示。
【问题讨论】:
-
相关:Speeding up vectorized eye-tracking algorithm in numpy。您还可以检查循环性 (example code)。
-
其他选项:使用HoughCircles 直接检测圆圈和/或如果内部区域比外部更暗,则仅保留轮廓。如果眼睛始终居中且距离相同,您还可以定义感兴趣区域 (ROI) + 使用该区域。
-
我会在二值图像上使用erode,然后简单地使用HoughCircles 来检测图像中最重要的圆圈。
-
谢谢!我尝试只使用侵蚀,但有时结果不够好......但完整的形态学对性能不利(丢帧等)。