findContours impossible result [closed]

asked 2013-03-08 18:11:41 -0600

kro007 gravatar image

updated 2013-03-09 02:18:56 -0600

berak gravatar image

hy all,

using findContours on this image (or any other binarised image...)

image description

i have strange result :

image description

has you can see, somme point are negative, so when i want to draw coutours, it's crash.

here is extract of my code :

img_inrange.copyTo(img_contour);

Canny( img_contour, img_contour, 100, 200, 3 );

findContours(img_contour, contours, CV_RETR_LIST , CV_CHAIN_APPROX_NONE);

who can help mee ?

edit the is the full code :

#include "camtest.hpp"

Mat img_test,img_affiche,img_origin,img_hsv,img_inrange,img_inrange2,img_canny,img_contour,img_gray;
Point p(-100,-100);
bool ac=false;
RNG rng(12345);

int main(void)
{
    int camnumber=1;
    bool debug=false;
    vector <objet> bougie(NBR_BOUGIE);
    string cfile,mc;

        namedWindow("img",CV_WINDOW_NORMAL| CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);
        namedWindow("trackbar",CV_WINDOW_NORMAL| CV_WINDOW_KEEPRATIO | CV_GUI_EXPANDED);


        setMouseCallback( "img", onimg);
        waitKey(1);
        cfile.clear();
        cout<<"\nquelle image ?\n";
        cin>>cfile;
        img_origin=imread(cfile,3 );

            if(img_origin.data==NULL)
            {
                cout<<"erreur ouverture image..."<<endl;
                waitKey(1000);
                exit(0);
            }
            else
            {
                img_origin.copyTo(img_affiche);
                cvtColor(img_affiche,img_hsv,CV_RGB2HSV_FULL);
                imshow("img",img_affiche);
                waitKey(1);
            }

            //Scalar min(0,0,0),max(0,0,0);
            //jaune balle 72::121
            //jaune sol 135::147
            //rouge 170::186
            //bleu 16::28

            int min[3]={16,151,28},max[3]={39,255,255},canny[3]={7,2,3};//Hue, Sat, Value

            createTrackbar( "Hmin1", "trackbar",&min[0], 256);
            createTrackbar( "Hmax1", "trackbar", &max[0], 256);
            createTrackbar( "Smin1", "trackbar", &min[1], 256);
            createTrackbar( "Smax1", "trackbar", &max[1], 256);
            createTrackbar( "Vmin1", "trackbar", &min[2], 256);
            createTrackbar( "Vmax1", "trackbar", &max[2], 256);
            createTrackbar( "morph size", "trackbar", &canny[0], 15);

        vector<vector<Point> > contours,contours2;
        //Mat img_blob = Mat::zeros(img_hsv.rows, img_hsv.cols, CV_8UC1);
        Mat element = getStructuringElement( MORPH_RECT, Size( 2*canny[0] + 1, 2*canny[0]+1 ), Point( canny[0], canny[0] ) );
    while(1)
    {

        element = getStructuringElement( MORPH_RECT, Size( 2*canny[0] + 1, 2*canny[0]+1 ), Point( canny[0], canny[0] ) );

        inRange(img_hsv,Scalar(min[0],min[1],min[2]),Scalar(max[0],max[1],max[2]),img_inrange);

        blur( img_inrange, img_inrange, Size(3,3) );
        erode(img_inrange,img_inrange,element);
        dilate(img_inrange,img_inrange,element);

        threshold(img_inrange,img_inrange,1,255,THRESH_BINARY);
        img_inrange.copyTo(img_contour);


        Canny( img_contour, img_contour, 100, 200, 3 );


        imshow("img_inrange",img_inrange);
        imshow("canny",img_contour);
        //waitKey(0);

        findContours(img_contour, contours, CV_RETR_LIST , CV_CHAIN_APPROX_NONE);

        /*
        vector<Rect> boundRect(contours.size());
        vector<vector<Point> > contours_poly( contours.size() );
        Mat drawing = Mat::zeros( img_inrange.size(), CV_8UC3 );

            for(int i=0;i<contours.size();++i)
             {
                 //Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
                 Scalar color(255,0,0);
                boundRect[i] = boundingRect(contours[i]);
                rectangle( drawing, boundRect[i].tl(), boundRect[i].br(), color, 2, 8, 0 );
            }

        //imshow("result",drawing);*/

        img_inrange2.setTo(0);
        img_affiche.copyTo(img_inrange2,img_inrange);//ptit mascage
        imshow("img_inrange2",img_inrange2);


        waitKey(25);



    }
    cout<<"end of program...";
    destroyWindow("img");

}
edit retag flag offensive reopen merge delete

Closed for the following reason the question is answered, right answer was accepted by sturkmen
close date 2017-09-04 08:41:45.717781

Comments

Sorry, not enough information in your question. Perhaps there is something strange about the way your contours vector is defined.

xaffeine gravatar imagexaffeine ( 2013-03-08 18:20:38 -0600 )edit

what is strange with my verctor ?

vector<vector<Point> > contours;

kro007 gravatar imagekro007 ( 2013-03-08 18:27:18 -0600 )edit

In order to answer your question, more information about the code is needed.

xaffeine gravatar imagexaffeine ( 2013-03-08 18:30:13 -0600 )edit

nop i have edit and add my code

kro007 gravatar imagekro007 ( 2013-03-08 18:34:44 -0600 )edit

took the img above, thresholded, and findContours gave me 18 contours, all nice & valid.

do you think, it's related to this ?

berak gravatar imageberak ( 2013-03-09 05:04:49 -0600 )edit

it could be a bug, but i am using visual studio...so it's not this one...i will try the new opencv version.

kro007 gravatar imagekro007 ( 2013-03-09 06:10:45 -0600 )edit

using the new opencv 244, the problem is solved.... thanks for help !

kro007 gravatar imagekro007 ( 2013-03-09 10:45:57 -0600 )edit

ôh, nice ;)

berak gravatar imageberak ( 2013-03-09 11:05:47 -0600 )edit