2017-05-17 77 views
1

我試圖覆蓋襯衫上的圖像使用OpenCV Python的自己的實時視頻流。因爲三天,我又卡在這個特殊的錯誤:OpenCV Python錯誤:錯誤:(-215)(mtype == CV_8U || mtype == CV_8S)&& _mask.sameSize(* psrc1)in function cv :: binary_op

錯誤:(-215)(MTYPE == CV_8U || MTYPE == CV_8S)& & _mask.sameSize(* PSRC1)函數CV :: binary_op

發生在這條線

此錯誤:

roi_bg = cv2.bitwise_and(ROI,ROI,掩模= mask_inv)

我的代碼:

import cv2        # Library for image processing 
import numpy as np 
imgshirt = cv2.imread('C:/Users/sayyed javed ahmed/Desktop/Humaira/Images For Programs/aureknayashirt.png',1) #original img in bgr 
musgray = cv2.cvtColor(imgshirt,cv2.COLOR_BGR2GRAY) #grayscale conversion 
ret, orig_mask = cv2.threshold(musgray,150 , 255, cv2.THRESH_BINARY) 
orig_mask_inv = cv2.bitwise_not(orig_mask) 
origshirtHeight, origshirtWidth = imgshirt.shape[:2] 
face_cascade=cv2.CascadeClassifier('C:\Users\sayyed javed ahmed\Desktop\Humaira\haarcascade_frontalface_default.xml') 
cap=cv2.VideoCapture(0) 
while True: 
    ret,img=cap.read() 
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
    faces=face_cascade.detectMultiScale(gray,1.3,5) 
    for (x,y,w,h) in faces: 
     cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) 
     shirtWidth = 3 * w #approx wrt face width 
     shirtHeight = shirtWidth * origshirtHeight/origshirtWidth #preserving aspect ratio of original image.. 

     # Center the shirt..just random calculations.. 
     x1 = x-w 
     x2 =x1+3*w 
     y1 = y+h 
     y2 = y1+h*2 


     # Check for clipping(whetehr x1 is coming out to be negative or not..) 

     if x1 < 0: 
      x1 = 0 
     if y1 < 0: 
      y1 = 0 
     if x2 > 4*w: 
      x2 =4*w 
     if y2 > 2* h: 
      y2 = x2* origshirtHeight/origshirtWidth 

     print x1 #debugging 
     print x2 
     print y1 
     print y2 
     print w 
     print h 
     # Re-calculate the width and height of the shirt image(to resize the image when it wud be pasted) 
     shirtWidth = x2 - x1 
     shirtHeight = y2 - y1 

     # Re-size the original image and the masks to the shirt sizes 

     shirt = cv2.resize(imgshirt, (shirtWidth,shirtHeight), interpolation = cv2.INTER_AREA) #resize all,the masks you made,the originla image,everything 
     mask = cv2.resize(orig_mask, (shirtWidth,shirtHeight), interpolation = cv2.INTER_AREA) 
     mask_inv = cv2.resize(orig_mask_inv, (shirtWidth,shirtHeight), interpolation = cv2.INTER_AREA) 

     # take ROI for shirt from background equal to size of shirt image 
     roi = img[y1:y2, x1:x2] 

     print shirt.size #debugginh 
     print mask.size 
     print mask_inv.size 
     print roi.size 

     print shirt.shape 
     print roi.shape 

     print mask.shape 
     print mask_inv.shape 


      # roi_bg contains the original image only where the shirt is not 
      # in the region that is the size of the shirt. 
     roi_bg = cv2.bitwise_and(roi,roi,mask = mask_inv) 

      # roi_fg contains the image of the shirt only where the shirt is 
     roi_fg = cv2.bitwise_and(shirt,shirt,mask = mask) 
     print roi_bg.shape #debugging 
     print roi_fg.shape 

      # join the roi_bg and roi_fg 
     dst = cv2.add(roi_bg,roi_fg) 
     print dst.shape 
      # place the joined image, saved to dst back over the original image 
     roi = dst 


     break 
    cv2.imshow('img',img) 
    if cv2.waitKey(1) == ord('q'): 
     break; 
cap.release()       # Destroys the cap object 
cv2.destroyAllWindows()     # Destroys all the windows created by imshow 

我讀過這個帖子: http://www.stackoverflow.com/questions/30117740/opencv-error-assertion-failed-mask-size-src1-size-in-binary-op 但是沒有太多掌握。我知道roi和襯衫的尺寸應該是一樣的,我打印這些值是爲了檢查它們是否相同,但不是。根據我的陳述:

ROI = IMG [Y1:Y2,X1:X2]

