Problem to find contours
I have problem to find contours. There are errors when show the countours. I must change some parameters?
The code:
imageInput = imread(PATH+images[i], CV_LOAD_IMAGE_COLOR);
fileOutput <<"Immagine: "<< images[i]<<"\n";
//verifico l'esistenza dell'immagine
if(! imageInput.data )
{
cout << "Impossibile aprire l'immagine" << std::endl ;
return -1;
}
//RGB->Livelli di grigio....necessario per OTSU
cvtColor( imageInput, imageInputGray, CV_BGR2GRAY );
//applico OTSU
// CV_THRESH_BINARY | CV_THRESH_OTSU per questioni di ottimizzazioni
double otsu_thresh_val= threshold( imageInputGray, imageAfterOtsu, 0, 255, CV_THRESH_BINARY | CV_THRESH_OTSU );
//applico 4 volte un filtro mediano 3x3 per risolvere il problema della 'polvere'
medianBlur(imageAfterOtsu, imageAfterMedianBlur, 3);
medianBlur(imageAfterMedianBlur, imageAfterMedianBlur, 3);
medianBlur(imageAfterMedianBlur, imageAfterMedianBlur, 3);
medianBlur(imageAfterMedianBlur, imageAfterMedianBlur, 3);
//labeling
int thresh = 0;
int max_thresh = 255;
RNG rng(12345);
vector<vector<Point> > contours;
vector<Vec4i> hierarchy;
double high_thresh_val = otsu_thresh_val,
lower_thresh_val = otsu_thresh_val * 0.33;
/// Detect edges using canny
Canny( imageAfterMedianBlur, imageAfterCanny, lower_thresh_val, high_thresh_val, 3 );
/// Find contours
findContours( imageAfterCanny, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );
/// Draw contours
Mat drawing = Mat::zeros( imageAfterCanny.size(), CV_8UC3 );
for( int j = 0; j< contours.size(); j++ )
{
Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
drawContours( drawing, contours, j, color, 1, 8, hierarchy, 0, Point() );
cout<<hierarchy[j][0]<<" "<<hierarchy[j][1]<<" "<<hierarchy[j][2]<<" "<<hierarchy[j][3]<<endl;
}
cout<<"---------"<<endl;
The image's result:
if there are errors, which ?