Discussion:
projectPoints not working as expected
ed7000mk1
2011-01-24 16:56:33 UTC
Permalink
I have a set of 3d points (they are arranged in two planes), which I am trying to convert to 2d points, as if a camera had taken a photo of them.

When I put them really far from the camera, (z value of tvec is really large, and focal length is also large) it works as expected. http://db.tt/gtmrSUA (The red ones are closer to the camera)

If I move them all closer to the camera, they look distorted somehow (my distortion matrix is all zeroes). http://db.tt/OFJQsV7 If I use project points at the point I actually want to use, the points are all over the place - I can't even work out what kind of projection would cause this effect http://db.tt/c2c4vpM


Can anyone please help me identify what I am doing wrong, or is this a bug?


Extra info - I am doing it like this:

//camera matrix
cv::Mat camM(3,3,CV_32FC1);
camM.at<float>(0,0) = 5000;
camM.at<float>(0,1) = 0;
camM.at<float>(0,2) = 255;
camM.at<float>(1,0) = 0;
camM.at<float>(1,1) = 5000;
camM.at<float>(1,2) = 255;
camM.at<float>(2,0) = 0;
camM.at<float>(2,1) = 0;
camM.at<float>(2,2) = 1;

//rotation matrix
cv::Mat rvec(3,1,CV_32FC1);
rvec.at<float>(0,0)=0;
rvec.at<float>(1,0)=0;
rvec.at<float>(2,0)=-1.57079633;//(-2/pi)

//translation matrix
cv::Mat tvec(3,1,CV_32FC1);
tvec.at<float>(0,0)=-30;
tvec.at<float>(1,0)=+25;
tvec.at<float>(2,0)=-1000;// this gives sensible looking images at 100000 or higher

cv::Mat distortions(5,1,CV_32FC1);

cv::projectPoints(Mat(calmatrix1_3d),rvec,tvec,camM,distortions,calmatrix1);
cv::projectPoints(Mat(calmatrix2_3d),rvec,tvec,camM,distortions,calmatrix2);



all the variables are single precision floating point, I did try to change everything to double, but then I just got a load of zeroes.



------------------------------------

Change settings: http://www.yahoogroups.com/mygroups, select
Get Emails (get all posts)
Daily Digest (one summary email per day)
Read on the web (read posts on the web only)Or Unsubscribe by mailing OpenCV-***@yahoogroups.com
Shervin Emami
2011-01-25 22:44:12 UTC
Permalink
If you got a load of zeros when you used doubles, then that is telling you
something important. You should stick to doubles for your initial testing
since they will be more accurate and it seems in this case that when you are
using floats that it is giving very different values, implying that your
numbers are either too small to fit in a float or too big to fit in a float.
So I'd recommend switching to doubles for now, try to figure out why
everything is 0 and not what you expect (could just be that the Z axis is
the wrong way around or something like that), then switch back to floats
when it is finally working. Whenever I'm trying to solve a problem like
this, I visualize the data on the screen and move/zoom/rotate the points to
figure out if one of my axis' are wrong or something like that. Because just
looking at numbers can only tell you so much when it comes to 3D transforms.


Cheers,
Shervin Emami.
http://www.shervinemami.co.cc/openCV.html
Post by ed7000mk1
I have a set of 3d points (they are arranged in two planes), which I am
trying to convert to 2d points, as if a camera had taken a photo of them.
When I put them really far from the camera, (z value of tvec is really
large, and focal length is also large) it works as expected.
http://db.tt/gtmrSUA (The red ones are closer to the camera)
If I move them all closer to the camera, they look distorted somehow (my
distortion matrix is all zeroes). http://db.tt/OFJQsV7 If I use project
points at the point I actually want to use, the points are all over the
place - I can't even work out what kind of projection would cause this
effect http://db.tt/c2c4vpM
Can anyone please help me identify what I am doing wrong, or is this a bug?
//camera matrix
cv::Mat camM(3,3,CV_32FC1);
camM.at<float>(0,0) = 5000;
camM.at<float>(0,1) = 0;
camM.at<float>(0,2) = 255;
camM.at<float>(1,0) = 0;
camM.at<float>(1,1) = 5000;
camM.at<float>(1,2) = 255;
camM.at<float>(2,0) = 0;
camM.at<float>(2,1) = 0;
camM.at<float>(2,2) = 1;
//rotation matrix
cv::Mat rvec(3,1,CV_32FC1);
rvec.at<float>(0,0)=0;
rvec.at<float>(1,0)=0;
rvec.at<float>(2,0)=-1.57079633;//(-2/pi)
//translation matrix
cv::Mat tvec(3,1,CV_32FC1);
tvec.at<float>(0,0)=-30;
tvec.at<float>(1,0)=+25;
tvec.at<float>(2,0)=-1000;// this gives sensible looking images at 100000 or higher
cv::Mat distortions(5,1,CV_32FC1);
cv::projectPoints(Mat(calmatrix1_3d),rvec,tvec,camM,distortions,calmatrix1);
cv::projectPoints(Mat(calmatrix2_3d),rvec,tvec,camM,distortions,calmatrix2);
all the variables are single precision floating point, I did try to change
everything to double, but then I just got a load of zeroes.
ed7000mk1
2011-01-31 21:40:01 UTC
Permalink
Thanks Shervin, I tried it again with double precision variables and eventually managed to get it working.

