0

First of all, I am not asking anyone to do my homework. I would like to get an explanation or clarification about my difficulties in understanding the following question.

I just finished my image processing test, but one question that I could not solve due to my confusion.

The question is:

Write the code to detect the red eye in a given image in RGB color space using the following formula for HSL color space:

LS_ratio = L / S

eye_pixel = (L >= 64) and (S >= 100) and (LS_ratio > 0.5) and (LS_ratio < 1.5) and ((H <= 7) or (H >= 162))

Please note that in above formula, H, S and L represent a single pixel value for the image in HSL color space and the value of ‘eye_pixel’ will be either True or False depending on the values of H, S and L (i.e. it will be either a red eye color pixel or not). Your task is to write the code to check all pixels in the image. Store the result as a numpy array and display the resulted image.

My code is:

from __future__ import print_function
import numpy as np
import argparse
import cv2

#argument paser
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True, help = "Path to the image")
args = vars(ap.parse_args())

#load the image
image = cv2.imread(args["image"])

#Convert image to HLS
hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)

#Split HLS Channels
H = hls[:, :, 0]
S = hls[:, :, 1]
L = hls[:, :, 2]
LS_ratio = L / S

#eye_pixel = (L >= 64) and (S >= 100) and (LS_ratio > 0.5) and (LS_ratio < 1.5) and ((H <= 7) or (H >= 162))

#if HSL pixel
#eye pixel either red or not
#show the image
#cv2.imshow("Image", np.hstack([image, red_eye]))


#debug
print("Lightness is: {}".format(L))
print("Saturation is: {}".format(S))
print("Hue is: {}".format(H))
#print("LS ratio: {}", LS_ratio)

cv2.waitKey(0)

Suppose that the image is:

enter image description here

I literally feel confused about what needs to be done. Highly appreciate if anyone helps explains to me what should be done.

Thank you.

1 Answers1

1

All you need to do is implement the formula in term of the entire H, L, S images.

#Convert image to HLS
hls = cv2.cvtColor(image, cv2.COLOR_BGR2HLS)

#Split HLS Channels
H = hls[:, :, 0]
L = hls[:, :, 1]
S = hls[:, :, 2]
LS_ratio = L/(S + 1e-6)

redeye = ((L>=64) * (S>=100) * np.logical_or(H<=7, H>=162) * (LS_ratio>0.5) * (LS_ratio<1.5)).astype(bool)

Here redeye is a bool array the same size of your original image, where each pixel contains a True or False, representing whether if it's a redeye pixel or not. If I display the image:

redeye = cv2.cvtColor(redeye.astype(np.uint8)*255, cv2.COLOR_GRAY2BGR)
cv2.imshow('image-redeye', np.hstack([image, redeye]))

normal and redeye

Mercury
  • 3,417
  • 1
  • 10
  • 35
  • Thank you for your answer. highly appreciate it. I have one more question, what should I do to display the original image and the image with red eye only? – mrsimone010101 Nov 13 '20 at 08:31
  • Do you mean displaying "only the redeye part of the original image"? Or do you mean showing the original image and the redeye image separately? You can do the latter by simply not using np.hstack, and using imshow separately on `image` or `redeye`. To do the former, use the redeye bool array you initially get as a mask. – Mercury Nov 13 '20 at 08:42
  • The former: Display the original image and the image with red eye only horizontally stacked. I will try it on my own. – mrsimone010101 Nov 13 '20 at 08:45
  • I have tried your method. I was using only redeye bool array. Did you get the red color instead of white? – mrsimone010101 Nov 13 '20 at 09:21
  • Something like: `redeye_only = image.copy(); redeye_only[np.where(np.logical_not(redeye_bool_array))] = 255` – Mercury Nov 13 '20 at 18:40
  • Do you have a GitHub account? It would be nice to correspondent with you – mrsimone010101 Nov 14 '20 at 11:47