Search in sources :

Example 1 with RectifiedPairPanel

use of boofcv.gui.stereo.RectifiedPairPanel in project BoofCV by lessthanoptimal.

the class ShowRectifyCalibratedApp method addRectified.

private void addRectified(final String name, final DMatrixRMaj rect1, final DMatrixRMaj rect2) {
    // 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);
    // Will rectify the image
    ImageType<GrayF32> imageType = ImageType.single(GrayF32.class);
    ImageDistort<GrayF32, GrayF32> imageDistortLeft = RectifyImageOps.rectifyImage(param.getLeft(), rect1_F32, BorderType.ZERO, imageType);
    ImageDistort<GrayF32, GrayF32> imageDistortRight = RectifyImageOps.rectifyImage(param.getRight(), rect2_F32, BorderType.ZERO, imageType);
    // Fill the image with all black
    GImageMiscOps.fill(rectLeft, 0);
    GImageMiscOps.fill(rectRight, 0);
    // Render the rectified image
    DistortImageOps.distortPL(distLeft, rectLeft, imageDistortLeft);
    DistortImageOps.distortPL(distRight, rectRight, imageDistortRight);
    // convert for output
    final BufferedImage outLeft = ConvertBufferedImage.convertTo(rectLeft, null, true);
    final BufferedImage outRight = ConvertBufferedImage.convertTo(rectRight, null, true);
    // Add this rectified image
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            gui.addItem(new RectifiedPairPanel(true, outLeft, outRight), name);
        }
    });
}
Also used : FMatrixRMaj(org.ejml.data.FMatrixRMaj) GrayF32(boofcv.struct.image.GrayF32) RectifiedPairPanel(boofcv.gui.stereo.RectifiedPairPanel) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Example 2 with RectifiedPairPanel

use of boofcv.gui.stereo.RectifiedPairPanel 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 RectifiedPairPanel

use of boofcv.gui.stereo.RectifiedPairPanel 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)

Example 4 with RectifiedPairPanel

use of boofcv.gui.stereo.RectifiedPairPanel in project BoofCV by lessthanoptimal.

the class ShowRectifyCalibratedApp method configure.

public void configure(final BufferedImage origLeft, final BufferedImage origRight, StereoParameters param) {
    this.param = param;
    // distorted images
    distLeft = ConvertBufferedImage.convertFromPlanar(origLeft, null, true, GrayF32.class);
    distRight = ConvertBufferedImage.convertFromPlanar(origRight, null, true, GrayF32.class);
    // storage for undistorted + rectified images
    rectLeft = new Planar<>(GrayF32.class, distLeft.getWidth(), distLeft.getHeight(), distLeft.getNumBands());
    rectRight = new Planar<>(GrayF32.class, distRight.getWidth(), distRight.getHeight(), distRight.getNumBands());
    // 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();
    DMatrixRMaj rectK = rectifyAlg.getCalibrationMatrix();
    // show results and draw a horizontal line where the user clicks to see rectification easier
    SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            gui.reset();
            gui.addItem(new RectifiedPairPanel(true, origLeft, origRight), "Original");
        }
    });
    // add different types of adjustments
    addRectified("No Adjustment", rect1, rect2);
    RectifyImageOps.allInsideLeft(param.left, rect1, rect2, rectK);
    addRectified("All Inside", rect1, rect2);
    RectifyImageOps.fullViewLeft(param.left, rect1, rect2, rectK);
    addRectified("Full View", rect1, rect2);
    hasProcessed = true;
}
Also used : GrayF32(boofcv.struct.image.GrayF32) RectifyCalibrated(boofcv.alg.geo.rectify.RectifyCalibrated) DMatrixRMaj(org.ejml.data.DMatrixRMaj) RectifiedPairPanel(boofcv.gui.stereo.RectifiedPairPanel) Se3_F64(georegression.struct.se.Se3_F64)

Example 5 with RectifiedPairPanel

use of boofcv.gui.stereo.RectifiedPairPanel in project BoofCV by lessthanoptimal.

the class ExampleStereoTwoViewsOneCamera method main.

