Search in sources :

Example 1 with RectifyCalibrated

use of boofcv.alg.geo.rectify.RectifyCalibrated in project BoofCV by lessthanoptimal.

the class CalibrateStereoPlanarGuiApp method setRectification.

/**
 * Computes stereo rectification and then passes the distortion along to the gui.
 */
private void setRectification(final StereoParameters param) {
    // calibration matrix for left and right camera
    DMatrixRMaj K1 = PerspectiveOps.calibrationMatrix(param.getLeft(), (DMatrixRMaj) null);
    DMatrixRMaj K2 = PerspectiveOps.calibrationMatrix(param.getRight(), (DMatrixRMaj) null);
    RectifyCalibrated rectify = RectifyImageOps.createCalibrated();
    rectify.process(K1, new Se3_F64(), K2, param.getRightToLeft().invert(null));
    final DMatrixRMaj rect1 = rectify.getRect1();
    final DMatrixRMaj rect2 = rectify.getRect2();
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            gui.setRectification(param.getLeft(), rect1, param.getRight(), rect2);
        }
    });
    gui.repaint();
}
Also used : RectifyCalibrated(boofcv.alg.geo.rectify.RectifyCalibrated) DMatrixRMaj(org.ejml.data.DMatrixRMaj) Se3_F64(georegression.struct.se.Se3_F64)

Example 2 with RectifyCalibrated

use of boofcv.alg.geo.rectify.RectifyCalibrated in project BoofCV by lessthanoptimal.

the class ExampleRectifyCalibratedStereo method main.

public static void main(String[] args) {
    String dir = UtilIO.pathExample("calibration/stereo/Bumblebee2_Chess/");
    StereoParameters param = CalibrationIO.load(new File(dir, "stereo.yaml"));
    // load images
    BufferedImage origLeft = UtilImageIO.loadImage(dir, "left05.jpg");
    BufferedImage origRight = UtilImageIO.loadImage(dir, "right05.jpg");
    // distorted images
    Planar<GrayF32> distLeft = ConvertBufferedImage.convertFromPlanar(origLeft, null, true, GrayF32.class);
    Planar<GrayF32> distRight = ConvertBufferedImage.convertFromPlanar(origRight, null, true, GrayF32.class);
    // storage for undistorted + rectified images
    Planar<GrayF32> rectLeft = distLeft.createSameShape();
    Planar<GrayF32> rectRight = distRight.createSameShape();
    // Compute rectification
    RectifyCalibrated rectifyAlg = RectifyImageOps.createCalibrated();
    Se3_F64 leftToRight = param.getRightToLeft().invert(null);
    // original camera calibration matrices
    DMatrixRMaj K1 = PerspectiveOps.calibrationMatrix(param.getLeft(), (DMatrixRMaj) null);
    DMatrixRMaj K2 = PerspectiveOps.calibrationMatrix(param.getRight(), (DMatrixRMaj) null);
    rectifyAlg.process(K1, new Se3_F64(), K2, leftToRight);
    // rectification matrix for each image
    DMatrixRMaj rect1 = rectifyAlg.getRect1();
    DMatrixRMaj rect2 = rectifyAlg.getRect2();
    // New calibration matrix,
    // Both cameras have the same one after rectification.
    DMatrixRMaj rectK = rectifyAlg.getCalibrationMatrix();
    // Adjust the rectification to make the view area more useful
    RectifyImageOps.fullViewLeft(param.left, rect1, rect2, rectK);
    // RectifyImageOps.allInsideLeft(param.left, leftHanded, rect1, rect2, rectK);
    // undistorted and rectify images
    // TODO simplify code some how
    FMatrixRMaj rect1_F32 = new FMatrixRMaj(3, 3);
    FMatrixRMaj rect2_F32 = new FMatrixRMaj(3, 3);
    ConvertMatrixData.convert(rect1, rect1_F32);
    ConvertMatrixData.convert(rect2, rect2_F32);
    ImageDistort rectifyImageLeft = RectifyImageOps.rectifyImage(param.getLeft(), rect1_F32, BorderType.SKIP, distLeft.getImageType());
    ImageDistort rectifyImageRight = RectifyImageOps.rectifyImage(param.getRight(), rect2_F32, BorderType.SKIP, distRight.getImageType());
    rectifyImageLeft.apply(distLeft, rectLeft);
    rectifyImageRight.apply(distRight, rectRight);
    // convert for output
    BufferedImage outLeft = ConvertBufferedImage.convertTo(rectLeft, null, true);
    BufferedImage outRight = ConvertBufferedImage.convertTo(rectRight, null, true);
    // show results and draw a horizontal line where the user clicks to see rectification easier
    ListDisplayPanel panel = new ListDisplayPanel();
    panel.addItem(new RectifiedPairPanel(true, origLeft, origRight), "Original");
    panel.addItem(new RectifiedPairPanel(true, outLeft, outRight), "Rectified");
    ShowImages.showWindow(panel, "Stereo Rectification Calibrated", true);
}
Also used : FMatrixRMaj(org.ejml.data.FMatrixRMaj) ListDisplayPanel(boofcv.gui.ListDisplayPanel) RectifyCalibrated(boofcv.alg.geo.rectify.RectifyCalibrated) DMatrixRMaj(org.ejml.data.DMatrixRMaj) ImageDistort(boofcv.alg.distort.ImageDistort) RectifiedPairPanel(boofcv.gui.stereo.RectifiedPairPanel) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) GrayF32(boofcv.struct.image.GrayF32) StereoParameters(boofcv.struct.calib.StereoParameters) File(java.io.File) Se3_F64(georegression.struct.se.Se3_F64)

