2015-01-21 39 views
2

我試圖在Python中製作一個結合了Haar Cascade Classification和Lucas Kanade的面部檢測程序。但是我得到錯誤說是這樣的:OpenCV Python:在GoodFeatureToDetect中使用Mask參數出錯

錯誤:

Traceback (most recent call last): 
    File "/home/anthony/Documents/Programming/Python/Computer-Vision/OpenCV-Doc/optical-flow-and-haar-detection-test.py", line 80, in <module> 
    corners_t = cv2.goodFeaturesToTrack(gray, mask = mask_use, **feature_params) 
error: /build/buildd/opencv-2.4.8+dfsg1/modules/imgproc/src/featureselect.cpp:63: error: (-215) mask.empty() || (mask.type() == CV_8UC1 && mask.size() == image.size()) in function goodFeaturesToTrack 

我的程序是如何工作的:

我的程序使用哈爾級聯得到一個檢測面部的座標,複製無論是在由座標(在這種情況下爲臉部)創建的區域,以除黑色之外的其他圖像(通過numpy將所有像素設置爲零)拍攝圖像,並將複製的臉部粘貼到黑色背景中。通過將具有黑色背景的新臉部設置爲遮罩參數,這將強制Lucas Kanade(goodFeaturesToDetect)在臉部上創建特徵點,該特徵點將通過光流跟蹤。

代碼:

from matplotlib import pyplot as plt 
import numpy as np 

import cv2 

rectangle_x = 0 


face_classifier = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml') 


#cap = cv2.VideoCapture('video/sample.mov') 
cap = cv2.VideoCapture(0) 


# params for ShiTomasi corner detection 
feature_params = dict(maxCorners = 200, 
         qualityLevel = 0.01, 
         minDistance = 10, 
         blockSize = 7) 

# Parameters for lucas kanade optical flow 
lk_params = dict(winSize = (15,15), 
        maxLevel = 2, 
        criteria = (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10, 0.03)) 

# Create some random colors 
color = np.random.randint(0,255,(100,3)) 

# Take first frame and find corners in it 
ret, old_frame = cap.read() 

#old_frame = cv2.imread('images/webcam-first-frame-two.png') 

######Adding my code### 
cv2.imshow('Old_Frame', old_frame) 
cv2.waitKey(0) 
old_gray = cv2.cvtColor(old_frame, cv2.COLOR_BGR2GRAY) 
restart = True 
#while restart == True: 
face = face_classifier.detectMultiScale(old_gray, 1.2, 4) 

if len(face) == 0: 
    print "This is empty" 

for (x,y,w,h) in face: 
    focused_face = old_frame[y: y+h, x: x+w] 
    #cv2.rectangle(old_frame, (x,y), (x+w, y+h), (0,255,0),2) 

#initalize all pixels to zero (picture completely black) 
mask_use = np.zeros(old_frame.shape,np.uint8) 

#Crop old_frame coordinates and paste it on the black mask) 
mask_use[y:y+h,x:x+w] = old_frame[y:y+h,x:x+w] 

height, width, depth = mask_use.shape 
print "Height: ", height 
print "Width: ", width 
print "Depth: ", depth 


height, width, depth = old_frame.shape 
print "Height: ", height 
print "Width: ", width 
print "Depth: ", depth 

cv2.imshow('Stuff', mask_use) 

cv2.imshow('Old_Frame', old_frame) 
#cv2.imshow('Zoom in', focused_face) 

face_gray = cv2.cvtColor(old_frame,cv2.COLOR_BGR2GRAY) 

gray = cv2.cvtColor(focused_face,cv2.COLOR_BGR2GRAY) 



corners_t = cv2.goodFeaturesToTrack(gray, mask = mask_use, **feature_params) 
corners = np.int0(corners_t) 

#print corners 



for i in corners: 
    ix,iy = i.ravel() 
    cv2.circle(focused_face,(ix,iy),3,255,-1) 
    cv2.circle(old_frame,(x+ix,y+iy),3,255,-1) 

    print ix, " ", iy 

plt.imshow(old_frame),plt.show() 
""" 
print "X: ", x 
print "Y: ", y 
print "W: ", w 
print "H: ", h 
#face_array = [x,y,w,h] 
""" 

############################# 
p0 = cv2.goodFeaturesToTrack(old_gray, mask = old_gray, **feature_params) 
############################# 
# Create a mask image for drawing purposes 
mask = np.zeros_like(old_frame) 

while(1): 
    ret,frame = cap.read() 
    frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 

    # calculate optical flow 
    p1, st, err = cv2.calcOpticalFlowPyrLK(old_gray, frame_gray, p0, None, **lk_params) 

    # Select good points 
    good_new = p1[st==1] 
    ###print "Good_New" 
    ###print good_new 
    good_old = p0[st==1] 

    # draw the tracks 
    for i,(new,old) in enumerate(zip(good_new,good_old)): 
     #print i 
     #print color[i] 
     a,b = new.ravel() 
     c,d = old.ravel() 
     cv2.line(mask, (a,b),(c,d), color[i].tolist(), 2) 
     cv2.circle(frame,(a, b),5,color[i].tolist(),-1) 
     if i == 99: 
      break 
     #For circle, maybe replace (a,b) with (c,d)? 
    #img = cv2.add(frame,mask) 

    cv2.imshow('frame',frame) 
    k = cv2.waitKey(30) & 0xff 
    if k == 27: 
     break 

    # Now update the previous frame and previous points 
    old_gray = frame_gray.copy() 
    p0 = good_new.reshape(-1,1,2) 





cv2.destroyAllWindows() 
cap.release() 

任何人都可以看到的問題,並告訴我如何解決它?謝謝。

回答

1

我有使用不同大小的數組引起此錯誤。

您有一個for循環,動態地將值賦給focused_face,但追蹤的good_features使用靜態大小(=到focused_face的最後一個實例)。 Old_frame看起來像使用focused_face的第一個實例的形狀。

確保您在goodFeaturesToTrack中使用相同形狀的圖像和遮罩陣列。