cv::findContours makes grayscale image completly black

I’m trying to get fitEllipse work with openframeworks, but even though the code runs without error, the drawn image from cv::findContours is completly black.
Here is the code:

void ofApp::update(){
   ofBackground(100, 100, 100);
   bool bNewFrame = false;
   vidGrabber.update();
   bNewFrame = vidGrabber.isFrameNew();
   if (bNewFrame) {
        img = ofxCv::toCv(vidGrabber);
	    cvtColor(img, gray, CV_BGR2GRAY);
	    GaussianBlur(gray, gray, Size(9, 9), 2, 2);
        findContours(gray, contours, RETR_LIST, CHAIN_APPROX_NONE);
   }
}

and in draw():

void ofApp::draw(){
	ofxCv::drawMat(img, 20, 20);
	ofxCv::drawMat(gray, 360, 20);
}

contours is of type

vector<vector<point>> contours;

Any ideas why this isn’t working?

Looks like you’re using the findContours from the cv namespace. As far as I know cv::findContours() needs a binary image. You might better use findCountours() from the ofxCv::ContourFinder.

i’ve messed around with that, but the problem is i have to run this code

    cimg = Mat::zeros(gray.size(), CV_8UC3);
	
	for (size_t i = 0; i < contours.size(); i++)
	{
		size_t count = contours[i].size();
		if (count < 6)
			continue;
		
		Mat(contours[i]).convertTo(pointsf, CV_32F);
		RotatedRect box = fitEllipse(pointsf);
		if (MAX(box.size.width, box.size.height) > MIN(box.size.width, box.size.height) * 30)
			continue;
		drawContours(cimg, contours, (int)i, Scalar::all(255), 1, 8);
		ellipse(cimg, box, Scalar(0, 0, 255), 1, CV_AA);
		ellipse(cimg, box.center, box.size*0.5f, box.angle, 0, 360, Scalar(0, 255, 255), 1, CV_AA);
		
		box.points(vtx);
		for (int j = 0; j < 4; j++)
			line(cimg, vtx[j], vtx[(j + 1) % 4], Scalar(0, 255, 0), 1, CV_AA);
	}

and for that i think i need the vector<vector> contours?

As I said, cv::findContours needs a binary image. Look into the opencv documentation for findContours, there you will find the following:

Source, an 8-bit single-channel image. Non-zero pixels are treated as 1’s. Zero pixels remain 0’s, so the image is treated as binary . You can use compare() , inRange() , threshold() , adaptiveThreshold() , Canny() , and others to create a binary image out of a grayscale or color one.

In this opencv tutorial the Canny edge detector is used to convert the grayscale image into a binary image. Another option would be to use threshold or adaptiveThreshold.

Also look into the ContourFinder class of ofxCv for how it’s done there (using the threshold function).

Thanks a bunch! i think it works now!