Example 3 with RectifyCalibrated

use of boofcv.alg.geo.rectify.RectifyCalibrated in project BoofCV by lessthanoptimal.

the class ExampleStereoDisparity method rectify.

/**
 * Rectified the input images using known calibration.
 */
public static RectifyCalibrated rectify(GrayU8 origLeft, GrayU8 origRight, StereoParameters param, GrayU8 rectLeft, GrayU8 rectRight) {
    // Compute rectification
    RectifyCalibrated rectifyAlg = RectifyImageOps.createCalibrated();
    Se3_F64 leftToRight = param.getRightToLeft().invert(null);
    // original camera calibration matrices
    DMatrixRMaj K1 = PerspectiveOps.calibrationMatrix(param.getLeft(), (DMatrixRMaj) null);
    DMatrixRMaj K2 = PerspectiveOps.calibrationMatrix(param.getRight(), (DMatrixRMaj) null);
    rectifyAlg.process(K1, new Se3_F64(), K2, leftToRight);
    // rectification matrix for each image
    DMatrixRMaj rect1 = rectifyAlg.getRect1();
    DMatrixRMaj rect2 = rectifyAlg.getRect2();
    // New calibration matrix,
    DMatrixRMaj rectK = rectifyAlg.getCalibrationMatrix();
    // Adjust the rectification to make the view area more useful
    RectifyImageOps.allInsideLeft(param.left, rect1, rect2, rectK);
    // undistorted and rectify images
    FMatrixRMaj rect1_F32 = new FMatrixRMaj(3, 3);
    FMatrixRMaj rect2_F32 = new FMatrixRMaj(3, 3);
    ConvertMatrixData.convert(rect1, rect1_F32);
    ConvertMatrixData.convert(rect2, rect2_F32);
    ImageDistort<GrayU8, GrayU8> imageDistortLeft = RectifyImageOps.rectifyImage(param.getLeft(), rect1_F32, BorderType.SKIP, origLeft.getImageType());
    ImageDistort<GrayU8, GrayU8> imageDistortRight = RectifyImageOps.rectifyImage(param.getRight(), rect2_F32, BorderType.SKIP, origRight.getImageType());
    imageDistortLeft.apply(origLeft, rectLeft);
    imageDistortRight.apply(origRight, rectRight);
    return rectifyAlg;
}
Also used : FMatrixRMaj(org.ejml.data.FMatrixRMaj) RectifyCalibrated(boofcv.alg.geo.rectify.RectifyCalibrated) DMatrixRMaj(org.ejml.data.DMatrixRMaj) GrayU8(boofcv.struct.image.GrayU8) Se3_F64(georegression.struct.se.Se3_F64)

Example 4 with RectifyCalibrated

use of boofcv.alg.geo.rectify.RectifyCalibrated in project BoofCV by lessthanoptimal.

the class ExampleStereoDisparity3D method main.

