Search in sources :

Example 1 with RectifyFundamental

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

the class ExampleRectifyUncalibratedStereo method rectify.

/**
 * Rectifies the image using the provided fundamental matrix.  Both the fundamental matrix
 * and set of inliers need to be accurate.  Small errors will cause large distortions.
 *
 * @param F Fundamental matrix
 * @param inliers Set of associated pairs between the two images.
 * @param origLeft Original input image.  Used for output purposes.
 * @param origRight Original input image.  Used for output purposes.
 */
public static void rectify(DMatrixRMaj F, List<AssociatedPair> inliers, BufferedImage origLeft, BufferedImage origRight) {
    // Unrectified images
    Planar<GrayF32> unrectLeft = ConvertBufferedImage.convertFromPlanar(origLeft, null, true, GrayF32.class);
    Planar<GrayF32> unrectRight = ConvertBufferedImage.convertFromPlanar(origRight, null, true, GrayF32.class);
    // storage for rectified images
    Planar<GrayF32> rectLeft = unrectLeft.createSameShape();
    Planar<GrayF32> rectRight = unrectRight.createSameShape();
    // Compute rectification
    RectifyFundamental rectifyAlg = RectifyImageOps.createUncalibrated();
    rectifyAlg.process(F, inliers, origLeft.getWidth(), origLeft.getHeight());
    // rectification matrix for each image
    DMatrixRMaj rect1 = rectifyAlg.getRect1();
    DMatrixRMaj rect2 = rectifyAlg.getRect2();
    // Adjust the rectification to make the view area more useful
    RectifyImageOps.fullViewLeft(origLeft.getWidth(), origLeft.getHeight(), rect1, rect2);
    // RectifyImageOps.allInsideLeft(origLeft.getWidth(),origLeft.getHeight(), rect1, rect2 );
    // 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<GrayF32, GrayF32> imageDistortLeft = RectifyImageOps.rectifyImage(rect1_F32, BorderType.SKIP, GrayF32.class);
    ImageDistort<GrayF32, GrayF32> imageDistortRight = RectifyImageOps.rectifyImage(rect2_F32, BorderType.SKIP, GrayF32.class);
    DistortImageOps.distortPL(unrectLeft, rectLeft, imageDistortLeft);
    DistortImageOps.distortPL(unrectRight, rectRight, imageDistortRight);
    // 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
    // Don't worry if the image appears upside down
    ShowImages.showWindow(new RectifiedPairPanel(true, outLeft, outRight), "Rectified");
}
Also used : RectifyFundamental(boofcv.alg.geo.rectify.RectifyFundamental) FMatrixRMaj(org.ejml.data.FMatrixRMaj) GrayF32(boofcv.struct.image.GrayF32) DMatrixRMaj(org.ejml.data.DMatrixRMaj) RectifiedPairPanel(boofcv.gui.stereo.RectifiedPairPanel) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Aggregations

RectifyFundamental (boofcv.alg.geo.rectify.RectifyFundamental)1 RectifiedPairPanel (boofcv.gui.stereo.RectifiedPairPanel)1 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)1 GrayF32 (boofcv.struct.image.GrayF32)1 BufferedImage (java.awt.image.BufferedImage)1 DMatrixRMaj (org.ejml.data.DMatrixRMaj)1 FMatrixRMaj (org.ejml.data.FMatrixRMaj)1