OpenCV和Python中的瞳孔检测

Yeo*_*on_ 9 python opencv

我正在为我的学校项目进行学生检测.这是我第一次使用Python版本3.4.2和OpenCV 3.1.0来使用OpenCV和Python.

我正在使用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)

    ...
Run Code Online (Sandbox Code Playgroud)

输入图片:

在此输入图像描述

输出图像:

在此输入图像描述

如何删除图像中与瞳孔无关的部分,如上所示?

除了答案,任何提示也欢迎.

sie*_*hie 7

你可以做几件事.它们的工作效果取决于您想要应用算法的图像的变化程度.您可以做出几个假设,然后丢弃所有不符合它们的候选人.

删除小的检测

首先,我会考虑通过在循环开头添加此行来删除太小的候选项:

if area < 100:
    continue
Run Code Online (Sandbox Code Playgroud)

阈值是随机选择的,对于这个特定的图像效果很好.它删除了几乎所有的错误检测.只剩下最大的一个.但是你必须根据你的其他图像进行检查,并根据你的需要进行调整.

在此输入图像描述

删除非圆形的检测

您可以做出的另一个假设是,学生通常是圆形的,您可以删除每个非"圆"的检测.圆度的一个简单测量是观察周长与面积的比率.

circumference = cv2.arcLength(contour,True)
circularity = circumference ** 2 / (4*math.pi*area)
Run Code Online (Sandbox Code Playgroud)

右侧阴影的圆形度约为2.72,瞳孔的圆形度约为1.31.

改善圆度

你注意到,由于反射,瞳孔的轮廓不完美.您可以通过计算轮廓的凸包来改善这一点.

contour = cv2.convexHull(contour)
Run Code Online (Sandbox Code Playgroud)

如果在计算面积和周长之前这样做,则圆度值为1.01和1.37.(一个完美的圆的圆度为1)这意味着反射的缺陷几乎完全被修复了.在这种情况下,这可能不是必需的,但在有更多反射的情况下可能有用.

在此输入图像描述