Search in sources :

Example 1 with LensDistortionBrown

use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.

the class VisualizeSquareBinaryFiducial method process.

public void process(String nameImage, @Nullable String nameIntrinsic) {
    CameraPinholeBrown intrinsic = nameIntrinsic == null ? null : (CameraPinholeBrown) CalibrationIO.load(nameIntrinsic);
    GrayF32 input = Objects.requireNonNull(UtilImageIO.loadImage(nameImage, GrayF32.class));
    var undistorted = new GrayF32(input.width, input.height);
    InputToBinary<GrayF32> inputToBinary = FactoryThresholdBinary.globalOtsu(0, 255, 1.0, true, GrayF32.class);
    Detector detector = new Detector(gridWidth, borderWidth, inputToBinary);
    detector.setLengthSide(0.1);
    if (intrinsic != null) {
        CameraPinholeBrown paramUndist = new CameraPinholeBrown();
        ImageDistort<GrayF32, GrayF32> undistorter = LensDistortionOps.changeCameraModel(AdjustmentType.EXPAND, BorderType.EXTENDED, intrinsic, new CameraPinhole(intrinsic), paramUndist, ImageType.single(GrayF32.class));
        detector.configure(new LensDistortionBrown(paramUndist), paramUndist.width, paramUndist.height, false);
        undistorter.apply(input, undistorted);
        detector.process(undistorted);
    } else {
        detector.process(input);
    }
    System.out.println("Total Found: " + detector.squares.size());
    DogArray<FoundFiducial> fiducials = detector.getFound();
    int N = Math.min(20, detector.squares.size());
    ListDisplayPanel squares = new ListDisplayPanel();
    for (int i = 0; i < N; i++) {
        squares.addImage(VisualizeBinaryData.renderBinary(detector.squares.get(i), false, null), " " + i);
        squares.addImage(ConvertBufferedImage.convertTo(detector.squaresGray.get(i), null), " " + i);
    }
    BufferedImage output = new BufferedImage(input.width, input.height, BufferedImage.TYPE_INT_RGB);
    ConvertBufferedImage.convertTo(input, output);
    Graphics2D g2 = output.createGraphics();
    g2.setColor(Color.RED);
    g2.setStroke(new BasicStroke(2));
    for (int i = 0; i < N; i++) {
        VisualizeShapes.drawArrowSubPixel(fiducials.get(i).distortedPixels, 3, 1, g2);
    }
    ShowImages.showWindow(output, "Binary", true);
    ShowImages.showWindow(squares, "Candidates", true);
}
Also used : ListDisplayPanel(boofcv.gui.ListDisplayPanel) CameraPinholeBrown(boofcv.struct.calib.CameraPinholeBrown) LensDistortionBrown(boofcv.alg.distort.brown.LensDistortionBrown) FoundFiducial(boofcv.alg.fiducial.square.FoundFiducial) CameraPinhole(boofcv.struct.calib.CameraPinhole) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) GrayF32(boofcv.struct.image.GrayF32) FactoryShapeDetector(boofcv.factory.shape.FactoryShapeDetector) ConfigPolygonDetector(boofcv.factory.shape.ConfigPolygonDetector)

Example 2 with LensDistortionBrown

use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.

the class FiducialDetection method process.

