我正在使用OpenCV+Python从(脸部)图像中检测和提取眼镜。我遵循了这篇文章(https://stackoverflow.com/questi..)的推理思路,如下所示:
1)检测脸部
2)找出面部区域的最大轮廓,必须是眼镜的外框
3)找出面部区域的第二和第三大轮廓,必须是两个透镜的框架
4)提取这些轮廓之间基本上代表眼镜的区域
然而,findContours()确实找到了眼镜的外框作为轮廓,但它并没有精确地找到两个镜片的边框作为轮廓。在
我的源代码如下:import cv2
import numpy as np
import matplotlib.pyplot as plt
img = cv2.imread('Luc_Marion.jpg')
RGB_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Detect the face in the image
haar_face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = haar_face_cascade.detectMultiScale(gray_img, scaleFactor=1.1, minNeighbors=8);
# Loop in all detected faces - in our case it is only one
for (x,y,w,h) in faces:
cv2.rec