def find_pupil(eye_img_bgr, debug_index=False):

    eye_img_r = cv2.cvtColor(eye_img_bgr, cv2.COLOR_BGR2GRAY)

    # Scale to small image for faster computation
    scale = __fast_width / eye_img_r.shape[1]
    small_size = (int(__fast_width),
                  int((__fast_width / eye_img_r.shape[1]) *
                      eye_img_r.shape[0]))
    eye_img_small = cv2.resize(eye_img_r, small_size)
    eye_img_small = cv2.GaussianBlur(eye_img_small, (3, 3), 0)

    center_map = get_center_map(eye_img_small)

    max_val_index = np.argmax(center_map)
    pupil_y0, pupil_x0 = max_val_index // center_map.shape[
        1], max_val_index % center_map.shape[1]

    # Scale back to original coordinates
    pupil_y0, pupil_x0 = int((pupil_y0 + 0.5) / scale), int(
        (pupil_x0 + 0.5) / scale)

    if debug_index:

        eye_img_r_debug = cv2.cvtColor(eye_img_r, cv2.COLOR_GRAY2BGR)
        debug_img = eye_img_bgr.copy()

        cmap_norm = cv2.normalize(center_map,
                                  0,
                                  255,
                                  norm_type=cv2.NORM_MINMAX).astype(np.uint8)
        center_map_big = cv2.resize(
            cmap_norm,
            (eye_img_r.shape[1], eye_img_r.shape[0])).astype(np.uint8)
        center_map_big = cv2.cvtColor(center_map_big, cv2.COLOR_GRAY2BGR)

        overlay_img = cv2.addWeighted(center_map_big, 0.9, eye_img_r_debug,
                                      0.1, 1)

        draw_utils.draw_cross(debug_img, (pupil_x0, pupil_y0), (0, 255, 255),
                              6)
        draw_utils.draw_cross(overlay_img, (pupil_x0, pupil_y0), (255, 0, 0),
                              6)

        # stacked_small_size = image_utils.stack_imgs_vertical([eye_img_small, cmap_norm])
        stacked_imgs = image_utils.stack_imgs_horizontal(
            [debug_img, overlay_img])
        __debug_imgs[debug_index] = stacked_imgs

        if debug_index == 2:
            full_debug_img = image_utils.stack_imgs_vertical(
                [__debug_imgs[1], __debug_imgs[2]])
            cv2.imshow(__winname, full_debug_img)
        elif debug_index > 2:
            cv2.imshow(__winname, stacked_imgs)

    return pupil_x0, pupil_y0
Ejemplo n.º 2
0
def find_pupil(eye_img_bgr, debug_index=False):
    """ Estimates the centre of the pupil using image gradients
    """

    eye_img_r = cv2.split(eye_img_bgr)[2]  # Extract red channel only

    # Scale to small image for faster computation
    scale = __fast_width / eye_img_bgr.shape[0]
    small_size = (int(
        (__fast_width / eye_img_bgr.shape[0]) * eye_img_bgr.shape[1]),
                  int(__fast_width))
    eye_img_small = cv2.resize(eye_img_r, small_size)

    center_map = get_center_map(eye_img_small)

    max_val_index = np.argmax(center_map)
    pupil_y0, pupil_x0 = max_val_index // center_map.shape[
        1], max_val_index % center_map.shape[1]

    # Scale back to original coordinates
    pupil_y0, pupil_x0 = int((pupil_y0 + 0.5) / scale), int(
        (pupil_x0 + 0.5) / scale)

    if debug_index:

        eye_img_r_debug = cv2.cvtColor(eye_img_r, cv2.COLOR_GRAY2BGR)
        debug_img = eye_img_bgr.copy()
        cmap_norm = cv2.normalize(center_map,
                                  alpha=0,
                                  beta=255,
                                  norm_type=cv2.NORM_MINMAX)
        center_map_big = cv2.resize(
            cmap_norm, (eye_img_bgr.shape[1], eye_img_bgr.shape[0]))
        center_map_big = cv2.cvtColor(center_map_big.astype(np.uint8),
                                      cv2.COLOR_GRAY2BGR)
        draw_utils.draw_cross(debug_img, (pupil_x0, pupil_y0), (0, 255, 255),
                              6)
        draw_utils.draw_cross(center_map_big, (pupil_x0, pupil_y0),
                              (255, 0, 0), 6)

        stacked_imgs = image_utils.stack_imgs_horizontal(
            [debug_img, eye_img_r_debug, center_map_big])
        __debug_imgs[debug_index] = stacked_imgs

        if debug_index == 2:
            full_debug_img = image_utils.stack_imgs_vertical(
                [__debug_imgs[1], __debug_imgs[2]])
            cv2.imshow(__winname, full_debug_img)
        elif debug_index > 2:
            cv2.imshow(__winname, stacked_imgs)

    return pupil_x0, pupil_y0
