1 | initial version |
Skimming over your code I did not notice anything immediately wrong. I did notice however that your rotation between the two camera poses seems a bit too large. Have you tried doing stereo rectification with Bouguet's toolbox? The OpenCV implementation is based upon his Matlab implemenation. If you also have problems with the Matlab version, I suspect that the rotation between the cameras may be too large, causing numerical problems.
One quick way to check is calculate the location of the epipoles in both the camera coordinate systems (these are the null spaces of the fundamental matrix). If the epipole's are near or within your image borders, then the rotation is too large the OpenCV rectification algorithm likely won't work. In this case you can either modify your camera setup, or you can use alternative rectification algorithms, such Pollefey's polar algorithm (M. Pollefeys, R. Koch and L. Van Gool, A simple and efficient rectification method for general motion, Proc. International Conference on Computer Vision).
2 | Found error in the code. |
Skimming over your code I did not notice anything immediately wrong. I did notice however that your rotation between the two camera poses seems a bit too large. Have you tried doing stereo rectification with Bouguet's toolbox? The OpenCV implementation is based upon his Matlab implemenation. If you also have problems with the Matlab version, I suspect that the rotation between the cameras may be too large, causing numerical problems.
One quick way to check is calculate the location of the epipoles in both the camera coordinate systems (these are the null spaces of the fundamental matrix). If the epipole's are near or within your image borders, then the rotation is too large the OpenCV rectification algorithm likely won't work. In this case you can either modify your camera setup, or you can use alternative rectification algorithms, such Pollefey's polar algorithm (M. Pollefeys, R. Koch and L. Van Gool, A simple and efficient rectification method for general motion, Proc. International Conference on Computer Vision).
Edit: So I have found the immediate error in the code gist from the question. The OpenCV calibration and rectification code likes doubles, and you were using floats, causing the error message you have received. I have forked an updated version here: gist.
Nonetheless, like I said above, your calibration result indicates a somewhat suboptimal stereo setup, so don't expect great rectification results. With your parameters, stereoRectify completely rotated the right image out of view, while zooming and almost completely cropping the left image. This can best (and most easily) be solved by making changes to your stereo setup. The alternative is to write your own rectification code (I went this route due to my somewhat extreme lens distortion).