HamoudaOthman / javacv

Automatically exported from code.google.com/p/javacv
GNU General Public License v2.0
0 stars 0 forks source link

cvPOSIT returns NaN with apparently proper output #44

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
     CvPOSITObject positObj;
    double focalLength;
    CvTermCriteria criteria;
    float[] rotationMatrix = new float[9];
    float[] translationVector = new float[3];

        static final Point2f[] imagePoints = {new Point2f(-48, -224),
new Point2f(-287, -174),
new Point2f(132, -153),
new Point2f(-52, 149)};

        static final Point3f[] points = {new Point3f(0, 0, 0),
new Point3f(0, 0, 10),
new Point3f(10, 0, 0),
new Point3f(0, 10, 0)};
       criteria = new CvTermCriteria(
                cxcore.CV_TERMCRIT_EPS | cxcore.CV_TERMCRIT_ITER, 100, 1.0e-4f);
       CvPoint2D32f[] cvImagePoints = CvPoint2D32f.createArray(imagePoints.length);
        for (int i = 0; i < cvImagePoints.length; i++)
        {
            double x = imagePoints[i].x;
            double y = imagePoints[i].y;
            // do nothing if any points are invalid.
            cvImagePoints[i] = new CvPoint2D32f(x,y);
        }

         targetPoints = CvPoint3D32f.createArray(points.length);
        for (int i = 0; i < points.length; i++)
        {
            targetPoints[i] = new CvPoint3D32f(points[i].x, points[i].y,
                    points[i].z);
        }

        positObj = CvPOSITObject.create(targetPoints);

        cvPOSIT(positObj, cvImagePoints, focalLength, criteria.byValue(),
                rotationMatrix,
                translationVector);

What is the expected output? What do you see instead?
I expect valid rotation matrix and translation vector.
Instead all array entries are NaN.

The SAME code (essentially) in C++ works perfectly.

What version of the product are you using? On what operating system?
OpenCV 2.1,  Latest, as far as I can tell, JavaCV

Please provide any additional information below.

I have tried various ways of creating the structures to use in the call to 
cvPOSIT with no change in the result.  I have since written a C++ version to 
process my intended data with full success, but my whole application requires 
Java, so I would like to know how to debug this or interpret how to properly 
get the data into the call.

Original issue reported on code.google.com by edbol...@gmail.com on 15 Jan 2011 at 5:38

GoogleCodeExporter commented 8 years ago
Can you remove all "new CvPoint2D32f(x,y)" can see if that works?

Original comment by samuel.a...@gmail.com on 15 Jan 2011 at 5:45

GoogleCodeExporter commented 8 years ago
Well, that was a good suggestion! My Junit test now works and matches the C++ 
results.
I still, for some reason, am getting all NaN in my live application, but 
hopefully I will find the source of the problem.  More test loops to be 
written, I guess.  

I guess I was TOO Java-like in my array creation.  However, the code I 
originally wrote corresponds closely with the C++ version by instantiating 
CvPoint2D32f and pushing it onto a vector.

Thanks again for the useful response!

Ed

Original comment by edbol...@gmail.com on 15 Jan 2011 at 9:46

GoogleCodeExporter commented 8 years ago
Found another instance of instantiating CvPoint3D32f in an alternate way of 
creating the CvPOSITObject.
Now I am getting valid transforms.
This issue can be closed - it is not a defect, although lack of good 
documentation is a real concern.
I am not clear if this is a common problem with using JavaCV or just with this 
particular method.

Original comment by edbol...@gmail.com on 15 Jan 2011 at 9:59

GoogleCodeExporter commented 8 years ago
Yes, it's quite confusing, but this is how JNA works... I had no choice for 
JavaCV when using JNA, but I will be moving away from JNA, and it should make 
it more consistent and less error prone I think. We will see...

Original comment by samuel.a...@gmail.com on 16 Jan 2011 at 1:49

GoogleCodeExporter commented 8 years ago
I didn't get it, should "new CvPoint2D32f(x,y)" be replaced by what? I tried 
with cvPoint2D32f() but the same result, below are my codes which returns all 
"NaN":

