2017-04-27 70 views
2

我正在使用汽車級聯來檢測示例視頻中的所有汽車。該程序目前正在繪製檢測到的每輛車周圍的矩形。但是,矩形在幀間不斷變化。如果下一幀中的新矩形與前一個矩形重疊,我想通過保留原始矩形來增加穩定性。爲了實現這一點,我保存了前一幀(並檢測前一幀的汽車),並將前一幀的矩形與當前幀進行比較。從視頻獲取前一幀Opencv Haar Cascade

Mat frame; 
Mat prevFrame; 

while (capture.isOpened()) { 
    capture.read(frame); 
    vector<Rect> cars; // center of rectangles where each rectangle contains the detected object 
    vector<Rect> prevCars; // to store previous tracked rectangles 

    // Detects objects of different sizes in the input image. The detected objects are returned as a list of rectangles. 
    car_cascade.detectMultiScale(frame, cars, 1.1, 2); 

    if(!prevFrame.empty()) { 
     car_cascade.detectMultiScale(prevFrame, prevCars, 1.1, 2); 
    } else { 
     cout << "EMPTY" << endl; // for testing 
    } 

    cout << "current : " << cars.size() << endl; // print out number of cars 
    cout << "previous: " << prevCars.size() << endl; // print out number of cars 


    // more code goes here which I haven't written here 

    frame.copyTo(prevFrame); // set previous frame to current frame 
    imshow("Video", frame); 

    char key = waitKey(33); 
    if (key == 'q') 
    { 
     break; 
    } 
} 

但是,從上一幀檢測到的汽車數量與以前的電流不同。例如,

EMPTY 電流:3 以前:0 < - 0,因爲它是空 電流:3 前面:2 < - 以前是2,而應該是3,因爲之前的電流爲3 電流:3 前面:2

+0

爲什麼你需要矢量爲汽車和prevCars ????,一個是最好的確定..... –

+0

我有兩個,因爲我要使用for循環遍歷所有保存的汽車在汽車中,並將其與prevCars中的汽車進行比較,以檢查兩個矩形是否正在綁定。 –

回答

1

爲了跟蹤和更新汽車的矩形,這裏是我會做什麼(蟒蛇一樣的代碼):

def getIndexOfCorrespondingTrackedCar(car) : 
    for i in range(0, len(tracked_cars)) : 
     if distance(car.center, tracked_cars[i].center) < THRESHOLD : // you will have to define a threshold according to your case. It has to be smaller than the speed of cars (in px/frame) though. 
      return(i) // we found the corresponding car in the tracked_cars list 
    return(-1) // we found no corresponding car, it must be a new car 

tracked_cars = [] // list of tracked Rects 
cars_current_frame = [] // list of Rects on the current frame 

while(camera.open()) : 

    cars_current_frame = getCarsInFrame(frame) // here you want to use your detectMultiScale function 

    for ccf in cars_current_frame : 
     car_idx = getIndexOfCorrespondingTrackedCar(ccf) // get the index of the corresponding car in tracked_cars list 
     if car_idx is -1 : // No correspondance has been found, this is a new car 
      tracked_cars.append(ccf) 
     else : 
      tracked_cars[car_idx] = ccf // we update the position of the tracked car with its new position 

    deleteOutdatedCars(tracked_cars) // delete all the tracked cars that haven't been updated for a certain time (it most probably means that the car went off of the frame) 

在我的例子,我只能用重新名單cts,但在這裏使用對象會更方便。我建議你創建一個汽車類具有以下成員變量:

  • 一個矩形跟蹤汽車的位置
  • (這將需要的最後更新的時間戳刪除了「過時」的汽車)
  • 一個速度矢量近似轎廂的下一個幀上(可選)

我使用的位置d與手部跟蹤系統類似的方法,它運行良好。

+0

我在實際看到它之前已實際使用它(有點)。非常感謝您的簡潔回答:) –