opencv - Bounding a foreground object -


i want bound foreground object rectangle, how shall go doing it?

i tried bounding rectangle collecting white pixels , bounds whole screen...

how shall solve problem?

enter image description here

//defined later vector<point> points; rng rng(12345);  int main(int argc, char* argv[]) {     //create gui windows     namedwindow("frame");     namedwindow("fg mask mog 2");     pmog2 = createbackgroundsubtractormog2();      videocapture capture(0);         //update background model         pmog2->apply(frame, fgmaskmog2);         frame_check = fgmaskmog2.clone();         erode(frame_check, frame_check, getstructuringelement(morph_ellipse, size(5, 5)));         dilate(frame_check, frame_check, getstructuringelement(morph_ellipse, size(5, 5)));          vector<vector<point> > contours;         vector<vec4i> hierarchy;          /// detect edges using threshold         //threshold(frame_check, frame_check, 100, 255, thresh_binary);         //find contours         findcontours(frame_check, contours, hierarchy, cv_retr_tree, cv_chain_approx_simple, point(0, 0));          //points         (size_t = 0; < contours.size(); i++) {             (size_t j = 0; j < contours[i].size(); j++) {                 if (contours[i].size() > 2000)                 {                     cv::point p = contours[i][j];                     points.push_back(p);                 }             }         }                if (points.size() > 0){                 rect brect = cv::boundingrect(cv::mat(points).reshape(2));                 rectangle(frame, brect.tl(), brect.br(), cv_rgb(0, 255, 0), 2, cv_aa);                 cout << points.size() << endl;              }             imshow("frame", frame);             imshow("fg mask mog 2", fgmaskmog2);             //get input keyboard             keyboard = waitkey(1000);         }     } 

try this, it's project working on. isolate objects use color masks, produce binary images, in case proper threshold on foreground image (after filtering) can achieve same result.

// morphological opening erode(binary, binary, getstructuringelement(morph_ellipse, filtersize)); dilate(binary, binary, getstructuringelement(morph_ellipse, filtersize));  // morphological closing dilate(binary, binary, getstructuringelement(morph_ellipse, filtersize)); erode(binary, binary, getstructuringelement(morph_ellipse, filtersize));  // find contours vector<vector<point>> contours; findcontours(binary, contours, cv_retr_external, cv_chain_approx_simple);  vector<rect> rectangles; for(auto& contour : contours) {     // produce closed polygon object's contours     vector<point> polygon;     approxpolydp(contour, polygon, 3, true);      // polygon's bounding rectangles     rect rect = boundingrect(polygon);     rectangles.push_back(rect); } 

Comments