I'm trying to fit an ellipse to a set of 2D integer points in javacv and keep running into problems. So my question is, what kind of data does cvFitEllipse2 expect?
From OpenCV's manual I found that
CvBox2D cvFitEllipse2(const CvArr* points)
Parameters points – Input 2D point set, stored in:
I have tried both CvSeq as well as CvMat the following way:
CvMemStorage mem = cvCreateMemStorage(0);
CvSeq seq = cvCreateSeq(0, Loader.sizeof(CvSeq.class), Loader.sizeof(CvPoint.class), mem);
CvPoint pts = new CvPoint(6);
pts.position(0).put(cvPoint(3, 0));
pts.position(1).put(cvPoint(1, 5));
pts.position(2).put(cvPoint(2, 9));
pts.position(3).put(cvPoint(1, 15));
pts.position(4).put(cvPoint(0, 9));
pts.position(5).put(cvPoint(1, 5));
for(int i=0; i<6; i++){
CvPoint v = new CvPoint(pts.position(i).x(), pts.position(i).y());
cvSeqPush(seq, v);
}
cvFitEllipse2(seq);
OpenCV Error: Bad argument (Unsupported sequence type) in cvFitEllipse2, file /tmp/opencv-2.4.3+dfsg/modules/imgproc/src/shapedescr.cpp, line 790 Exception in thread "main" java.lang.RuntimeException: /tmp/opencv-2.4.3+dfsg/modules/imgproc/src/shapedescr.cpp:790: error: (-5) Unsupported sequence type in function cvFitEllipse2
CvMat mat = cvCreateMat(pts.capacity(), 2, CV_32S);
mat.getIntBuffer().put(pts.asByteBuffer().asIntBuffer());
cvFitEllipse2(mat);
Error: OpenCV Error: Unsupported format or combination of formats (The matrix can not be converted to point sequence because of inappropriate element type) in unknown function, file ......\src\opencv\modules\imgproc\src\utils.cpp, line 59 Exception in thread "main" java.lang.RuntimeException: ......\src\opencv\modules\imgproc\src\utils.cpp:59: error: (-210) The matrix can not be converted to point sequence because of inappropriate element type
I also tried doing CvMat mat = cvCreateMat(1, pts.capacity()*2, CV_32S); with the same result. Unsurprisingly, it seems the CV_32S is wrong? I'm not sure.
I can't quite make any sense of this. I have asked this on javacv's google group but did not get any input yet.
I have found a solution to my problem using a simple float array instead. Here's my solution:
//6 2D-points stored in a 1-dimensional float array
float points[] = { 1.0f, 1.1f, 1.0f, 3.0f, 3.0f, 7.0f, 7.0f, 3.0f, 3.0f, 0.0f, 2.0f, 1.0f,1.0f, 0.0f};
//1 row matrix with 6 2-D points of type CV_32FC2 and a FloatPointer to the points array.
CvMat mat = cvMat(1, 6, CV_32FC2, new FloatPointer(points));
CvBox2D result = cvFitEllipse2(mat);
System.out.println(result);
result: ((4.42315, 4.259364), (5.7341976, 9.166761), 146.46394)