opencv - How to getting a SOLID blob for each object using findcontours? -
i wanna segementing solid blobs each object extracted foreground , bounding each object box. code show many boxes bounding random blobs on 1 object, because blob not solid 1 object , there're many small blobs too.
here go code:
#include"stdafx.h" #include<vector> #include<iostream> #include<opencv2/opencv.hpp> #include<opencv2/core/core.hpp> #include<opencv2/imgproc/imgproc.hpp> #include<opencv2/highgui/highgui.hpp> int main(int argc, char *argv[]) { cv::mat frame; cv::mat fg; cv::mat thresholded; cv::mat thresholded2; cv::mat result; cv::mat bgmodel; cv::namedwindow("frame"); cv::namedwindow("background model"); cv::videocapture cap(0); cv::backgroundsubtractormog2 bgs; bgs.nmixtures = 2; bgs.history = 60; bgs.varthreshold = 15; bgs.bshadowdetection = true; bgs.nshadowdetection = 0; bgs.ftau = 0.5; std::vector<std::vector<cv::point>> contours; for(;;) { cap >> frame; cv::blur(frame,frame,cv::size(10,10)); bgs.operator()(frame,fg); bgs.getbackgroundimage(bgmodel); cv::erode(fg,fg,cv::mat()); cv::dilate(fg,fg,cv::mat()); cv::threshold(fg,thresholded,70.0f,255,cv_thresh_binary); cv::threshold(fg,thresholded2,70.0f,255,cv_thresh_binary); cv::findcontours(thresholded,contours,cv_retr_external,cv_chain_approx_none); cv::cvtcolor(thresholded2,result,cv_gray2rgb); int cmin= 50; int cmax= 10000; std::vector<std::vector<cv::point>>::iterator itc=contours.begin(); while (itc!=contours.end()) { if (itc->size() < cmin || itc->size() > cmax){ itc= contours.erase(itc);} else{ std::vector<cv::point> pts = *itc; cv::mat pointsmatrix = cv::mat(pts); cv::scalar color( 0, 255, 0 ); cv::rect r0= cv::boundingrect(pointsmatrix); cv::rectangle(result,r0,color,2); ++itc; } } cv::imshow("frame",result); cv::imshow("background model",bgmodel); if(cv::waitkey(30) >= 0) break; } return 0; }
and result here: frame
so how can segmenting solid blob each object found extracted foreground, , bounding object on 1 box?
a solid blob mean solid white blob here: xxx
i'll apreciating here.
nb: sorry bad english. :)
=================
this edited code!
#include"stdafx.h" #include<vector> #include<iostream> #include<opencv2/opencv.hpp> #include<opencv2/core/core.hpp> #include<opencv2/imgproc/imgproc.hpp> #include<opencv2/highgui/highgui.hpp> int main(int argc, char *argv[]) { cv::mat frame; cv::mat fg; cv::mat blurred; cv::mat thresholded; cv::mat thresholded2; cv::mat result; cv::mat bgmodel; cv::namedwindow("frame"); cv::namedwindow("background model"); cv::videocapture cap(0); cv::backgroundsubtractormog2 bgs; bgs.nmixtures = 2; bgs.history = 60; bgs.varthreshold = 15; bgs.bshadowdetection = true; bgs.nshadowdetection = 0; bgs.ftau = 0.5; std::vector<std::vector<cv::point>> contours; for(;;) { cap >> frame; cv::blur(frame,blurred,cv::size(10,10)); bgs.operator()(blurred,fg); bgs.getbackgroundimage(bgmodel); cv::threshold(fg,thresholded,70.0f,255,cv_thresh_binary); cv::threshold(fg,thresholded2,70.0f,255,cv_thresh_binary); cv::mat element50(50,50,cv_8u,cv::scalar(1)); cv::morphologyex(thresholded,thresholded,cv::morph_close,element50); cv::morphologyex(thresholded2,thresholded2,cv::morph_close,element50); cv::findcontours(thresholded,contours,cv_retr_external,cv_chain_approx_simple); cv::cvtcolor(thresholded2,result,cv_gray2rgb); int cmin= 50; int cmax= 10000; std::vector<std::vector<cv::point>>::iterator itc=contours.begin(); while (itc!=contours.end()) { if (itc->size() < cmin || itc->size() > cmax){ itc= contours.erase(itc);} else{ std::vector<cv::point> pts = *itc; cv::mat pointsmatrix = cv::mat(pts); cv::scalar color( 0, 255, 0 ); cv::rect r0= cv::boundingrect(pointsmatrix); cv::rectangle(result,r0,color,2); ++itc; } } cv::imshow("frame",result); cv::imshow("background model",bgmodel); if(cv::waitkey(30) >= 0) break; } return 0; }
and result here: frame
thanks elactic. :)
you can try merge blogs morphological closing (which erosion of dilation of binary image). can use cv functions erode
, dilate
that.
this tutorial should you. assume still have filter blobs size after that.
Comments
Post a Comment