Ask Your Question

Revision history [back]

Do you think that's a good way to solve this problem ?

#include "opencv2/highgui/highgui.hpp" #include <iostream> #include <fstream> #include <string> #include <sstream>

using namespace cv; 
using namespace std;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<char> fillCtr;

Mat c;

void DrawTree( int idx, int level) { int i=idx; drawContours(c, contours, i, Scalar(i),CV_FILLED); fillCtr[i]=1; while (hierarchy[i][0]!=-1) { int j=hierarchy[i][0]; if (fillCtr[j] == 0) { drawContours(c, contours, j, Scalar(j),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_16U); fillCtr.resize(contours.size()); for( int i=0;i <contours.size();i++) {="" if="" (hierarchy[i][3]="=" -1="" &amp;&amp;="" fillctr[i]="=0)" {="" drawtree(i,0);="" }="" }<="" p="">

imshow("contour",c);
imwrite("contour.tif",c);
waitKey(0);
}

}

Do you think that's a good way to solve this problem ?

#include "opencv2/highgui/highgui.hpp" #include <iostream> #include <fstream> #include <string> #include <sstream>

   #include "opencv2/highgui/highgui.hpp"
   #include <iostream>
   #include <fstream>
   #include <string>
   #include <sstream>

using namespace cv; 
using namespace std;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<char> fillCtr;

Mat c;

void DrawTree( int idx, int level) { int i=idx; drawContours(c, contours, i, Scalar(i),CV_FILLED); fillCtr[i]=1; while (hierarchy[i][0]!=-1) { { int j=hierarchy[i][0]; if (fillCtr[j] == 0) { { drawContours(c, contours, j, Scalar(j),CV_FILLED); fillCtr[j]=1; DrawTree(j,level); } i=hierarchy[i][0];

}
    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_16U); fillCtr.resize(contours.size()); for( int i=0;i <contours.size();i++) {="" if="" (hierarchy[i][3]="=" -1="" &amp;&amp;="" fillctr[i]="=0)" {="" drawtree(i,0);="" }="" }<="" p="">

<contours.size();i++)
{
   if (hierarchy[i][3] == -1 && fillCtr[i]==0)
   {
       DrawTree(i,0);
   }
 }

imshow("contour",c);
imwrite("contour.tif",c);
waitKey(0);
}

}

Do you think that's a good way to solve this problem ?

#include "opencv2/opencv.hpp" #include "opencv2/highgui/highgui.hpp" #include <iostream> #include <fstream> #include <string> #include <sstream> <iostream>

using namespace cv; 
using namespace std;

vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
vector<char> fillCtr;

Mat c;

void DrawTree( int idx, int level)
{

int i=idx;
drawContours(c, contours, i, Scalar(i),CV_FILLED);
fillCtr[i]=1;
while (hierarchy[i][0]!=-1)
{
    int j=hierarchy[i][0];
    if (fillCtr[j] == 0)
    {
        drawContours(c, contours, j, Scalar(j),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("14415468805620458.jpg",CV_LOAD_IMAGE_GRAYSCALE);
 imshow("original",x);
imshow("original", x);
Mat y;
threshold(x,y,50,255,THRESH_BINARY);
imshow("threshold",y);
y,cc;
double ccMin, ccMax;
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_16U);
fillCtr.resize(contours.size());
for( int i=0;i <contours.size();i++)
{
   if (hierarchy[i][3] labels1, labels2;
connectedComponents(y, labels1, 8, CV_32S);
minMaxIdx(labels1, &ccMin, &ccMax);
bitwise_not(y, y);
connectedComponents(y, labels2, 8, CV_32S);
Mat mask = labels1 == -1 && fillCtr[i]==0)
   {
       DrawTree(i,0);
   }
 }

imshow("contour",c);
imwrite("contour.tif",c);
0;
add(labels2, ccMax, labels2, mask);
labels1 = labels1 + labels2 - 1;
minMaxIdx(labels1, &ccMin, &ccMax);
cout<<ccMax<<"\n";
labels1.convertTo(cc, CV_8UC1, 32, 0);
applyColorMap(cc, cc, cv::COLORMAP_HSV);
imshow("Labels", cc);
waitKey(0);
}

}