【问题标题】:putting the detected face into an image and display in another window in opencv using python使用python将检测到的人脸放入图像并显示在opencv的另一个窗口中
【发布时间】:2012-02-15 19:40:11
【问题描述】:

我是图像处理和 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)

【问题讨论】:

    标签: python opencv face-detection


    【解决方案1】:

    我希望这段代码来自 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 示例还具有其他功能,例如眼睛检测、嘴巴检测等。试试看。而不是 cmets,最好作为一个新问题提出。
    • 问题与这个问题有关,我不确定它是否可以作为另一个问题提出。我只想像提取脸部一样提取眼睛,就像您在代码中所做的那样。我已经附加了眼睛提取代码,请看看是否有任何错误,因为我没有得到眼睛。
    • 嗯,你告诉的错误是由于眼睛的错误检测(i>1)。因此,为避免程序意外终止,请在i=i+1 行之后设置if i==2:breakif i>1:break 条件。所以我们将自己限制在前两个检测到的对象(即眼睛)上。但问题是我们不能确定前两个检测到的是眼睛。可能,第一个是假的,第二个,第三个是眼睛。不要在这里问,如何将眼睛与错误检测隔离开来。那是不同的主题并提出单独的问题。在此之前,上述解决方案就足够了..
    猜你喜欢
    • 2016-01-29
    • 2019-12-01
    • 2012-03-08
    • 2015-12-18
    • 1970-01-01
    • 2014-05-11
    • 1970-01-01
    • 2017-09-04
    • 2013-02-19
    相关资源
    最近更新 更多