2017-04-27 6 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追跡および更新車のRectをするために2

+0

なぜ車とprevCarsの両方にベクトルが必要ですか?1つはほとんどOKです..... –

+0

forループを使用してすべての車を繰り返し保存するため2つあります車に乗って、prevCarsの車と比較して、両方の長方形が境界になっているかどうかを確認してください。 –

答えて

1

は、ここでは(コードのようなのpython)私はどうなるのかです::前

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) 

私の例では、私だけのReリストを使用しますctsですが、ここでオブジェクトを使用する方が便利です。私は、あなたが以下のメンバ変数とCarクラスを作成することをお勧めします:

  • のRectは、車の位置を追跡するために
  • 最終更新の タイムスタンプは(これは削除するために必要です
  • 「時代遅れ」車)
  • 次のフレーム(オプション)私が使用

上の車の位置を近似する速度ベクトルハンドトラッキングシステムを用いた同様のアプローチで、うまくいきました。

+0

これを見る前に、実際には(多少)動作させてしまいました。とにかくあなたの簡潔な答えをありがとう:) –

関連する問題