private void process() {
    if (detector == null) {
        System.err.println("Need to specify which fiducial you wish to detect");
        System.exit(1);
    }
    if (outputPath != null) {
        try {
            outputFile = new PrintStream(outputPath);
            outputFile.println("# Results from fiducial detection ");
            outputFile.println("# These comments should include the data source and the algorithm used, but I'm busy.");
            outputFile.println("# ");
            outputFile.println("# <frame #> <number of fiducials> <fiducial id> <X> <Y> <Z> <Q1> <Q2> <Q3> <Q4> ...");
            outputFile.println("# ");
            outputFile.println("# The special Euclidean transform saved each fiducial is from fiducial to camera");
            outputFile.println("# (X,Y,Z) is the translation and (Q1,Q2,Q3,Q4) specifies a quaternion");
            outputFile.println("# ");
        } catch (FileNotFoundException e) {
            System.err.println("Failed to open output file.");
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
    MediaManager media = DefaultMediaManager.INSTANCE;
    CameraPinholeBrown intrinsic = intrinsicPath == null ? null : (CameraPinholeBrown) CalibrationIO.load(intrinsicPath);
    SimpleImageSequence<GrayU8> sequence = null;
    long pause = 0;
    BufferedImage buffered = null;
    if (inputType == InputType.VIDEO || inputType == InputType.WEBCAM) {
        if (inputType == InputType.WEBCAM) {
            String device = getCameraDeviceString();
            sequence = Objects.requireNonNull(media.openCamera(device, desiredWidth, desiredHeight, ImageType.single(GrayU8.class)));
        } else {
            // just assume 30ms is appropriate. Should let the use specify this number
            pause = 30;
            sequence = Objects.requireNonNull(media.openVideo(filePath, ImageType.single(GrayU8.class)));
            sequence.setLoop(true);
        }
        intrinsic = handleIntrinsic(intrinsic, sequence.getWidth(), sequence.getHeight());
    } else {
        buffered = UtilImageIO.loadImage(filePath);
        if (buffered == null) {
            System.err.println("Can't find image or it can't be read. " + filePath);
            System.exit(1);
            throw new RuntimeException("Stupid null check");
        }
        intrinsic = handleIntrinsic(intrinsic, buffered.getWidth(), buffered.getHeight());
    }
    Objects.requireNonNull(intrinsic);
    Objects.requireNonNull(buffered);
    var gui = new ImagePanel();
    gui.setPreferredSize(new Dimension(intrinsic.width, intrinsic.height));
    ShowImages.showWindow(gui, "Fiducial Detector", true);
    detector.setLensDistortion(new LensDistortionBrown(intrinsic), intrinsic.width, intrinsic.height);
    if (sequence != null) {
        processStream(intrinsic, sequence, gui, pause);
    } else {
        processImage(intrinsic, buffered, gui);
    }
}
Also used : PrintStream(java.io.PrintStream) CameraPinholeBrown(boofcv.struct.calib.CameraPinholeBrown) LensDistortionBrown(boofcv.alg.distort.brown.LensDistortionBrown) FileNotFoundException(java.io.FileNotFoundException) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) MediaManager(boofcv.io.MediaManager) DefaultMediaManager(boofcv.io.wrapper.DefaultMediaManager) GrayU8(boofcv.struct.image.GrayU8) ImagePanel(boofcv.gui.image.ImagePanel)

Example 3 with LensDistortionBrown

use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.

the class CameraToEquirectangular_F64 method setCameraModel.

public void setCameraModel(CameraPinholeBrown camera) {
    Point2Transform2_F64 pixelToNormalized = new LensDistortionBrown(camera).undistort_F64(true, false);
    setCameraModel(camera.width, camera.height, pixelToNormalized);
}
Also used : LensDistortionBrown(boofcv.alg.distort.brown.LensDistortionBrown) Point2Transform2_F64(boofcv.struct.distort.Point2Transform2_F64)

Example 4 with LensDistortionBrown

use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.

the class BundleToRectificationStereoParameters method setView1.

/**
 * Specifies lens parameters for view-1. This is done independently since often the same view is compared against
 * multiple other views
 */
public void setView1(BundleAdjustmentCamera bundle1, int width, int height) {
    BoofMiscOps.checkTrue(width > 0);
    BoofMiscOps.checkTrue(height > 0);
    BundleAdjustmentOps.convert(bundle1, width, height, intrinsic1);
    PerspectiveOps.pinholeToMatrix(intrinsic1, K1);
    intrinsic1.width = width;
    intrinsic1.height = height;
    Point2Transform2_F64 p_to_p = new LensDistortionBrown(intrinsic1).undistort_F64(true, true);
    view1_dist_to_undist = new PointToPixelTransform_F64(p_to_p);
}
Also used : LensDistortionBrown(boofcv.alg.distort.brown.LensDistortionBrown) Point2Transform2_F64(boofcv.struct.distort.Point2Transform2_F64) PointToPixelTransform_F64(boofcv.struct.distort.PointToPixelTransform_F64)

Example 5 with LensDistortionBrown

use of boofcv.alg.distort.brown.LensDistortionBrown in project BoofCV by lessthanoptimal.

the class GenericFiducialDetectorChecks method checkCenter.

@Test
void checkCenter() {
    // It's not specified if the center should be undistorted or distorted. Just make it easier by
    // using undistorted
    CameraPinholeBrown intrinsic = loadDistortion(false);
    LensDistortionBrown lensDistorted = new LensDistortionBrown(intrinsic);
    for (ImageType type : types) {
        ImageBase image = renderImage(intrinsic, type);
        FiducialDetector detector = createDetector(type);
        detector.setLensDistortion(lensDistorted, image.width, image.height);
        // ShowImages.showBlocking(image, "asdfasdf", 5_000);
        detect(detector, image);
        assertTrue(detector.totalFound() >= 1);
        assertTrue(detector.is3D());
        for (int i = 0; i < detector.totalFound(); i++) {
            Se3_F64 fidToCam = new Se3_F64();
            Point2D_F64 found = new Point2D_F64();
            detector.getFiducialToCamera(i, fidToCam);
            detector.getCenter(i, found);
            Point2D_F64 rendered = new Point2D_F64();
            WorldToCameraToPixel worldToPixel = PerspectiveOps.createWorldToPixel(lensDistorted, fidToCam);
            worldToPixel.transform(new Point3D_F64(0, 0, 0), rendered);
            // see if the reprojected is near the pixel location
            assertEquals(0.0, rendered.distance(found), pixelAndProjectedTol);
        }
    }
}
Also used : Point3D_F64(georegression.struct.point.Point3D_F64) CameraPinholeBrown(boofcv.struct.calib.CameraPinholeBrown) Point2D_F64(georegression.struct.point.Point2D_F64) LensDistortionBrown(boofcv.alg.distort.brown.LensDistortionBrown) WorldToCameraToPixel(boofcv.alg.geo.WorldToCameraToPixel) ImageBase(boofcv.struct.image.ImageBase) ImageType(boofcv.struct.image.ImageType) Se3_F64(georegression.struct.se.Se3_F64) Test(org.junit.jupiter.api.Test)

Aggregations

LensDistortionBrown (boofcv.alg.distort.brown.LensDistortionBrown)25 CameraPinholeBrown (boofcv.struct.calib.CameraPinholeBrown)14 Se3_F64 (georegression.struct.se.Se3_F64)13 Test (org.junit.jupiter.api.Test)10 ImageBase (boofcv.struct.image.ImageBase)9 ImageType (boofcv.struct.image.ImageType)9 Point2Transform2_F64 (boofcv.struct.distort.Point2Transform2_F64)8 GrayF32 (boofcv.struct.image.GrayF32)8 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)7 BufferedImage (java.awt.image.BufferedImage)7 Point2D_F64 (georegression.struct.point.Point2D_F64)6 LensDistortionNarrowFOV (boofcv.alg.distort.LensDistortionNarrowFOV)4 ListDisplayPanel (boofcv.gui.ListDisplayPanel)4 Point3D_F64 (georegression.struct.point.Point3D_F64)4 PointToPixelTransform_F64 (boofcv.struct.distort.PointToPixelTransform_F64)3 GrayU8 (boofcv.struct.image.GrayU8)3 Polygon2D_F64 (georegression.struct.shapes.Polygon2D_F64)3 File (java.io.File)3 FoundFiducial (boofcv.alg.fiducial.square.FoundFiducial)2 ConfigPolygonDetector (boofcv.factory.shape.ConfigPolygonDetector)2