public static void main(String[] args) {
    // ------------- Compute Stereo Correspondence
    // Load camera images and stereo camera parameters
    String calibDir = UtilIO.pathExample("calibration/stereo/Bumblebee2_Chess/");
    String imageDir = UtilIO.pathExample("stereo/");
    StereoParameters param = CalibrationIO.load(new File(calibDir, "stereo.yaml"));
    // load and convert images into a BoofCV format
    BufferedImage origLeft = UtilImageIO.loadImage(imageDir, "chair01_left.jpg");
    BufferedImage origRight = UtilImageIO.loadImage(imageDir, "chair01_right.jpg");
    GrayU8 distLeft = ConvertBufferedImage.convertFrom(origLeft, (GrayU8) null);
    GrayU8 distRight = ConvertBufferedImage.convertFrom(origRight, (GrayU8) null);
    // re-scale input images
    GrayU8 scaledLeft = new GrayU8((int) (distLeft.width * scale), (int) (distLeft.height * scale));
    GrayU8 scaledRight = new GrayU8((int) (distRight.width * scale), (int) (distRight.height * scale));
    new FDistort(distLeft, scaledLeft).scaleExt().apply();
    new FDistort(distRight, scaledRight).scaleExt().apply();
    // Don't forget to adjust camera parameters for the change in scale!
    PerspectiveOps.scaleIntrinsic(param.left, scale);
    PerspectiveOps.scaleIntrinsic(param.right, scale);
    // rectify images and compute disparity
    GrayU8 rectLeft = new GrayU8(scaledLeft.width, scaledLeft.height);
    GrayU8 rectRight = new GrayU8(scaledRight.width, scaledRight.height);
    RectifyCalibrated rectAlg = ExampleStereoDisparity.rectify(scaledLeft, scaledRight, param, rectLeft, rectRight);
    // GrayU8 disparity = ExampleStereoDisparity.denseDisparity(rectLeft, rectRight, 3,minDisparity, maxDisparity);
    GrayF32 disparity = ExampleStereoDisparity.denseDisparitySubpixel(rectLeft, rectRight, 3, minDisparity, maxDisparity);
    // ------------- Convert disparity image into a 3D point cloud
    // The point cloud will be in the left cameras reference frame
    DMatrixRMaj rectK = rectAlg.getCalibrationMatrix();
    DMatrixRMaj rectR = rectAlg.getRectifiedRotation();
    // used to display the point cloud
    PointCloudViewer viewer = new PointCloudViewer(rectK, 10);
    viewer.setPreferredSize(new Dimension(rectLeft.width, rectLeft.height));
    // extract intrinsic parameters from rectified camera
    double baseline = param.getBaseline();
    double fx = rectK.get(0, 0);
    double fy = rectK.get(1, 1);
    double cx = rectK.get(0, 2);
    double cy = rectK.get(1, 2);
    // Iterate through each pixel in disparity image and compute its 3D coordinate
    Point3D_F64 pointRect = new Point3D_F64();
    Point3D_F64 pointLeft = new Point3D_F64();
    for (int y = 0; y < disparity.height; y++) {
        for (int x = 0; x < disparity.width; x++) {
            double d = disparity.unsafe_get(x, y) + minDisparity;
            // skip over pixels were no correspondence was found
            if (d >= rangeDisparity)
                continue;
            // Coordinate in rectified camera frame
            pointRect.z = baseline * fx / d;
            pointRect.x = pointRect.z * (x - cx) / fx;
            pointRect.y = pointRect.z * (y - cy) / fy;
            // rotate into the original left camera frame
            GeometryMath_F64.multTran(rectR, pointRect, pointLeft);
            // add pixel to the view for display purposes and sets its gray scale value
            int v = rectLeft.unsafe_get(x, y);
            viewer.addPoint(pointLeft.x, pointLeft.y, pointLeft.z, v << 16 | v << 8 | v);
        }
    }
    // display the results.  Click and drag to change point cloud camera
    BufferedImage visualized = VisualizeImageData.disparity(disparity, null, minDisparity, maxDisparity, 0);
    ShowImages.showWindow(visualized, "Disparity");
    ShowImages.showWindow(viewer, "Point Cloud");
}
Also used : Point3D_F64(georegression.struct.point.Point3D_F64) RectifyCalibrated(boofcv.alg.geo.rectify.RectifyCalibrated) DMatrixRMaj(org.ejml.data.DMatrixRMaj) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) GrayF32(boofcv.struct.image.GrayF32) FDistort(boofcv.abst.distort.FDistort) PointCloudViewer(boofcv.gui.d3.PointCloudViewer) GrayU8(boofcv.struct.image.GrayU8) StereoParameters(boofcv.struct.calib.StereoParameters) File(java.io.File)

