簡體   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