CvPoint3D32f[] points = CvPoint3D32f.createArray(4);
points[0] = cvPoint3D32f(0, 0, 0);
points[1] = cvPoint3D32f(4, 2, 0);
points[2] = cvPoint3D32f(0, 0, 8);
points[3] = cvPoint3D32f(0, 2, 8);
CvPoint2D32f[] imagePoints = CvPoint2D32f.createArray(4);
imagePoints[0]  = cvPoint2D32f(0, 0);
imagePoints[1]  = cvPoint2D32f(228, -216);
imagePoints[2]  = cvPoint2D32f(-309, -273);
imagePoints[3]  = cvPoint2D32f(-311, -378);
CvPOSITObject obj = cvCreatePOSITObject(points, 4);
CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 
100, 1.0e-4f);
float[] rotationMatrix = new float[9];
float[] transitionVector = new float[3];
cvPOSIT(obj, imagePoints, 5, criteria.byValue(), rotationMatrix, 
transitionVector);
for (int i = 0; i < rotationMatrix.length; i++) {
    System.out.println(rotationMatrix[i]);
}
for (int i = 0; i < transitionVector.length; i++) {
    System.out.println(transitionVector[i]);
}
cvReleasePOSITObject(obj.pointerByReference());

Original comment by bjzhangh...@gmail.com on 28 Jan 2011 at 3:37

GoogleCodeExporter commented 8 years ago
It seems that after doing the "createArray", the point records are already 
created.
So you want to do "points[0].set(0,0,0)"  and so on.
Same with imagePoints[3].set(-311,-378) for example.
As I responded, it is just not clear, but if you look at how the array is 
created, it makes a bit of sense - the C++ stuff expects the array elements to 
be contiguously allocated.

Original comment by edbol...@gmail.com on 28 Jan 2011 at 4:55

GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
[deleted comment]
GoogleCodeExporter commented 8 years ago
It makes sense, unfortunately I still saw only "NaN" after making the change 
(see below). Can you post a sample code that works?

CvPoint3D32f[] points = CvPoint3D32f.createArray(4);
points[0].set(0, 0, 0);
points[1].set(4, 2, 0);
points[2].set(0, 0, 8);
points[3].set(0, 2, 8);
CvPoint2D32f[] imagePoints = CvPoint2D32f.createArray(4);
imagePoints[0].set(0, 0);
imagePoints[1].set(228, -216);
imagePoints[2].set(-309, -273);
imagePoints[3].set(-311, -378);
CvPOSITObject obj = cvCreatePOSITObject(points, 4);
CvTermCriteria criteria = cvTermCriteria(CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 
100, 1.0e-4f);
float[] rotationMatrix = new float[9];
float[] transitionVector = new float[3];
cvPOSIT(obj, imagePoints, 5, criteria.byValue(), rotationMatrix, 
transitionVector);
for (int i = 0; i < rotationMatrix.length; i++) {
    System.out.println(rotationMatrix[i]);
}
for (int i = 0; i < transitionVector.length; i++) {
    System.out.println(transitionVector[i]);
}
cvReleasePOSITObject(obj.pointerByReference());

Original comment by bjzhangh...@gmail.com on 29 Jan 2011 at 12:55

GoogleCodeExporter commented 8 years ago
If I use a focal length of 500 instead of 5, I get other values than NaN.. are 
they valid?

Original comment by samuel.a...@gmail.com on 29 Jan 2011 at 1:24

GoogleCodeExporter commented 8 years ago
You're right, in this way it works. I was using 5 because the focal length was 
unknown, however the rotation matrix result differs while focal length changes, 
then how to choose a reasonable value as focal length? It's not a problem of 
javacv of course.

Original comment by bjzhangh...@gmail.com on 29 Jan 2011 at 1:48

