'how can i move the eye coordenates in an matrix [closed]

I'm a novice in python and I'm trying to normalize an image in accordance with mpeg7 for a face detection algorithm however I'm having issues with changing the eyes coordinates to the specified ones so my question is how can I move the eyes coordinates on a matrix for example right eye [50, 70] and left eye [65, 72] and I want to turn there coordinates into the right eye [15, 24] left eye[32, 24].

correctly I've done the face and eyes detection through haar cascade, I've performed face rotation and I've scaled it already. however, mpeg7 dictates that the image should be normalized the dimension 46 by 56 monochrome with 255 levels and eyes align horizontally at line 24 and columns 16 and 32 respectively. for the code, I've followed this link https://datahacker.rs/010-how-to-align-faces-with-opencv-in-python/



Solution 1:[1]

So, my approach assumes that you are given any (colored) image as (a numpy array) alongside the coordinates (indices) of where the eyes are as well as where they should be. It then

  1. shifts the image such that the midpoint of the eyes is at the midpoint of the image (after which the midpoint is at the desired location),
  2. rotates the shifted image by the angle described by the slope of the line going through the eye coordinates and the line going through the target eye coordinates around the image center (after which the orientation of the eyes is at the desired angle),
  3. zooms the rotated image by the factor described by the ratio of the distance between the target eye coordinates and the distance between the original eye coordinates (after which the distance between the eyes is as desired)
  4. and crops the image to be of the same width and height as the original again (after which the image size is as desired).
import numpy as np
from skimage.io import imread
import matplotlib.pyplot as plt
from requests import get
from scipy.ndimage import rotate, zoom

# 1. load random image of a face
url = "https://i2-prod.manchestereveningnews.co.uk/incoming/article14444584.ece/ALTERNATES/s615b/409.png"

img = imread(url)

# 2. specify current and target eye coordinates (i.e. indices)

# current eye coords (assuming as given, here manually determined)
right_eye_coords = [164, 102]
left_eye_coords = [127, 150]
eye_coords = [right_eye_coords, left_eye_coords]

# target: eye line should be horizontal & each eye shold be 1/6-th of image width from center
target_right_eye_coords = [img.shape[0] // 2, img.shape[1] // 3]
target_left_eye_coords = [img.shape[0] // 2, img.shape[1] // 3 * 2]
target_eye_coords = [target_right_eye_coords, target_left_eye_coords]

def imshow_wrapper(img):
    plt.imshow(img)
    plt.axis('off')
    plt.show()

imshow_wrapper(img)

Original Image

# 3. shift image
def shift_eyes_center_to_img_center(image, eye_coords):
    shifted_image = image.copy()
    img_center = image.shape[0] // 2, image.shape[1] // 2
    eyes_center = [(eye_coords[0][i] + eye_coords[1][i]) // 2 for i in [0, 1]]
    diffs = [img_center[i] - eyes_center[i] for i in [0, 1]]
    col = 255
    for ax in [0, 1]:
        shifted_image = np.roll(shifted_image, diffs[ax], axis=ax)
    if diffs[0] > 0:
        shifted_image[:diffs[0], :, :] = col
    elif diffs[0] < 0:
        shifted_image[diffs[0]:, :, :] = col
    if diffs[1] > 0:
        shifted_image[:, :diffs[1], :] = col
    elif diffs[1] < 0:
        shifted_image[:, diffs[0]:, :] = col
    eye_coords_shifted = [[eye_coords[i][j] + diffs[j] for j in [0, 1]] for i in [0, 1]]
    return shifted_image, eye_coords_shifted

shifted_image, eye_coords_shifted = shift_eyes_center_to_img_center(img, eye_coords)
imshow_wrapper(shifted_image)

enter image description here

# 4. rotate image
def rotate_image_to_horizontal_eye_line(image, eye_coords, target_eye_coords):
    eyeline_slope = -(eye_coords[0][0] - eye_coords[1][0]) / (eye_coords[0][1] - eye_coords[1][1])
    target_eyeline_slope = -(target_eye_coords[0][0] - target_eye_coords[1][0]) / (target_eye_coords[0][1] - target_eye_coords[1][1])
    rotation_rad = (target_eyeline_slope - eyeline_slope) / (1 + target_eyeline_slope * eyeline_slope)
    rotation_deg = np.rad2deg(rotation_rad)
    rotated_image = rotate(image, rotation_deg, reshape=False, cval=255)
    return rotated_image

rotated_image = rotate_image_to_horizontal_eye_line(shifted_image, eye_coords_shifted, target_eye_coords)
imshow_wrapper(rotated_image)

enter image description here

# 5. zoom and resize
def zoom_and_resize_image(image, eye_coords, target_eye_coords):
    eyes_dist = ((eye_coords[0][0] - eye_coords[1][0]) ** 2 + (eye_coords[0][1] - eye_coords[1][1]) ** 2) ** 0.5
    target_eyes_dist = ((target_eye_coords[0][0] - target_eye_coords[1][0]) ** 2 + (target_eye_coords[0][1] - target_eye_coords[1][1]) ** 2) ** 0.5
    zoom_factor = target_eyes_dist / eyes_dist
    zoomed_image = zoom(image, (zoom_factor, zoom_factor, 1))
    midi, midj = zoomed_image.shape[0] // 2, zoomed_image.shape[1] // 2
    midi_orig, midj_orig = image.shape[0] // 2, image.shape[1] // 2
    zoomed_image_resized = zoomed_image[midi-midi_orig:midi+midi_orig+1, midj-midj_orig:midj+midj_orig+1, :]
    return zoomed_image_resized

final_image = zoom_and_resize_image(rotated_image, eye_coords, target_eye_coords)
imshow_wrapper(final_image)

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1