我正在使用汽車級聯來檢測示例視頻中的所有汽車。該程序目前正在繪製檢測到的每輛車周圍的矩形。但是,矩形在幀間不斷變化。如果下一幀中的新矩形與前一個矩形重疊,我想通過保留原始矩形來增加穩定性。爲了實現這一點,我保存了前一幀(並檢測前一幀的汽車),並將前一幀的矩形與當前幀進行比較。從視頻獲取前一幀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
爲什麼你需要矢量爲汽車和prevCars ????,一個是最好的確定..... –
我有兩個,因爲我要使用for循環遍歷所有保存的汽車在汽車中,並將其與prevCars中的汽車進行比較,以檢查兩個矩形是否正在綁定。 –