findContours impossible result [closed]
hy all,
using findContours on this image (or any other binarised image...)
i have strange result :
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");
}
Sorry, not enough information in your question. Perhaps there is something strange about the way your contours vector is defined.
what is strange with my verctor ?
vector<vector<Point> > contours;
In order to answer your question, more information about the code is needed.
nop i have edit and add my code
took the img above, thresholded, and findContours gave me 18 contours, all nice & valid.
do you think, it's related to this ?
it could be a bug, but i am using visual studio...so it's not this one...i will try the new opencv version.
using the new opencv 244, the problem is solved.... thanks for help !
ôh, nice ;)