如何在opencv中裁剪检测到的面部并将roi保存为opencv python中的图像 [英] how to crop the detected face in opencv and save roi as image in opencv python

查看:1027
本文介绍了如何在opencv中裁剪检测到的面部并将roi保存为opencv python中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python中使用opencv,这是我检测脸部和保存脸部的代码..但它没有保存roi(检测到脸部),我一直在做这个问题。请帮助我如何解决这个问题。

Im using opencv in python and this is my code in detecting the face and saving the face..but it does not save the roi(the face detected),i've been having trouble doing this.please help me how to fix this.

   TRAINSET = "data/lbpcascades/lbpcascade_frontalface.xml"
   DOWNSCALE = 4

   cam = cv2.VideoCapture(0) #capture a video

   cv2.namedWindow("preview")
   classifier = cv2.CascadeClassifier(TRAINSET) 

   Compare_images=[]
   for file in os.listdir("images"):
       if file.endswith(".jpg"):
          Compare_images.append(file)
while True: # try to get the first frame
    _, frame = cam.read() 

key = cv2.waitKey(20)
if(key==32):

    print "Name of Image:"

    n= raw_input()

    value=len(Compare_images)
    cv2.imwrite('images/image'+str(n)+'.jpg', frame)
    saved_image=cv2.imread("images/image"+str(n)+".jpg")
    minisize = (saved_image.shape[1]/DOWNSCALE,saved_image.shape[0]/DOWNSCALE)
    miniframe = cv2.resize(saved_image, minisize)
    faces = classifier.detectMultiScale(miniframe)
    for f in faces:
        x, y, w, h = [ v*DOWNSCALE for v in f ]     
        print x 
        print y,w,h      

        x0,y0=int(x),int(y)
        x1,y1=int(x+w),int(y+h)
        print x0,y0,y1,y0

        image = cv2.rectangle(saved_image, (x0,y0), (x1,y1), (0,0,255),2)

        roi=saved_image[y0:y1,x1:x0]#crop 
        cv2.imwrite('roi.jpg',roi)
        cv2.imshow("adsa", saved_image) 


cv2.putText(frame, "Press ESC to close.", (5, 25),
            cv2.FONT_HERSHEY_SIMPLEX, 1.0, (255,255,255))
cv2.imshow("preview", frame)


推荐答案

您的意思是?:

.
.
.
print x0,y0,x1,y1
.
.
.
roi=saved_image[y0:y1,x0:x1]

上下压痕语句似乎不正确。

The indentation above and below the while statement seems incorrect.

三重引号只应临时用于块引号,因为它们可能会导致问题。

Triple quotes should only be used temporarily for block quotes as they can cause problems.

也许使用代替:

#x0,y0=x,y
#x1,y1=x+w,y+h

除非这就是假设该函数的帮助如何阅读。

Unless that is how the help for that function is suppose to read.

在你的问题中包含错误也会有所帮助。

Including errors in your question would be helpful too.

这篇关于如何在opencv中裁剪检测到的面部并将roi保存为opencv python中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