GoogleCodeExporter commented 8 years ago
I don't understand how the focal length comes into it either.  The math relies 
on it heavily, however, as it affects the perspective projection.  Camera 
calibration programs (some included in OpenCV) will derive this value, but I 
have never understood what units are expected in POSIT. I believe it is in the 
same units as the model coordinates, but might be in pixels.  I am using it 
with a Wii camera which cannot be easily calibrated - the generally published 
value is 1300 or 1380 for that.

Original comment by edbol...@gmail.com on 29 Jan 2011 at 5:30

GoogleCodeExporter commented 8 years ago
The unit of focal length should be in pixel, because focal length equals to 
physical focal length (mm) multiplied by density of pixels (pixels per mm).

Original comment by bjzhangh...@gmail.com on 30 Jan 2011 at 1:37

GoogleCodeExporter commented 8 years ago
I guess this needs to be reopened, as POSIT fails to work with the cpp version 
of JavaCV along with OpenCV 2.2.  The code that worked for the older version of 
JavaCV had to be refactored to use things like points.position(i).set(0,0,10).  
This appears to do the proper thing when I read the data back, but calling 
cvPOSIT again produces NaN results regardless of input.  A C++ test of the same 
data using OpenCV 2.2 produces correct results.  I can supply a test case if 
necessary, but perhaps someone knows what else has changed in sending a vector 
or array of CvPoint2D32F and CvPoint3D23F to cvPOSIT.  The problem could be 
anywhere, perhaps in the creation of the CvPOSITObject which is opaque and 
somewhat difficult to investigate.

Original comment by edbol...@gmail.com on 11 May 2011 at 6:27

GoogleCodeExporter commented 8 years ago
You do call points.position(0) before calling cvPOSIT() right? If so, can you 
provide your test case? thanks

Original comment by samuel.a...@gmail.com on 11 May 2011 at 6:30

GoogleCodeExporter commented 8 years ago
I'll try to put together a simplified test case (right now it is part of a set 
of JUnit tests and some possibly proprietary code).  Maybe my test case will 
just work :)
Yes, I am using the weird points.position(i).set and so on, and have verified 
that I can get the correct values back. I can't do much with CvPOSITObject once 
it is created since it is opaque.  I find it difficult to debug at the C++ 
level when running from JavaCV - I have done so in the distant past, but am 
unsure how to do it at the moment.
If it matters, I am running on Ubuntu 10.10 32 bit.

Thanks for the great project. I have tried following the source, but don't see 
anything wildly different from other stuff that does appear to work.

Original comment by edbol...@gmail.com on 11 May 2011 at 9:13

GoogleCodeExporter commented 8 years ago
Here is a test program.  It produces NaN for all results.  Interestingly, when 
creating and debugging it, the first "successful" run produced actual numbers 
for the translation, although I think they were wrong.  Ever since, I get NaN :)

The results from the C++ version of this are:
-.- ESTIMATED POSE
0.707107 | 0 | 0.707107 | 0
0.5 | 0.707107 | -0.5 | 0
-0.5 | 0.707107 | 0.5 | 100
0 | 0 | 0 | 1

Where 0,0,100 is the translation vector.

Original comment by edbol...@gmail.com on 11 May 2011 at 11:02

Attachments:

GoogleCodeExporter commented 8 years ago
Apparently, I need to use point.position(0) as the argument in all the calls
- input and output!  That is certainly not clear from usage elsewhere.

The calls:
        positObj = CvPOSITObject.create(targetPoints.position(0),nPoints);
and
        cvPOSIT(positObj, cvImagePoints.position(0), focalLength, criteria,
                rotationMatrix,
                translationVector);

do the trick.

Ohhh - did you mean that I have to actually "position" the points object
before the call?
Weird - it does remember the last position used, doesn't it?  Boy, I wish
there was real usage documentation going on here!

Ed

Original comment by edbol...@gmail.com on 12 May 2011 at 12:36

GoogleCodeExporter commented 8 years ago
Works if
        targetPoints.position(0);
        cvImagePoints.position(0);
is added after initialization and before using.

Original comment by edbol...@gmail.com on 12 May 2011 at 1:07