Ask Your Question

Revision history [back]

I am using 3.4.6 and cvFindContours is way faster than cv::findContours.

I checked the code on cv::fincContours and don't really understand where the time goes: On the c++ call I just measure the time of:

 cv::findContours(img1, contours, cv::RETR_CCOMP, cv::CHAIN_APPROX_SIMPLE);

For the c call I measure the time of cvFindContours and the copy of contours to a c++ vector of vectors:

IplImage * img_stub = cvCreateImageHeader(cvSize(img1.cols, img1.rows), 8, 1);
cvSetData(img_stub, img1.data, (int)img1.step);

CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* _contours = nullptr;
cvFindContours( img_stub, storage, &_contours, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);

cv::Seq<CvSeq*> all_contours(cvTreeToNodeSeq( _contours, sizeof(CvSeq), storage ));
int i, total = (int)all_contours.size();
out.create(total, 1, 0, -1, true);
cv::SeqIterator<CvSeq*> it = all_contours.begin();
for( i = 0; i < total; i++, ++it )
{
    CvSeq* c = *it;
    ((CvContour*)c)->color = (int)i;
    out.create((int)c->total, 1, CV_32SC2, i, true);
    cv::Mat ci = out.getMat(i);
    CV_Assert( ci.isContinuous() );
    cvCvtSeqToArray(c, ci.ptr());
}

cvClearMemStorage( storage );
cvReleaseMemStorage(&storage);
cvReleaseImageHeader(&img_stub);

(I have copied the code from the cv::findContours implementation)

There is a big different between each calls:

cv::findContours : 25.6760 ms 
cv::findContours : 12.5560 ms 
cv::findContours : 12.4280 ms 
cv::findContours : 12.3370 ms 
cv::findContours : 13.2180 ms 
cv::findContours : 12.0600 ms 
cv::findContours : 12.7090 ms 
cv::findContours : 12.0270 ms 

cvFindContours : 4.9750 ms 
cvFindContours : 5.1180 ms 
cvFindContours : 4.9960 ms 
cvFindContours : 4.9750 ms 
cvFindContours : 5.4470 ms 
cvFindContours : 4.8460 ms 
cvFindContours : 5.1010 ms 
cvFindContours : 4.9200 ms

I ran the same code a few times with the same image.

Any idea why the difference?