Ejemplo n.º 3
0
    def find_limbus_edge_pts(self, eye_roi, debug=False):

        blurred_eye_roi_img = cv2.GaussianBlur(eye_roi.img, (5, 5), 5)

        pupil_x0, pupil_y0 = eye_roi.img.shape[1] / 2, eye_roi.img.shape[0] / 2

        min_limb_r = int(eye_roi.img.shape[0] * self.limb_r_ratios[0])
        max_limb_r = int(eye_roi.img.shape[0] * self.limb_r_ratios[1])

        pts_found = set()
        pts_found = pts_found.union(
            self.cast_rays_spread(bgr_img=blurred_eye_roi_img,
                                  start_pos=(pupil_x0, pupil_y0),
                                  angle_mean=0,
                                  spread=120,
                                  limb_r_range=(min_limb_r, max_limb_r)))
        pts_found = pts_found.union(
            self.cast_rays_spread(bgr_img=blurred_eye_roi_img,
                                  start_pos=(pupil_x0, pupil_y0),
                                  angle_mean=180,
                                  spread=120,
                                  limb_r_range=(min_limb_r, max_limb_r)))

        if debug:
            debug_img = blurred_eye_roi_img.copy()
            cv2.circle(blurred_eye_roi_img,
                       (eye_roi.img.shape[1] / 2, eye_roi.img.shape[0] / 2),
                       min_limb_r, (255, 255, 0))
            cv2.circle(blurred_eye_roi_img,
                       (eye_roi.img.shape[1] / 2, eye_roi.img.shape[0] / 2),
                       max_limb_r, (255, 255, 0))
            draw_cross(debug_img, (pupil_x0, pupil_y0),
                       color=(255, 255, 0),
                       width=6)
            draw_points(debug_img,
                        pts_found, (0, 0, 255),
                        width=1,
                        thickness=2)
            stacked_imgs = np.concatenate(
                [eye_roi.img, blurred_eye_roi_img, debug_img], axis=1)

            if debug == 1:
                self.full_debug_img = stacked_imgs
            elif debug == 2:
                self.full_debug_img = stack_imgs_vertical(
                    [self.full_debug_img, stacked_imgs])
                cv2.imshow(winname, self.full_debug_img)
            elif debug == 3:
                cv2.imshow(winname, stacked_imgs)

        return pts_found
def find_pupil(eye_img_bgr, debug_index=False):
    
    eye_img_r = cv2.cvtColor(eye_img_bgr, cv2.COLOR_BGR2GRAY)
    
    # Scale to small image for faster computation
    scale = __fast_width / eye_img_r.shape[1]
    small_size = (int(__fast_width), int((__fast_width / eye_img_r.shape[1]) * eye_img_r.shape[0]))
    eye_img_small = cv2.resize(eye_img_r, small_size)
    eye_img_small = cv2.GaussianBlur(eye_img_small, (3, 3), 0)
    
    center_map = get_center_map(eye_img_small)
    
    max_val_index = np.argmax(center_map)
    pupil_y0, pupil_x0 = max_val_index // center_map.shape[1], max_val_index % center_map.shape[1]
    
    # Scale back to original coordinates
    pupil_y0, pupil_x0 = int((pupil_y0 + 0.5) / scale), int((pupil_x0 + 0.5) / scale)
    
    if debug_index:
        
        eye_img_r_debug = cv2.cvtColor(eye_img_r, cv2.COLOR_GRAY2BGR)
        debug_img = eye_img_bgr.copy()
        
        cmap_norm = cv2.normalize(center_map, 0, 255, norm_type=cv2.NORM_MINMAX).astype(np.uint8)
        center_map_big = cv2.resize(cmap_norm, (eye_img_r.shape[1], eye_img_r.shape[0])).astype(np.uint8)
        center_map_big = cv2.cvtColor(center_map_big, cv2.COLOR_GRAY2BGR)
        
        overlay_img = cv2.addWeighted(center_map_big, 0.9, eye_img_r_debug, 0.1, 1)
                
        draw_utils.draw_cross(debug_img, (pupil_x0, pupil_y0), (0, 255, 255), 6)
        draw_utils.draw_cross(overlay_img, (pupil_x0, pupil_y0), (255, 0, 0), 6)
        
        # stacked_small_size = image_utils.stack_imgs_vertical([eye_img_small, cmap_norm])
        stacked_imgs = image_utils.stack_imgs_horizontal([debug_img, overlay_img])
        __debug_imgs[debug_index] = stacked_imgs
        
        if debug_index == 2:
            full_debug_img = image_utils.stack_imgs_vertical([__debug_imgs[1], __debug_imgs[2]]);
            cv2.imshow(__winname, full_debug_img)
        elif debug_index > 2:
            cv2.imshow(__winname, stacked_imgs);

    return pupil_x0, pupil_y0
