Search in sources :

Example 1 with ColorizeMultiViewStereoResults

use of boofcv.alg.mvs.ColorizeMultiViewStereoResults in project BoofCV by lessthanoptimal.

the class ExampleMultiViewSparseReconstruction method visualizeSparseCloud.

/**
 * To visualize the results we will render a sparse point cloud along with the location of each camera in the
 * scene.
 */
public void visualizeSparseCloud() {
    checkTrue(scene.isHomogenous());
    List<Point3D_F64> cloudXyz = new ArrayList<>();
    Point4D_F64 world = new Point4D_F64();
    // NOTE: By default the colors found below are not used. Look before to see why and how to turn them on.
    // 
    // Colorize the cloud by reprojecting the images. The math is straight forward but there's a lot of book
    // keeping that needs to be done due to the scene data structure. A class is provided to make this process easy
    var imageLookup = new LookUpImageFilesByIndex(imageFiles);
    var colorize = new ColorizeMultiViewStereoResults<>(new LookUpColorRgbFormats.PL_U8(), imageLookup);
    DogArray_I32 rgb = new DogArray_I32();
    rgb.resize(scene.points.size);
    colorize.processScenePoints(scene, // String encodes the image's index
    (viewIdx) -> viewIdx + "", // Assign the RGB color
    (pointIdx, r, g, b) -> rgb.set(pointIdx, (r << 16) | (g << 8) | b));
    // Convert the structure into regular 3D points from homogenous
    for (int i = 0; i < scene.points.size; i++) {
        scene.points.get(i).get(world);
        // array would be out of sync. Let's just throw it far far away then.
        if (world.w == 0.0)
            cloudXyz.add(new Point3D_F64(0, 0, Double.MAX_VALUE));
        else
            cloudXyz.add(new Point3D_F64(world.x / world.w, world.y / world.w, world.z / world.w));
    }
    PointCloudViewer viewer = VisualizeData.createPointCloudViewer();
    viewer.setFog(true);
    // We just did a bunch of work to look up the true color of points, however for sparse data it's easy to see
    // the structure with psuedo color. Comment out the line below to see the true color.
    viewer.setColorizer(new TwoAxisRgbPlane.Z_XY(1.0).fperiod(40));
    viewer.setDotSize(1);
    viewer.setTranslationStep(0.15);
    viewer.addCloud((idx, p) -> p.setTo(cloudXyz.get(idx)), rgb::get, rgb.size);
    viewer.setCameraHFov(UtilAngle.radian(60));
    SwingUtilities.invokeLater(() -> {
        // Show where the cameras are
        BoofSwingUtil.visualizeCameras(scene, viewer);
        // Size the window and show it to the user
        viewer.getComponent().setPreferredSize(new Dimension(600, 600));
        ShowImages.showWindow(viewer.getComponent(), "Refined Scene", true);
        var copy = new DogArray<>(Point3dRgbI_F64::new);
        viewer.copyCloud(copy);
        try (var out = new FileOutputStream("saved_cloud.ply")) {
            PointCloudIO.save3D(PointCloudIO.Format.PLY, PointCloudReader.wrapF64RGB(copy.toList()), true, out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    });
}
Also used : Point3D_F64(georegression.struct.point.Point3D_F64) LookUpColorRgbFormats(boofcv.core.image.LookUpColorRgbFormats) ArrayList(java.util.ArrayList) DogArray_I32(org.ddogleg.struct.DogArray_I32) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) DogArray(org.ddogleg.struct.DogArray) ColorizeMultiViewStereoResults(boofcv.alg.mvs.ColorizeMultiViewStereoResults) FileOutputStream(java.io.FileOutputStream) PointCloudViewer(boofcv.visualize.PointCloudViewer) Point3dRgbI_F64(boofcv.struct.Point3dRgbI_F64) Point4D_F64(georegression.struct.point.Point4D_F64) LookUpImageFilesByIndex(boofcv.io.image.LookUpImageFilesByIndex)

Aggregations

ColorizeMultiViewStereoResults (boofcv.alg.mvs.ColorizeMultiViewStereoResults)1 LookUpColorRgbFormats (boofcv.core.image.LookUpColorRgbFormats)1 LookUpImageFilesByIndex (boofcv.io.image.LookUpImageFilesByIndex)1 Point3dRgbI_F64 (boofcv.struct.Point3dRgbI_F64)1 PointCloudViewer (boofcv.visualize.PointCloudViewer)1 Point3D_F64 (georegression.struct.point.Point3D_F64)1 Point4D_F64 (georegression.struct.point.Point4D_F64)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 UncheckedIOException (java.io.UncheckedIOException)1 ArrayList (java.util.ArrayList)1 DogArray (org.ddogleg.struct.DogArray)1 DogArray_I32 (org.ddogleg.struct.DogArray_I32)1