【问题标题】:problem with circle correct eye detection in opencvopencv中圆形正确眼睛检测的问题
【发布时间】:2021-09-02 07:37:24
【问题描述】:

我在下面的程序中遇到了正确的眼睛检测问题。问题是在错误的位置检测眼睛。如果有人知道答案,请帮忙

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_eye.xml')

while True:
    ret, frame = cap.read()  

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)  
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)  
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 5)
        roi_gray = gray[y:y + w, x:x + w]  
        roi_color = frame[y:y + h, x:x + w]  
        eyes = eye_cascade.detectMultiScale(roi_gray, 1.3, 5)  
        for (ex, ey, ew, eh) in eyes:
           # center_coordinates = ex + ew // 2, ey + eh // 2
            radius = eh // 2
            cv2.circle(roi_color, (ex, ey), radius, (0, 0, 255), 5)
         #cv2.rectangle(roi_color, (ex, ey), (ex + ew, ey + eh), (0, 255, 0), 5)

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

【问题讨论】:

  • 您是打算在roi_gray = gray[y:y + w, x:x + w] 中使用y+w 还是在roi_gray = gray[y:y + h, x:x + w] 中使用y+h
  • 我修好了,但对我的问题没有帮助:(

标签: python opencv face-detection


【解决方案1】:

当你用cv2.rectangle 画一个矩形时,你需要左上角和右下角,但是当你用cv2.circle 画一个圆时,你需要中心点。您使用的 cv2.circle 与初始矩形具有相同的坐标,您需要先将它们转换为中心。添加这个,它应该可以工作

for (ex,ey,ew,eh) in eyes:
    radius = eh//2
    eye_x = int(ex+0.5*ew)
    eye_y = int(ey+0.5*ey)
    cv2.circle(roi_color, (eye_x, eye_y), radius, (0, 0, 255), 5)

【讨论】:

  • 非常感谢
猜你喜欢
  • 2011-02-16
  • 2020-03-05
  • 2012-08-27
  • 2018-10-04
  • 1970-01-01
  • 2011-05-02
  • 2018-05-02
  • 1970-01-01
  • 2013-01-03
相关资源
最近更新 更多