I don't know if I was just making a really silly mistake, but in case anyone else has the same problem, although the documentation says that an empty matrix will be assumed to be all zeroes, creating it like this:
cv::Mat distortion(4,1,CV_64FC1);
does not work, however if the all the values are set to zero manually,
distortion.at<double>(0,0)=0;
distortion.at<double>(1,0)=0;
etc..
then it will work!
Post by Shervin Emami
If you got a load of zeros when you used doubles, then that is telling you
something important. You should stick to doubles for your initial testing
since they will be more accurate and it seems in this case that when you are
using floats that it is giving very different values, implying that your
numbers are either too small to fit in a float or too big to fit in a float.
So I'd recommend switching to doubles for now, try to figure out why
everything is 0 and not what you expect (could just be that the Z axis is
the wrong way around or something like that), then switch back to floats
when it is finally working. Whenever I'm trying to solve a problem like
this, I visualize the data on the screen and move/zoom/rotate the points to
figure out if one of my axis' are wrong or something like that. Because just
looking at numbers can only tell you so much when it comes to 3D transforms.
Cheers,
Shervin Emami.
http://www.shervinemami.co.cc/openCV.html
Post by ed7000mk1
I have a set of 3d points (they are arranged in two planes), which I am
trying to convert to 2d points, as if a camera had taken a photo of them.
When I put them really far from the camera, (z value of tvec is really
large, and focal length is also large) it works as expected.
http://db.tt/gtmrSUA (The red ones are closer to the camera)
If I move them all closer to the camera, they look distorted somehow (my
distortion matrix is all zeroes). http://db.tt/OFJQsV7 If I use project
points at the point I actually want to use, the points are all over the
place - I can't even work out what kind of projection would cause this
effect http://db.tt/c2c4vpM
Can anyone please help me identify what I am doing wrong, or is this a bug?
//camera matrix
cv::Mat camM(3,3,CV_32FC1);
camM.at<float>(0,0) = 5000;
camM.at<float>(0,1) = 0;
camM.at<float>(0,2) = 255;
camM.at<float>(1,0) = 0;
camM.at<float>(1,1) = 5000;
camM.at<float>(1,2) = 255;
camM.at<float>(2,0) = 0;
camM.at<float>(2,1) = 0;
camM.at<float>(2,2) = 1;
//rotation matrix
cv::Mat rvec(3,1,CV_32FC1);
rvec.at<float>(0,0)=0;
rvec.at<float>(1,0)=0;
rvec.at<float>(2,0)=-1.57079633;//(-2/pi)
//translation matrix
cv::Mat tvec(3,1,CV_32FC1);
tvec.at<float>(0,0)=-30;
tvec.at<float>(1,0)=+25;
tvec.at<float>(2,0)=-1000;// this gives sensible looking images at 100000 or higher
cv::Mat distortions(5,1,CV_32FC1);
cv::projectPoints(Mat(calmatrix1_3d),rvec,tvec,camM,distortions,calmatrix1);
cv::projectPoints(Mat(calmatrix2_3d),rvec,tvec,camM,distortions,calmatrix2);
all the variables are single precision floating point, I did try to change
everything to double, but then I just got a load of zeroes.
------------------------------------

Change settings: http://www.yahoogroups.com/mygroups, select
Get Emails (get all posts)
Daily Digest (one summary email per day)
Read on the web (read posts on the web only)Or Unsubscribe by mailing OpenCV-***@yahoogroups.com
Loading...