使用python将检测到的人脸放入图像并显示在opencv的另一个窗口中
我不熟悉图像处理和opencv,但到目前为止,易于理解的函数和良好的文档使我能够尝试并理解一些级别的代码,如facedetection等 现在,当我检测到网络摄像头视频流中的人脸时,程序会在人脸周围画一个正方形。现在,我希望图像的大部分区域,在脸周围的正方形中,被创建为另一个图像。通过我所做的,我得到了一个矩形区域的图像,其中的脸甚至不存在 我已经使用了cv.GetSubRect()并理解了它的用法。例如:使用python将检测到的人脸放入图像并显示在opencv的另一个窗口中,python,opencv,face-detection,Python,Opencv,Face Detection,我不熟悉图像处理和opencv,但到目前为止,易于理解的函数和良好的文档使我能够尝试并理解一些级别的代码,如facedetection等 现在,当我检测到网络摄像头视频流中的人脸时,程序会在人脸周围画一个正方形。现在,我希望图像的大部分区域,在脸周围的正方形中,被创建为另一个图像。通过我所做的,我得到了一个矩形区域的图像,其中的脸甚至不存在 我已经使用了cv.GetSubRect()并理解了它的用法。例如: img=cv.LoadImage("C:\opencv\me.jpg") sub=cv.
img=cv.LoadImage("C:\opencv\me.jpg")
sub=cv.GetSubRect(img, (700,525,200,119))
cv.NamedWindow("result",1)
cv.ShowImage("result",sub)
- 给了我一张我眼睛的剪短照片
min_size = (17,17)
#max_size = (30,30)
image_scale = 2
haar_scale = 2
min_neighbors = 2
haar_flags = 0
# Allocate the temporary images
gray = cv.CreateImage((image.width, image.height), 8, 1)
smallImage = cv.CreateImage((cv.Round(image.width / image_scale),cv.Round (image.height / image_scale)), 8 ,1)
#eyeregion = cv.CreateImage((cv.Round(image.width / image_scale),cv.Round (image.height / image_scale)), 8 ,1)
#cv.ShowImage("smallImage",smallImage)
# Convert color input image to grayscale
cv.CvtColor(image, gray, cv.CV_BGR2GRAY)
# Scale input image for faster processing
cv.Resize(gray, smallImage, cv.CV_INTER_LINEAR)
# Equalize the histogram
cv.EqualizeHist(smallImage, smallImage)
# Detect the faces
faces = cv.HaarDetectObjects(smallImage, faceCascade, cv.CreateMemStorage(0),
haar_scale, min_neighbors, haar_flags, min_size)
#, max_size)
# If faces are found
if faces:
for ((x, y, w, h), n) in faces:
# the input to cv.HaarDetectObjects was resized, so scale the
# bounding box of each face and convert it to two CvPoints
pt1 = (int(x * image_scale), int(y * image_scale))
pt2 = (int((x + w) * image_scale), int((y + h) * image_scale))
cv.Rectangle(image, pt1, pt2, cv.RGB(255, 0, 0), 3, 4, 0)
face_region = cv.GetSubRect(image,(x,int(y + (h/4)),w,int(h/2)))
cv.ShowImage("face",face_region)
cv.SetImageROI(image, (pt1[0],
pt1[1],
pt2[0] - pt1[0],
int((pt2[1] - pt1[1]) * 0.7)))
eyes = cv.HaarDetectObjects(image, eyeCascade,
cv.CreateMemStorage(0),
eyes_haar_scale, eyes_min_neighbors,
eyes_haar_flags, eyes_min_size)
if eyes:
# For each eye found
for eye in eyes:
eye[0][0],eye[0][1] are x,y co-ordinates of the top-left corner of detected eye
eye[0][2],eye[0][3] are the width and height of the cvRect of the detected eye region (i mean c'mon, that can be made out from the for loop of the face detection)
# Draw a rectangle around the eye
ept1 = (eye[0][0],eye[0][1])
ept2 = ((eye[0][0]+eye[0][2]),(eye[0][1]+eye[0][3]))
cv.Rectangle(image,ept1,ept2,cv.RGB(0,0,255),1,8,0) # This is working..
ea = ept1[0]
eb = ept1[1]
ec = (ept2[0]-ept1[0])
ed = (ept2[1]-ept1[1])
# i've tried multiplying with image_scale to get the eye region within
# the window of eye but still i'm getting just a top-left area of the
# image, top-left to my head. It does make sense to multiply with image_scale right?
eyeregion=cv.GetSubRect(image, (ea,eb,ec,ed))
cv.ShowImage("eye",eyeregion)
我希望这段代码来自OpenCV/samples/Python。在cv.GetSubRect内坐标的参数中有一个小错误。请将上述程序的最后两行替换为以下内容:
a=pt1[0]
b=pt1[1]
c=pt2[0]-pt1[0]
d=pt2[1]-pt1[1]
face_region = cv.GetSubRect(image,(a,b,c,d))
cv.ShowImage("face",face_region)
请确保没有错误检测或多次检测。很抱歉这么晚才回复。我的互联网连接被切断了,因为一些账单支付不一致。无论如何,我所理解的是cv.Rectangle绘制了一个具有两个对角点的矩形,而cv.GetSubRect采用了一种矩形尺寸的cvRect形式(它只是一个左上角坐标,宽度和高度=x,y,w,h)。基本上,从pt2和pt1计算出w,h,分别是c和d。在cv.GetSubRect函数中使用了它们。现在我在获取眼睛区域时遇到了问题。我已经在主程序中添加了代码。请指定问题是什么?用于人脸检测的OpenCV示例还具有其他功能,如眼睛检测、嘴巴检测等。试试看。与其评论,不如作为一个新问题提问。这个问题与这个问题有关,我不确定它是否可以作为另一个问题提问。我只是想把眼睛像脸一样提取出来,就像你在代码中做的那样。我已经添加了眼睛提取代码,请查看是否有任何错误,因为我没有得到眼睛。好吧,您所说的错误是由于错误检测到眼睛(I>1)。因此,为了避免程序意外终止,在
i=i+1
行之后设置一个条件,即if i==2:break
或if i>1:break
。因此,我们仅限于前两个检测到的对象(即眼睛)。但问题是我们不能确定前两个被检测到的是眼睛。也许,第一个是假的,第二个,第三个是眼睛。这里不要问,如何将眼睛从错误检测中隔离出来。这是不同的话题,问不同的问题。在此之前,上述解决方案已足够。。