襯衫= cv2.resize(imgshirt,(shirtWidth,shirtHeight),插值= cv2.INTER_AREA )

應使它們的大小爲x2-x1和y2-y1,但這並未發生。自從三天以來,我一直在摸索着這一行,任何幫助都是值得讚賞的!

+0

會[這裏](https://stackoverflow.com/q/34466597/3491991)幫助呢? – zelusp

回答

0

你有沒有做出肯定的疊加圖像不會在前景尺寸去,因爲這通常是什麼原因造成的面具大小以不同

1

錯誤很可能從那裏你有變量x和y撥弄來了而不確定它們是否適合攝像頭饋送的框架。

我返工你的代碼工作之一:

import cv2 
import numpy as np 
imgshirt = cv2.imread('shirt.png',1) 
musgray = cv2.cvtColor(imgshirt,cv2.COLOR_BGR2GRAY) #grayscale conversion 
ret, orig_mask = cv2.threshold(musgray,150 , 255, cv2.THRESH_BINARY) 
orig_mask_inv = cv2.bitwise_not(orig_mask) 
origshirtHeight, origshirtWidth = imgshirt.shape[:2] 
face_cascade=cv2.CascadeClassifier('haarcascade_frontalface_default.xml') 
cap=cv2.VideoCapture(0) 
ret,img=cap.read() 
img_h, img_w = img.shape[:2] 
while True: 
    ret,img=cap.read() 
    gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) 
    faces=face_cascade.detectMultiScale(gray,1.3,5) 
    for (x,y,w,h) in faces: 
     cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) 

     face_w = w 
     face_h = h 
     face_x1 = x 
     face_x2 = face_x1 + face_h 
     face_y1 = y 
     face_y2 = face_y1 + face_h 

     # set the shirt size in relation to tracked face 
     shirtWidth = 3 * face_w 
     shirtHeight = int(shirtWidth * origshirtHeight/origshirtWidth) 


     shirt_x1 = face_x2 - int(face_w/2) - int(shirtWidth/2) #setting shirt centered wrt recognized face 
     shirt_x2 = shirt_x1 + shirtWidth 
     shirt_y1 = face_y2 + 5 # some padding between face and upper shirt. Depends on the shirt img 
     shirt_y2 = shirt_y1 + shirtHeight 

     # Check for clipping 
     if shirt_x1 < 0: 
      shirt_x1 = 0 
     if shirt_y1 < 0: 
      shirt_y1 = 0 
     if shirt_x2 > img_w: 
      shirt_x2 = img_w 
     if shirt_y2 > img_h: 
      shirt_y2 = img_h 

     shirtWidth = shirt_x2 - shirt_x1 
     shirtHeight = shirt_y2 - shirt_y1 
     if shirtWidth < 0 or shirtHeight < 0: 
      continue 

     # Re-size the original image and the masks to the shirt sizes 
     shirt = cv2.resize(imgshirt, (shirtWidth,shirtHeight), interpolation = cv2.INTER_AREA) #resize all,the masks you made,the originla image,everything 
     mask = cv2.resize(orig_mask, (shirtWidth,shirtHeight), interpolation = cv2.INTER_AREA) 
     mask_inv = cv2.resize(orig_mask_inv, (shirtWidth,shirtHeight), interpolation = cv2.INTER_AREA) 

     # take ROI for shirt from background equal to size of shirt image 
     roi = img[shirt_y1:shirt_y2, shirt_x1:shirt_x2] 


     # roi_bg contains the original image only where the shirt is not 
     # in the region that is the size of the shirt. 
     roi_bg = cv2.bitwise_and(roi,roi,mask = mask) 
     roi_fg = cv2.bitwise_and(shirt,shirt,mask = mask_inv) 
     dst = cv2.add(roi_bg,roi_fg) 
     img[shirt_y1:shirt_y2, shirt_x1:shirt_x2] = dst 


     break 
    cv2.imshow('img',img) 
    if cv2.waitKey(1) == ord('q'): 
     break; 
cap.release() # Destroys the cap object 
cv2.destroyAllWindows() # Destroys all the windows created by imshow 

我改名變量,使他們更容易把握。我將襯衫圖像和haarcascade XML路徑設置爲工作目錄以進行本地測試。創建roi bg和fg時,我也換了口罩,不完全確定爲什麼這是必要的,但是這給了正確的結果。最後加入img[shirt_y1:shirt_y2, shirt_x1:shirt_x2] = dst實際上將襯衫粘貼到視頻框架中。

另一件需要注意的事情是,當處理numpy圖像時,總是將任何分割結果轉換爲int s。

face recognition with shirt webcam capture

相關問題