繁体   English   中英

如何在opencv中裁剪检测到的脸部并将ROI保存为opencv python中的图像

[英]how to crop the detected face in opencv and save roi as image in opencv python

我在python中使用opencv,这是我检测面部并保存面部的代码..但是它不保存roi(检测到的面部),我在执行此操作时遇到了麻烦。请帮助我解决此问题。

   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]

while语句上方和下方的缩进似乎不正确。

三重引号仅应暂时用于块引号,因为它们可能会引起问题。

也许使用#代替:

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

除非那样,否则应该阅读该功能的帮助。

在您的问题中包括错误也将有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM