Search in sources :

Example 11 with Point2Transform2_F32

use of boofcv.struct.distort.Point2Transform2_F32 in project BoofCV by lessthanoptimal.

the class LensDistortionOps method transformChangeModel_F32.

/**
 * Creates a {@link Point2Transform2_F32} for converting pixels from original camera model into a new synthetic
 * model.  The scaling of the image can be adjusted to ensure certain visibility requirements.
 *
 * @param type The type of adjustment it will apply to the transform
 * @param paramOriginal Camera model for the current image
 * @param paramDesired Desired camera model for the distorted image
 * @param desiredToOriginal If true then the transform's input is assumed to be pixels in the desired
 *                       image and the output will be in original image, if false then the reverse transform
 *                       is returned.
 * @param paramMod The modified camera model to meet the requested visibility requirements.  Null if you don't want it.
 * @return The requested transform
 */
public static <O extends CameraPinhole, D extends CameraPinhole> Point2Transform2_F32 transformChangeModel_F32(AdjustmentType type, O paramOriginal, D paramDesired, boolean desiredToOriginal, D paramMod) {
    LensDistortionNarrowFOV original = LensDistortionOps.narrow(paramOriginal);
    LensDistortionNarrowFOV desired = LensDistortionOps.narrow(paramDesired);
    Point2Transform2_F32 ori_p_to_n = original.undistort_F32(true, false);
    Point2Transform2_F32 des_n_to_p = desired.distort_F32(false, true);
    Point2Transform2_F32 ori_to_des = new SequencePoint2Transform2_F32(ori_p_to_n, des_n_to_p);
    RectangleLength2D_F32 bound;
    if (type == AdjustmentType.FULL_VIEW) {
        bound = DistortImageOps.boundBox_F32(paramOriginal.width, paramOriginal.height, new PointToPixelTransform_F32(ori_to_des));
    } else if (type == AdjustmentType.EXPAND) {
        bound = LensDistortionOps.boundBoxInside(paramOriginal.width, paramOriginal.height, new PointToPixelTransform_F32(ori_to_des));
        // ensure there are no strips of black
        LensDistortionOps.roundInside(bound);
    } else if (type == AdjustmentType.NONE) {
        bound = new RectangleLength2D_F32(0, 0, paramDesired.width, paramDesired.height);
    } else {
        throw new IllegalArgumentException("Unsupported type " + type);
    }
    float scaleX = bound.width / paramDesired.width;
    float scaleY = bound.height / paramDesired.height;
    float scale;
    if (type == AdjustmentType.FULL_VIEW) {
        scale = Math.max(scaleX, scaleY);
    } else if (type == AdjustmentType.EXPAND) {
        scale = Math.min(scaleX, scaleY);
    } else {
        scale = 1.0f;
    }
    float deltaX = (float) (bound.x0 + (scaleX - scale) * paramDesired.width / 2.0);
    float deltaY = (float) (bound.y0 + (scaleY - scale) * paramDesired.height / 2.0);
    // adjustment matrix
    FMatrixRMaj A = new FMatrixRMaj(3, 3, true, scale, 0, deltaX, 0, scale, deltaY, 0, 0, 1);
    FMatrixRMaj A_inv = new FMatrixRMaj(3, 3);
    if (!CommonOps_FDRM.invert(A, A_inv)) {
        throw new RuntimeException("Failed to invert adjustment matrix.  Probably bad.");
    }
    if (paramMod != null) {
        PerspectiveOps.adjustIntrinsic(paramDesired, A_inv, paramMod);
    }
    if (desiredToOriginal) {
        Point2Transform2_F32 des_p_to_n = desired.undistort_F32(true, false);
        Point2Transform2_F32 ori_n_to_p = original.distort_F32(false, true);
        PointTransformHomography_F32 adjust = new PointTransformHomography_F32(A);
        return new SequencePoint2Transform2_F32(adjust, des_p_to_n, ori_n_to_p);
    } else {
        PointTransformHomography_F32 adjust = new PointTransformHomography_F32(A_inv);
        return new SequencePoint2Transform2_F32(ori_to_des, adjust);
    }
}
Also used : FMatrixRMaj(org.ejml.data.FMatrixRMaj) RectangleLength2D_F32(georegression.struct.shapes.RectangleLength2D_F32) SequencePoint2Transform2_F32(boofcv.struct.distort.SequencePoint2Transform2_F32) Point2Transform2_F32(boofcv.struct.distort.Point2Transform2_F32) SequencePoint2Transform2_F32(boofcv.struct.distort.SequencePoint2Transform2_F32)

Example 12 with Point2Transform2_F32

use of boofcv.struct.distort.Point2Transform2_F32 in project BoofCV by lessthanoptimal.

the class LensDistortionOps method changeCameraModel.

/**
 * Creates a distortion for modifying the input image from one camera model into another camera model.  If
 * requested the camera model can be further modified to ensure certain visibility requirements are meet
 * and the adjusted camera model will be returned.
 * @param type How it should modify the image model to ensure visibility of pixels.
 * @param borderType How the image border is handled
 * @param original The original camera model
 * @param desired The desired camera model
 * @param modified (Optional) The desired camera model after being rescaled.  Can be null.
 * @param imageType Type of image.
 * @return Image distortion from original camera model to the modified one.
 */
public static <T extends ImageBase<T>, O extends CameraPinhole, D extends CameraPinhole> ImageDistort<T, T> changeCameraModel(AdjustmentType type, BorderType borderType, O original, D desired, D modified, ImageType<T> imageType) {
    Class bandType = imageType.getImageClass();
    boolean skip = borderType == BorderType.SKIP;
    // it has to process the border at some point, so if skip is requested just skip stuff truly outside the image
    if (skip)
        borderType = BorderType.EXTENDED;
    InterpolatePixelS interp = FactoryInterpolation.createPixelS(0, 255, InterpolationType.BILINEAR, borderType, bandType);
    Point2Transform2_F32 undistToDist = transformChangeModel_F32(type, original, desired, true, modified);
    ImageDistort<T, T> distort = FactoryDistort.distort(true, interp, imageType);
    distort.setModel(new PointToPixelTransform_F32(undistToDist));
    distort.setRenderAll(!skip);
    return distort;
}
Also used : InterpolatePixelS(boofcv.alg.interpolate.InterpolatePixelS) SequencePoint2Transform2_F32(boofcv.struct.distort.SequencePoint2Transform2_F32) Point2Transform2_F32(boofcv.struct.distort.Point2Transform2_F32)

Example 13 with Point2Transform2_F32

use of boofcv.struct.distort.Point2Transform2_F32 in project BoofCV by lessthanoptimal.

the class TestBaseDetectFiducialSquare method detectWithLensDistortion.

private void detectWithLensDistortion(List<Point2D_F64> expected, DetectCorner detector, CameraPinholeRadial intrinsic) {
    // create a pattern with a corner for orientation and put it into the image
    GrayU8 pattern = createPattern(6 * 20, true);
    GrayU8 image = new GrayU8(width, height);
    ImageMiscOps.fill(image, 255);
    image.subimage(60, 300, 60 + pattern.width, 300 + pattern.height, null).setTo(pattern);
    // place the pattern right next to one of the corners to maximize distortion
    // add lens distortion
    Point2Transform2_F32 distToUndistort = LensDistortionOps.narrow(intrinsic).undistort_F32(true, true);
    Point2Transform2_F64 undistTodist = LensDistortionOps.narrow(intrinsic).distort_F64(true, true);
    InterpolatePixelS interp = FactoryInterpolation.createPixelS(0, 255, InterpolationType.BILINEAR, BorderType.ZERO, GrayU8.class);
    ImageDistort<GrayU8, GrayU8> distorter = FactoryDistort.distortSB(false, interp, GrayU8.class);
    distorter.setModel(new PointToPixelTransform_F32(distToUndistort));
    GrayU8 distorted = new GrayU8(width, height);
    distorter.apply(image, distorted);
    detector.configure(new LensDistortionRadialTangential(intrinsic), width, height, false);
    detector.process(distorted);
    assertEquals(1, detector.getFound().size());
    FoundFiducial ff = detector.getFound().get(0);
    // see if the returned corners
    Point2D_F64 expectedDist = new Point2D_F64();
    for (int j = 0; j < 4; j++) {
        Point2D_F64 f = ff.distortedPixels.get(j);
        Point2D_F64 e = expected.get((j + 1) % 4);
        undistTodist.compute(e.x, e.y, expectedDist);
        assertTrue(f.distance(expectedDist) <= 0.4);
    }
// The check to see if square is correctly undistorted is inside the processing function itself
}
Also used : InterpolatePixelS(boofcv.alg.interpolate.InterpolatePixelS) LensDistortionRadialTangential(boofcv.alg.distort.radtan.LensDistortionRadialTangential) Point2D_F64(georegression.struct.point.Point2D_F64) Point2Transform2_F64(boofcv.struct.distort.Point2Transform2_F64) GrayU8(boofcv.struct.image.GrayU8) Point2Transform2_F32(boofcv.struct.distort.Point2Transform2_F32)

Example 14 with Point2Transform2_F32

use of boofcv.struct.distort.Point2Transform2_F32 in project BoofCV by lessthanoptimal.

the class RemoveLensDistortionApp method processImage.

@Override
public void processImage(int sourceID, long frameID, final BufferedImage buffered, ImageBase input) {
    // strip away distortion parameters
    CameraPinhole desired = new CameraPinhole(param);
    // distorted image
    dist = (T) input.clone();
    // storage for undistorted image
    undist = (T) input.createSameShape();
    // 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 ImagePanel(buffered), "Original");
        }
    });
    // add different types of adjustments
    Point2Transform2_F32 add_p_to_p = LensDistortionOps.transformChangeModel_F32(AdjustmentType.NONE, param, desired, true, null);
    addUndistorted("No Adjustment", add_p_to_p);
    Point2Transform2_F32 expand = LensDistortionOps.transformChangeModel_F32(AdjustmentType.EXPAND, param, desired, true, null);
    addUndistorted("Expand", expand);
    Point2Transform2_F32 fullView = LensDistortionOps.transformChangeModel_F32(AdjustmentType.FULL_VIEW, param, desired, true, null);
    addUndistorted("Full View", fullView);
}
Also used : Point2Transform2_F32(boofcv.struct.distort.Point2Transform2_F32) CameraPinhole(boofcv.struct.calib.CameraPinhole) ImagePanel(boofcv.gui.image.ImagePanel)

Example 15 with Point2Transform2_F32

use of boofcv.struct.distort.Point2Transform2_F32 in project BoofCV by lessthanoptimal.

the class RenderSyntheticCamerModelApp method updatedPinholeModel.

@Override
public synchronized void updatedPinholeModel(CameraPinholeRadial desired) {
    if (undist.width != desired.width || undist.height != desired.height) {
        undist.reshape(desired.width, desired.height);
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                gui.setPreferredSize(new Dimension(undist.width, undist.height));
            // gui.invalidate();
            }
        });
    }
    Point2Transform2_F32 add_p_to_p = LensDistortionOps.transformChangeModel_F32(adjustment, origModel, desired, true, null);
    undistorter.setModel(new PointToPixelTransform_F32(add_p_to_p));
    if (inputMethod == InputMethod.IMAGE)
        renderCameraModel();
}
Also used : PointToPixelTransform_F32(boofcv.alg.distort.PointToPixelTransform_F32) Point2Transform2_F32(boofcv.struct.distort.Point2Transform2_F32)

Aggregations

Point2Transform2_F32 (boofcv.struct.distort.Point2Transform2_F32)26 CameraPinholeRadial (boofcv.struct.calib.CameraPinholeRadial)10 SequencePoint2Transform2_F32 (boofcv.struct.distort.SequencePoint2Transform2_F32)9 Point2D_F32 (georegression.struct.point.Point2D_F32)9 Test (org.junit.Test)9 CameraPinhole (boofcv.struct.calib.CameraPinhole)7 PointToPixelTransform_F32 (boofcv.alg.distort.PointToPixelTransform_F32)5 FMatrixRMaj (org.ejml.data.FMatrixRMaj)5 PointTransformHomography_F32 (boofcv.alg.distort.PointTransformHomography_F32)4 RectangleLength2D_F32 (georegression.struct.shapes.RectangleLength2D_F32)4 LensDistortionRadialTangential (boofcv.alg.distort.radtan.LensDistortionRadialTangential)2 InterpolatePixelS (boofcv.alg.interpolate.InterpolatePixelS)2 Point2D_F64 (georegression.struct.point.Point2D_F64)2 PixelTransformCached_F32 (boofcv.alg.distort.PixelTransformCached_F32)1 PinholePtoN_F32 (boofcv.alg.distort.pinhole.PinholePtoN_F32)1 ImagePanel (boofcv.gui.image.ImagePanel)1 PixelTransform2_F32 (boofcv.struct.distort.PixelTransform2_F32)1 Point2Transform2_F64 (boofcv.struct.distort.Point2Transform2_F64)1 GrayU8 (boofcv.struct.image.GrayU8)1 Point3D_F32 (georegression.struct.point.Point3D_F32)1