gpt4 book ai didi

python - 将检测到的人脸放入图像中并使用python在opencv的另一个窗口中显示

转载 作者:太空宇宙 更新时间:2023-11-03 21:09:38 25 4
gpt4 key购买 nike

我是图像处理和 opencv 的新手,但到目前为止,易于理解的函数和良好的文档使我能够尝试和理解一些级别的代码,如面部检测等。

现在,当我在网络摄像头视频流中检测到人脸时,程序会在人脸周围绘制一个正方形。现在我想要图像的那么大区域,在脸部周围的正方形中,被创建为另一个图像。从我一直在做的事情来看,我得到了一个矩形区域,其中甚至没有人脸。

我使用过 cv.GetSubRect() 并了解它的用途。例如:

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)

确保没有错误检测或多重检测。

关于python - 将检测到的人脸放入图像中并使用python在opencv的另一个窗口中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8954981/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com