frame_binary = frame_gray.clone();
threshold(frame_gray, frame_binary, 128.0, 255.0, THRESH_BINARY);
frame_old_binary = frame_old.clone();
threshold(frame_old, frame_old_binary, 128.0, 255.0, THRESH_BINARY);
frame_sub = frame_binary - frame_old_binary;
BackgroundSubtractorMOG2 backsub;
// and for each frame:
fgmask = frame.clone();
backsub(frame, fgmask, 0.1);
backsub.getBackgroundImage(bg);
…by finding contours, then bounding boxes
vector<Rect> boxes;
vector<vector<Point> > contours;
Mat fgmask_contours = fgmask.clone();
findContours(fgmask_contours, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
vector<vector<Point> >::const_iterator it = contours.begin();
while(it != contours.end()) {
Rect box = boundingRect(Mat(*it));
if(box.width > 5 && box.height > 5) {
boxes.push_back(box);
}
++it;
}
// visualize the contours
drawContours(frame, contours, -1, Scalar(255), 1);
Kalman filter?