def find_pupil(eye_img_bgr, debug_index=False):
    
    """ Estimates the centre of the pupil using image gradients
    """

    eye_img_r = cv2.split(eye_img_bgr)[2]   # Extract red channel only
    
    # Scale to small image for faster computation
    scale = __fast_width / eye_img_bgr.shape[0]
    small_size = (int((__fast_width / eye_img_bgr.shape[0]) * eye_img_bgr.shape[1]), int(__fast_width))
    eye_img_small = cv2.resize(eye_img_r, small_size)
    
    center_map = get_center_map(eye_img_small)
    
    max_val_index = np.argmax(center_map)
    pupil_y0, pupil_x0 = max_val_index // center_map.shape[1], max_val_index % center_map.shape[1]
    
    # Scale back to original coordinates
    pupil_y0, pupil_x0 = int((pupil_y0 + 0.5) / scale), int((pupil_x0 + 0.5) / scale)
    
    if debug_index:
        
        eye_img_r_debug = cv2.cvtColor(eye_img_r, cv2.COLOR_GRAY2BGR)
        debug_img = eye_img_bgr.copy()
        cmap_norm = cv2.normalize(center_map, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
        center_map_big = cv2.resize(cmap_norm, (eye_img_bgr.shape[1], eye_img_bgr.shape[0]))
        center_map_big = cv2.cvtColor(center_map_big.astype(np.uint8), cv2.COLOR_GRAY2BGR)
        draw_utils.draw_cross(debug_img, (pupil_x0, pupil_y0), (0, 255, 255), 6)
        draw_utils.draw_cross(center_map_big, (pupil_x0, pupil_y0), (255, 0, 0), 6)
        
        stacked_imgs = image_utils.stack_imgs_horizontal([debug_img, eye_img_r_debug, center_map_big])
        __debug_imgs[debug_index] = stacked_imgs
        
        if debug_index == 2:
            full_debug_img = image_utils.stack_imgs_vertical([__debug_imgs[1], __debug_imgs[2]]);
            cv2.imshow(__winname, full_debug_img)
        elif debug_index > 2:
            cv2.imshow(__winname, stacked_imgs);
            
    return pupil_x0, pupil_y0
Ejemplo n.º 6
0
    def find_limbus_edge_pts(self, eye_roi, debug=False):
        
        blurred_eye_roi_img = cv2.GaussianBlur(eye_roi.img, (5, 5), 5)

        pupil_x0, pupil_y0 = eye_roi.img.shape[1] / 2, eye_roi.img.shape[0] / 2
    
        min_limb_r = int(eye_roi.img.shape[0] * self.limb_r_ratios[0])
        max_limb_r = int(eye_roi.img.shape[0] * self.limb_r_ratios[1])
        
        pts_found = set()
        pts_found = pts_found.union(self.cast_rays_spread(bgr_img=blurred_eye_roi_img,
                                                          start_pos=(pupil_x0, pupil_y0),
                                                          angle_mean=0, spread=120,
                                                          limb_r_range=(min_limb_r, max_limb_r)))
        pts_found = pts_found.union(self.cast_rays_spread(bgr_img=blurred_eye_roi_img,
                                                          start_pos=(pupil_x0, pupil_y0),
                                                          angle_mean=180, spread=120,
                                                          limb_r_range=(min_limb_r, max_limb_r)))
                    
        if debug:
            debug_img = blurred_eye_roi_img.copy()
            cv2.circle(blurred_eye_roi_img, (eye_roi.img.shape[1] / 2, eye_roi.img.shape[0] / 2), min_limb_r, (255, 255, 0))
            cv2.circle(blurred_eye_roi_img, (eye_roi.img.shape[1] / 2, eye_roi.img.shape[0] / 2), max_limb_r, (255, 255, 0))
            draw_cross(debug_img, (pupil_x0, pupil_y0), color=(255, 255, 0), width=6)
            draw_points(debug_img, pts_found, (0, 0, 255), width=1, thickness=2)
            stacked_imgs = np.concatenate([eye_roi.img, blurred_eye_roi_img, debug_img], axis=1)
            
            if debug == 1:
                self.full_debug_img = stacked_imgs
            elif debug == 2:
                self.full_debug_img = stack_imgs_vertical([self.full_debug_img, stacked_imgs])
                cv2.imshow(winname, self.full_debug_img)
            elif debug == 3:
                cv2.imshow(winname, stacked_imgs);
        
        return pts_found
Ejemplo n.º 7
0
def find_pupil(eye_img_bgr,
               fast_width_grads=25.5,
               fast_width_iso=80,
               weight_grads=0.9,
               weight_iso=0.1,
               debug_index=False):

    eye_img_r = cv2.split(eye_img_bgr)[2]

    fast_size_grads = (int(
        (fast_width_grads / eye_img_bgr.shape[0]) * eye_img_bgr.shape[1]),
                       int(fast_width_grads))
    fast_img_grads = cv2.resize(eye_img_r, fast_size_grads)

    fast_size_iso = (int(fast_width_iso),
                     int((fast_width_iso / eye_img_r.shape[1]) *
                         eye_img_r.shape[0]))
    fast_img_iso = cv2.resize(eye_img_r, fast_size_iso)

    c_map_grads = eye_center_locator_gradients.get_center_map(fast_img_grads)
    c_map_iso = eye_center_locator_isophote.get_center_map(fast_img_iso)

    c_map_norm_grads = cv2.normalize(c_map_grads,
                                     alpha=0,
                                     beta=255,
                                     norm_type=cv2.NORM_MINMAX)
    c_map_big_grads = cv2.resize(
        c_map_norm_grads,
        (eye_img_bgr.shape[1], eye_img_bgr.shape[0])).astype(np.uint8)

    c_map_norm_iso = cv2.normalize(c_map_iso,
                                   alpha=0,
                                   beta=255,
                                   norm_type=cv2.NORM_MINMAX)
    c_map_big_iso = cv2.resize(
        c_map_norm_iso,
        (eye_img_bgr.shape[1], eye_img_bgr.shape[0])).astype(np.uint8)

    joint_c_map = cv2.addWeighted(c_map_big_grads, w_grads, c_map_big_iso,
                                  w_iso, 1.0)

    max_val_index = np.argmax(joint_c_map)
    pupil_y0, pupil_x0 = max_val_index // joint_c_map.shape[
        1], max_val_index % joint_c_map.shape[1]

    max_val_index_2 = np.argmax(c_map_big_grads)
    pupil_y0_2, pupil_x0_2 = max_val_index_2 // joint_c_map.shape[
        1], max_val_index_2 % joint_c_map.shape[1]

    max_val_index_3 = np.argmax(c_map_big_iso)
    pupil_y0_3, pupil_x0_3 = max_val_index_3 // joint_c_map.shape[
        1], max_val_index_3 % joint_c_map.shape[1]

    if debug_index:

        debug_img = eye_img_bgr.copy()

        joint_c_map = cv2.cvtColor(joint_c_map, cv2.COLOR_GRAY2BGR)
        c_map_big_iso = cv2.cvtColor(c_map_big_iso, cv2.COLOR_GRAY2BGR)
        c_map_big_grads = cv2.cvtColor(c_map_big_grads, cv2.COLOR_GRAY2BGR)

        draw_utils.draw_cross(debug_img, (pupil_x0, pupil_y0), (0, 255, 255),
                              16, 2)
        draw_utils.draw_cross(joint_c_map, (pupil_x0_3, pupil_y0_3),
                              (255, 0, 255), 8, 2)
        draw_utils.draw_cross(joint_c_map, (pupil_x0_2, pupil_y0_2),
                              (255, 0, 255), 8, 2)
        draw_utils.draw_cross(joint_c_map, (pupil_x0, pupil_y0), (255, 0, 0),
                              16, 2)

        draw_utils.draw_cross(c_map_big_iso, (pupil_x0_3, pupil_y0_3),
                              (255, 0, 0), 16, 2)
        draw_utils.draw_cross(c_map_big_grads, (pupil_x0_2, pupil_y0_2),
                              (255, 0, 0), 16, 2)

        stacked_imgs = image_utils.stack_imgs_horizontal(
            [debug_img, c_map_big_grads, c_map_big_iso, joint_c_map])
        __debug_imgs[debug_index] = stacked_imgs

        if debug_index == 2:
            full_debug_img = image_utils.stack_imgs_vertical(
                [__debug_imgs[1], __debug_imgs[2]])
            cv2.imshow(__winname, full_debug_img)
        elif debug_index > 2:
            cv2.imshow(__winname, stacked_imgs)

    return pupil_x0, pupil_y0