Example 5 with RectifyCalibrated

use of boofcv.alg.geo.rectify.RectifyCalibrated in project BoofCV by lessthanoptimal.

the class ExampleStereoTwoViewsOneCamera method rectifyImages.

/**
 * Remove lens distortion and rectify stereo images
 *
 * @param distortedLeft  Input distorted image from left camera.
 * @param distortedRight Input distorted image from right camera.
 * @param leftToRight    Camera motion from left to right
 * @param intrinsic      Intrinsic camera parameters
 * @param rectifiedLeft  Output rectified image for left camera.
 * @param rectifiedRight Output rectified image for right camera.
 * @param rectifiedK     Output camera calibration matrix for rectified camera
 */
public static void rectifyImages(GrayU8 distortedLeft, GrayU8 distortedRight, Se3_F64 leftToRight, CameraPinholeRadial intrinsic, GrayU8 rectifiedLeft, GrayU8 rectifiedRight, DMatrixRMaj rectifiedK) {
    RectifyCalibrated rectifyAlg = RectifyImageOps.createCalibrated();
    // original camera calibration matrices
    DMatrixRMaj K = PerspectiveOps.calibrationMatrix(intrinsic, (DMatrixRMaj) null);
    rectifyAlg.process(K, new Se3_F64(), K, leftToRight);
    // rectification matrix for each image
    DMatrixRMaj rect1 = rectifyAlg.getRect1();
    DMatrixRMaj rect2 = rectifyAlg.getRect2();
    // New calibration matrix,
    rectifiedK.set(rectifyAlg.getCalibrationMatrix());
    // Adjust the rectification to make the view area more useful
    RectifyImageOps.allInsideLeft(intrinsic, rect1, rect2, rectifiedK);
    // undistorted and rectify images
    FMatrixRMaj rect1_F32 = new FMatrixRMaj(3, 3);
    FMatrixRMaj rect2_F32 = new FMatrixRMaj(3, 3);
    ConvertMatrixData.convert(rect1, rect1_F32);
    ConvertMatrixData.convert(rect2, rect2_F32);
    ImageDistort<GrayU8, GrayU8> distortLeft = RectifyImageOps.rectifyImage(intrinsic, rect1_F32, BorderType.SKIP, distortedLeft.getImageType());
    ImageDistort<GrayU8, GrayU8> distortRight = RectifyImageOps.rectifyImage(intrinsic, rect2_F32, BorderType.SKIP, distortedRight.getImageType());
    distortLeft.apply(distortedLeft, rectifiedLeft);
    distortRight.apply(distortedRight, rectifiedRight);
}
Also used : FMatrixRMaj(org.ejml.data.FMatrixRMaj) RectifyCalibrated(boofcv.alg.geo.rectify.RectifyCalibrated) DMatrixRMaj(org.ejml.data.DMatrixRMaj) GrayU8(boofcv.struct.image.GrayU8) Se3_F64(georegression.struct.se.Se3_F64)

Aggregations

RectifyCalibrated (boofcv.alg.geo.rectify.RectifyCalibrated)9 DMatrixRMaj (org.ejml.data.DMatrixRMaj)9 Se3_F64 (georegression.struct.se.Se3_F64)8 FMatrixRMaj (org.ejml.data.FMatrixRMaj)5 GrayF32 (boofcv.struct.image.GrayF32)3 GrayU8 (boofcv.struct.image.GrayU8)3 RectifiedPairPanel (boofcv.gui.stereo.RectifiedPairPanel)2 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)2 CameraPinholeRadial (boofcv.struct.calib.CameraPinholeRadial)2 StereoParameters (boofcv.struct.calib.StereoParameters)2 BufferedImage (java.awt.image.BufferedImage)2 File (java.io.File)2 FDistort (boofcv.abst.distort.FDistort)1 ImageDistort (boofcv.alg.distort.ImageDistort)1 ListDisplayPanel (boofcv.gui.ListDisplayPanel)1 PointCloudViewer (boofcv.gui.d3.PointCloudViewer)1 Point3D_F64 (georegression.struct.point.Point3D_F64)1