首页 > 解决方案 > 与红眼检测混淆

问题描述

首先,我没有要求任何人做我的功课。我想得到解释或澄清我在理解以下问题方面的困难。

我刚刚完成了图像处理测试,但是由于我的困惑,我无法解决一个问题。

问题是:

使用以下 HSL 颜色空间公式编写代码以在 RGB 颜色空间中检测给定图像中的红眼:

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

请注意,在上面的公式中,H、S 和 L 表示 HSL 颜色空间中图像的单个像素值,'eye_pixel' 的值将取决于 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)

假设图像是:

在此处输入图像描述

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

谢谢你。

标签: pythonopencvimage-processinghsl

解决方案


您需要做的就是根据整个 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]))

正常和红眼


推荐阅读