混淆红眼检测

Confusion with red eye detection

首先,我不是要任何人做我的作业。我想得到解释或澄清我在理解以下问题时遇到的困难。

我刚刚考完图像处理,但是有一道题我一头雾水没能解决

问题是:

使用以下 HSL 颜色公式 space:

编写代码以检测 RGB 颜色 space 给定图像中的红眼

LS_ratio=长/小

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

请注意,在上面的公式中,H、S 和 L 表示 HSL 颜色图像的单个像素值 space,'eye_pixel' 的值将为 True 或 False取决于 H、S 和 L 的值(即它是否是红眼颜色像素)。 您的任务是编写代码来检查图像中的所有像素。将结果存储为 numpy 数组并显示结果图像。

我的代码是:

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)

假设图像是:

我真的对需要做什么感到困惑。如果有人帮助向我解释应该做什么,我将不胜感激。

谢谢。

您需要做的就是根据整个 H、L、S 图像执行公式。

#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)

这里的redeye是一个bool数组,和你的原图大小一样,其中每个像素包含一个True或False,代表它是否是一个红眼像素。如果我显示图像:

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