Ejemplo n.º 8
0
def find_pupil(
    eye_img_bgr, fast_width_grads=25.5, fast_width_iso=80, weight_grads=0.9, weight_iso=0.1, debug_index=False
):

    eye_img_r = cv2.split(eye_img_bgr)[2]

    fast_size_grads = (int((fast_width_grads / eye_img_bgr.shape[0]) * eye_img_bgr.shape[1]), int(fast_width_grads))
    fast_img_grads = cv2.resize(eye_img_r, fast_size_grads)

    fast_size_iso = (int(fast_width_iso), int((fast_width_iso / eye_img_r.shape[1]) * eye_img_r.shape[0]))
    fast_img_iso = cv2.resize(eye_img_r, fast_size_iso)

    c_map_grads = eye_center_locator_gradients.get_center_map(fast_img_grads)
    c_map_iso = eye_center_locator_isophote.get_center_map(fast_img_iso)

    c_map_norm_grads = cv2.normalize(c_map_grads, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
    c_map_big_grads = cv2.resize(c_map_norm_grads, (eye_img_bgr.shape[1], eye_img_bgr.shape[0])).astype(np.uint8)

    c_map_norm_iso = cv2.normalize(c_map_iso, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
    c_map_big_iso = cv2.resize(c_map_norm_iso, (eye_img_bgr.shape[1], eye_img_bgr.shape[0])).astype(np.uint8)

    joint_c_map = cv2.addWeighted(c_map_big_grads, w_grads, c_map_big_iso, w_iso, 1.0)

    max_val_index = np.argmax(joint_c_map)
    pupil_y0, pupil_x0 = max_val_index // joint_c_map.shape[1], max_val_index % joint_c_map.shape[1]

    max_val_index_2 = np.argmax(c_map_big_grads)
    pupil_y0_2, pupil_x0_2 = max_val_index_2 // joint_c_map.shape[1], max_val_index_2 % joint_c_map.shape[1]

    max_val_index_3 = np.argmax(c_map_big_iso)
    pupil_y0_3, pupil_x0_3 = max_val_index_3 // joint_c_map.shape[1], max_val_index_3 % joint_c_map.shape[1]

    if debug_index:

        debug_img = eye_img_bgr.copy()

        joint_c_map = cv2.cvtColor(joint_c_map, cv2.COLOR_GRAY2BGR)
        c_map_big_iso = cv2.cvtColor(c_map_big_iso, cv2.COLOR_GRAY2BGR)
        c_map_big_grads = cv2.cvtColor(c_map_big_grads, cv2.COLOR_GRAY2BGR)

        draw_utils.draw_cross(debug_img, (pupil_x0, pupil_y0), (0, 255, 255), 16, 2)
        draw_utils.draw_cross(joint_c_map, (pupil_x0_3, pupil_y0_3), (255, 0, 255), 8, 2)
        draw_utils.draw_cross(joint_c_map, (pupil_x0_2, pupil_y0_2), (255, 0, 255), 8, 2)
        draw_utils.draw_cross(joint_c_map, (pupil_x0, pupil_y0), (255, 0, 0), 16, 2)

        draw_utils.draw_cross(c_map_big_iso, (pupil_x0_3, pupil_y0_3), (255, 0, 0), 16, 2)
        draw_utils.draw_cross(c_map_big_grads, (pupil_x0_2, pupil_y0_2), (255, 0, 0), 16, 2)

        stacked_imgs = image_utils.stack_imgs_horizontal([debug_img, c_map_big_grads, c_map_big_iso, joint_c_map])
        __debug_imgs[debug_index] = stacked_imgs

        if debug_index == 2:
            full_debug_img = image_utils.stack_imgs_vertical([__debug_imgs[1], __debug_imgs[2]])
            cv2.imshow(__winname, full_debug_img)
        elif debug_index > 2:
            cv2.imshow(__winname, stacked_imgs)

    return pupil_x0, pupil_y0