public static void main(String[] args) {
    // specify location of images and calibration
    String calibDir = UtilIO.pathExample("calibration/mono/Sony_DSC-HX5V_Chess/");
    String imageDir = UtilIO.pathExample("stereo/");
    // Camera parameters
    CameraPinholeRadial intrinsic = CalibrationIO.load(new File(calibDir, "intrinsic.yaml"));
    // Input images from the camera moving left to right
    BufferedImage origLeft = UtilImageIO.loadImage(imageDir, "mono_wall_01.jpg");
    BufferedImage origRight = UtilImageIO.loadImage(imageDir, "mono_wall_02.jpg");
    // Input images with lens distortion
    GrayU8 distortedLeft = ConvertBufferedImage.convertFrom(origLeft, (GrayU8) null);
    GrayU8 distortedRight = ConvertBufferedImage.convertFrom(origRight, (GrayU8) null);
    // matched features between the two images
    List<AssociatedPair> matchedFeatures = ExampleFundamentalMatrix.computeMatches(origLeft, origRight);
    // convert from pixel coordinates into normalized image coordinates
    List<AssociatedPair> matchedCalibrated = convertToNormalizedCoordinates(matchedFeatures, intrinsic);
    // Robustly estimate camera motion
    List<AssociatedPair> inliers = new ArrayList<>();
    Se3_F64 leftToRight = estimateCameraMotion(intrinsic, matchedCalibrated, inliers);
    drawInliers(origLeft, origRight, intrinsic, inliers);
    // Rectify and remove lens distortion for stereo processing
    DMatrixRMaj rectifiedK = new DMatrixRMaj(3, 3);
    GrayU8 rectifiedLeft = distortedLeft.createSameShape();
    GrayU8 rectifiedRight = distortedRight.createSameShape();
    rectifyImages(distortedLeft, distortedRight, leftToRight, intrinsic, rectifiedLeft, rectifiedRight, rectifiedK);
    // compute disparity
    StereoDisparity<GrayS16, GrayF32> disparityAlg = FactoryStereoDisparity.regionSubpixelWta(DisparityAlgorithms.RECT_FIVE, minDisparity, maxDisparity, 5, 5, 20, 1, 0.1, GrayS16.class);
    // Apply the Laplacian across the image to add extra resistance to changes in lighting or camera gain
    GrayS16 derivLeft = new GrayS16(rectifiedLeft.width, rectifiedLeft.height);
    GrayS16 derivRight = new GrayS16(rectifiedLeft.width, rectifiedLeft.height);
    LaplacianEdge.process(rectifiedLeft, derivLeft);
    LaplacianEdge.process(rectifiedRight, derivRight);
    // process and return the results
    disparityAlg.process(derivLeft, derivRight);
    GrayF32 disparity = disparityAlg.getDisparity();
    // show results
    BufferedImage visualized = VisualizeImageData.disparity(disparity, null, minDisparity, maxDisparity, 0);
    BufferedImage outLeft = ConvertBufferedImage.convertTo(rectifiedLeft, null);
    BufferedImage outRight = ConvertBufferedImage.convertTo(rectifiedRight, null);
    ShowImages.showWindow(new RectifiedPairPanel(true, outLeft, outRight), "Rectification");
    ShowImages.showWindow(visualized, "Disparity");
    showPointCloud(disparity, outLeft, leftToRight, rectifiedK, minDisparity, maxDisparity);
    System.out.println("Total found " + matchedCalibrated.size());
    System.out.println("Total Inliers " + inliers.size());
}
Also used : AssociatedPair(boofcv.struct.geo.AssociatedPair) GrayS16(boofcv.struct.image.GrayS16) ArrayList(java.util.ArrayList) DMatrixRMaj(org.ejml.data.DMatrixRMaj) RectifiedPairPanel(boofcv.gui.stereo.RectifiedPairPanel) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) GrayF32(boofcv.struct.image.GrayF32) CameraPinholeRadial(boofcv.struct.calib.CameraPinholeRadial) GrayU8(boofcv.struct.image.GrayU8) File(java.io.File) Se3_F64(georegression.struct.se.Se3_F64)

Aggregations

RectifiedPairPanel (boofcv.gui.stereo.RectifiedPairPanel)5 GrayF32 (boofcv.struct.image.GrayF32)5 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)4 BufferedImage (java.awt.image.BufferedImage)4 DMatrixRMaj (org.ejml.data.DMatrixRMaj)4 Se3_F64 (georegression.struct.se.Se3_F64)3 FMatrixRMaj (org.ejml.data.FMatrixRMaj)3 RectifyCalibrated (boofcv.alg.geo.rectify.RectifyCalibrated)2 File (java.io.File)2 ImageDistort (boofcv.alg.distort.ImageDistort)1 RectifyFundamental (boofcv.alg.geo.rectify.RectifyFundamental)1 ListDisplayPanel (boofcv.gui.ListDisplayPanel)1 CameraPinholeRadial (boofcv.struct.calib.CameraPinholeRadial)1 StereoParameters (boofcv.struct.calib.StereoParameters)1 AssociatedPair (boofcv.struct.geo.AssociatedPair)1 GrayS16 (boofcv.struct.image.GrayS16)1 GrayU8 (boofcv.struct.image.GrayU8)1 ArrayList (java.util.ArrayList)1