1 | initial version |
vector<vector<point> > contours; vector<vec4i> hierarchy; vector<char> fillCtr;
Mat c;
void DrawTree( int idx, int level)
{
int i=idx;
if (level%2==0)
drawContours(c, contours, i, Scalar(255 , 0,0),CV_FILLED);
else
drawContours(c, contours, i, Scalar(0, 255 ,0),CV_FILLED);
fillCtr[i]=1;
while (hierarchy[i][0]!=-1)
{
int j=hierarchy[i][0];
if (fillCtr[j] == 0)
{
if (level%2==0)
drawContours(c, contours, j, Scalar(255 , 0,0),CV_FILLED);
else
drawContours(c, contours, j, Scalar(0, 255 ,0),CV_FILLED);
fillCtr[j]=1;
DrawTree(j,level);
}
i=hierarchy[i][0];
}
if (hierarchy[idx][2]!=-1)
DrawTree(hierarchy[idx][2],level+1);
}
int main (int argc,char **argv) { { Mat x = imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/14415468805620458.jpg",CV_LOAD_IMAGE_GRAYSCALE); //Mat x = imread("f:/IMAGES/pic10.png",CV_LOAD_IMAGE_GRAYSCALE); imshow("original",x); Mat y; threshold(x,y,50,255,THRESH_BINARY); imshow("threshold",y); Mat yc; findContours(y,contours,hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE); c =Mat::zeros(x.size(),CV_8UC3); vector<mat> plan = {x,x,x}; merge(plan,c); cout << c.channels() << "\n"; // drawContours(c, contours, -1, Scalar(0,0,255),3); fillCtr.resize(contours.size()); for( int i=0;i <contours.size();i++) {="" if="" (hierarchy[i][3]="=" -1="" &&="" fillctr[i]="=0)" {="" drawtree(i,0);="" }="" }<="" p="">
imshow("contour",c);
waitKey(0);
}
2 | No.2 Revision |
with this program color are blue or green as funtion level in hierarchy (and not if contour have got a son or not)
vector<vector<point> > contours;
vector<vec4i> hierarchy;
vector<char> fillCtr;fillCtr;
Mat c;
Mat c;
void DrawTree( int idx, int level)
{
int i=idx;
if (level%2==0)
drawContours(c, contours, i, Scalar(255 , 0,0),CV_FILLED);
else
drawContours(c, contours, i, Scalar(0, 255 ,0),CV_FILLED);
fillCtr[i]=1;
while (hierarchy[i][0]!=-1)
{
int j=hierarchy[i][0];
if (fillCtr[j] == 0)
{
if (level%2==0)
drawContours(c, contours, j, Scalar(255 , 0,0),CV_FILLED);
else
drawContours(c, contours, j, Scalar(0, 255 ,0),CV_FILLED);
fillCtr[j]=1;
DrawTree(j,level);
}
i=hierarchy[i][0];
}
if (hierarchy[idx][2]!=-1)
DrawTree(hierarchy[idx][2],level+1);
}
int main (int argc,char **argv)
{
{
{
Mat x = imread("C:/Users/Laurent.PC-LAURENT-VISI/Downloads/14415468805620458.jpg",CV_LOAD_IMAGE_GRAYSCALE);
//Mat x = imread("f:/IMAGES/pic10.png",CV_LOAD_IMAGE_GRAYSCALE);
imread("14415468805620458.jpg",CV_LOAD_IMAGE_GRAYSCALE);
imshow("original",x);
Mat y;
threshold(x,y,50,255,THRESH_BINARY);
imshow("threshold",y);
Mat yc;
findContours(y,contours,hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
c =Mat::zeros(x.size(),CV_8UC3);
vector<mat> vector<Mat> plan = {x,x,x};
merge(plan,c);
cout << c.channels() << "\n";
// drawContours(c, contours, -1, Scalar(0,0,255),3);
fillCtr.resize(contours.size());
for( int i=0;i <contours.size();i++) {="" if="" (hierarchy[i][3]="=" -1="" &&="" fillctr[i]="=0)" {="" drawtree(i,0);="" }="" }<="" p="">
<contours.size();i++)
{
if (hierarchy[i][3] == -1 && fillCtr[i]==0)
{
DrawTree(i,0);
}
}
imshow("contour",c);
waitKey(0);
}