I am porting some code that is using OpenCV just to fit an ellipse. I may rewrite it later but for now I need to use the OpenCV routine.
This code is just a few lines but I had to change almost everything because the profile of cvFitEllipse2() has changed.
My problematic is the following one: convert an array of point (pair of uchar) into something I can feed cvFitEllipse2() with
I did not found a lot of code sample and I find that OpenCV is poorly documented (but since I am new to OpenCV, maybe this feeling is wrong).
Anyway here is what I wrote:
/* centers is the input uchar array */
CvBox2D box; //output
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* seq = cvCreateSeq(CV_32FC2, sizeof(CvSeq), sizeof(CvPoint2D32f), storage);
for(int h=0; h<mm; ++h)
{
CvPoint2D32f p;
p.x = (float)centers[h].x;
p.y = (float)centers[h].y;
cvSeqPush(seq, &p);
}
// Fits ellipse to current contour.
box = cvFitEllipse2(seq);
Problem: this code is damn slow compared to the original one.
- Is there any way to preallocate the memory required for seq? (using
the CvMemStorage object) - Are there better (simpler? containers than
CvSeq? - Can I replace the cvSeqPush by something faster?
Thanks a lot for your help!
I assume
centersis a vector of points.If you are happy to use the C++ interface instead of the old one, your pain is over.
Depending on the way centers is declared you can:
Or the hard-core approach:
EDIT
I have modified types to UCHAR