【问题标题】:How to mark features which are present in both arrays in OpenCV?如何标记 OpenCV 中两个数组中都存在的特征?
【发布时间】:2019-11-17 12:23:28
【问题描述】:

我正在尝试编写一个 Python 3.7 脚本,以使用将图像存储为 n 维 Numpy 数组的 OpenCV Haar 分类器文件来检测人脸和特征。现在我只使用两个功能: - 整个脸 - 眼睛 这两者都是使用两个不同的分类器获得的。 代码检测图像中这两个特征的存在,然后使用 cv2.rectangle() 函数在每个特征的 for 循环内用一个矩形标记它们,即一个用于检测到的面部,一个用于检测到的眼睛。

我希望脚本仅在面部阵列和眼睛阵列中找到这些点时才标记眼睛矩形。 numpy.intersect1d() 只在一维数组中查找交集。

我试过了

for x,y,w,h in eyes and x,y,w,h in faces:

它所做的只是返回这个错误信息:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

任何帮助将不胜感激。

这是在 Windows 10 64 位上尝试的,代码是用 Pycharm 2019 编写的,OpenCV 导入为 CV2。

# Creating the cascade classifier objects.
face_cascade = 
cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eye_cascade = cv2.CascadeClassifier("haarcascade_eye.xml")

# Entering and displaying the original image.
img = cv2.imread("filename.jpg", 1)
cv2.imshow("Original Images",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

# Convert the image to Black and White and store in a variable.
gry_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

faces = face_cascade.detectMultiScale(gry_img, scaleFactor = 1.05, minNeighbors = 8)
eyes = eye_cascade.detectMultiScale(gry_img, scaleFactor = 1.30, minNeighbors = 12)

# Now, we mark the detected features with a rectangle on the original image.
for x,y,w,h in faces:
    img = cv2.rectangle(img, (x,y), (x+w, y+h), (255, 21, 21), 3) # Blue.


for x,y,w,h in eyes:
    img = cv2.rectangle(img, (x,y), (x+w, y+h), (255, 255, 24), 3) # Cyan.

cv2.imshow("Detected Faces and Eyes",img)
cv2.waitKey(0)
cv2.destroyAllWindows()

只有在面部​​特征数组中也找到眼睛特征时,我才需要标记它们。

【问题讨论】:

    标签: python python-3.x numpy opencv haar-classifier


    【解决方案1】:

    OpenCV docs 中已经提出了解决此问题的更好方法。它建议不要通过整个图像来检测眼睛,而应该首先检测面部,然后裁剪图像,然后使用裁剪后的图像进行眼睛检测。为了裁剪面部图像,您可以使用 numpy 切片:

    for x,y,w,h in faces:
        face_img = gry_img[y:y+h, x:x+w]
        # Now detect the eyes in this `face_img` only.
        eyes = eye_cascade.detectMultiScale(face_img, scaleFactor = 1.30, minNeighbors = 12)
    

    【讨论】:

    【解决方案2】:

    天啊,我现在是不是觉得自己很蠢。好了:

    正如 ZdaR 所说,OpenCV 文档确实给出了一个完美的例子来说明如何达到我需要的结果,其要点是:

    for x,y,w,h in faces:
        face_img = gry_img[y:y+h, x:x+w]
        # Now detect the eyes in this `face_img` only.
        eyes = eye_cascade.detectMultiScale(face_img, scaleFactor = 1.30, minNeighbors = 12)
    

    除此之外,我需要做的显然是将原始图像裁剪为面部坐标以及要绘制的眼睛特征。我仍然不确定这 是否应该 工作,但现在看来添加额外的行 确实 工作。

    最终成功的代码:

    faces = face_cascade.detectMultiScale(gry_img, scaleFactor = 1.05, minNeighbors = 10)
    for (x, y, w, h) in faces:
        cv2.rectangle(img, (x, y),(x + w, y + h), (255, 0, 0), 3)
        gry_eye = gry_img[y:y+h, x:x+w]
        eye_img = img[y:y + h, x:x + w]
        eyes = eye_cascade.detectMultiScale(gry_eye, scaleFactor = 1.05, minNeighbors = 5)
        for (x, y, w, h) in eyes:
            cv2.rectangle(eye_img, (x, y), (x + w, y + h), (255, 255, 24), 3)
    

    嗯,这是一次学习经历。 感谢 ZdaR 的帮助!

    【讨论】:

    • 很高兴知道我能帮到你:)
    猜你喜欢
    • 2018-06-03
    • 2019-09-14
    • 1970-01-01
    • 1970-01-01
    • 2012-02-22
    • 2021-12-19
    • 2021-01-03
    • 2014-05-03
    • 2015-04-01
    相关资源
    最近更新 更多