python - 与红眼检测混淆
问题描述
首先,我没有要求任何人做我的功课。我想得到解释或澄清我在理解以下问题方面的困难。
我刚刚完成了图像处理测试,但是由于我的困惑,我无法解决一个问题。
问题是:
使用以下 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)
假设图像是:
我真的对需要做什么感到困惑。如果有人帮助向我解释应该做什么,我将不胜感激。
谢谢你。
解决方案
您需要做的就是根据整个 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]))
推荐阅读
- c# - 如果开关内部会出现动作,我可以使用 C# 开关表达式吗?
- javascript - 将用户文档添加到 mongo 集合后;如何在他们的个人资料页面上显示该名称?
- azure-resource-manager - 如何更改 CDN 端点源类型
- git - 如何用另一个(上游)分支覆盖 GIT 分支中的所有文件?
- dataframe - R使用数据框时更改数据值
- javascript - how to displayed key and value in functional component inside return part
- accessibility - WCAG 2.0 AA 要求链接和周围文本之间的颜色对比度为 3:1?
- autodesk-forge - Autodesk Forge Viewer - 放大比例
- python - python/flask 应用程序码头化期间的问题
- flutter - 将 Dart 中的地图更新到 Firestore