use of org.ejml.data.FMatrixRMaj in project BoofCV by lessthanoptimal.
the class TestAddRadialPtoN_F32 method againstManual.
public void againstManual(float t1, float t2) {
float fx = 600;
float fy = 500;
float skew = 2;
float xc = 300;
float yc = 350;
/**/
double[] radial = new /**/
double[] { 0.01, -0.03 };
// undistorted pixel coordinates
Point2D_F32 orig = new Point2D_F32(19.5f, 400.1f);
Point2D_F32 normPt = new Point2D_F32();
FMatrixRMaj K = new FMatrixRMaj(3, 3, true, fx, skew, xc, 0, fy, yc, 0, 0, 1);
FMatrixRMaj K_inv = new FMatrixRMaj(3, 3);
CommonOps_FDRM.invert(K, K_inv);
// compute normalized image coordinate
GeometryMath_F32.mult(K_inv, orig, normPt);
// undistorted normalized image coordinates
float nx = normPt.x;
float ny = normPt.y;
float r2 = nx * nx + ny * ny;
float ri2 = 1;
float sum = 0;
for (int i = 0; i < radial.length; i++) {
ri2 *= r2;
sum += radial[i] * ri2;
}
// distorted normalized image coordinates
float dnx = nx + nx * sum + 2 * t1 * nx * ny + t2 * (r2 + 2 * nx * nx);
float dny = ny + ny * sum + t1 * (r2 + 2 * ny * ny) + 2 * t2 * nx * ny;
AddRadialPtoN_F32 alg = new AddRadialPtoN_F32().setK(fx, fy, skew, xc, yc).setDistortion(radial, t1, t2);
Point2D_F32 found = new Point2D_F32();
alg.compute(orig.x, orig.y, found);
assertEquals(dnx, found.x, 1e-4);
assertEquals(dny, found.y, 1e-4);
}
use of org.ejml.data.FMatrixRMaj in project BoofCV by lessthanoptimal.
the class RectifyImageOps method rectifyImage.
/**
* Creates an {@link ImageDistort} for rectifying an image given its rectification matrix.
* Lens distortion is assumed to have been previously removed.
*
* @param rectify Transform for rectifying the image.
* @param imageType Type of single band image the transform is to be applied to.
* @return ImageDistort for rectifying the image.
*/
public static <T extends ImageGray<T>> ImageDistort<T, T> rectifyImage(FMatrixRMaj rectify, BorderType borderType, Class<T> imageType) {
boolean skip = borderType == BorderType.SKIP;
if (skip) {
borderType = BorderType.EXTENDED;
}
InterpolatePixelS<T> interp = FactoryInterpolation.bilinearPixelS(imageType, borderType);
FMatrixRMaj rectifyInv = new FMatrixRMaj(3, 3);
CommonOps_FDRM.invert(rectify, rectifyInv);
PointTransformHomography_F32 rectifyTran = new PointTransformHomography_F32(rectifyInv);
// don't bother caching the results since it is likely to only be applied once and is cheap to compute
ImageDistort<T, T> ret = FactoryDistort.distortSB(false, interp, imageType);
ret.setRenderAll(!skip);
ret.setModel(new PointToPixelTransform_F32(rectifyTran));
return ret;
}
use of org.ejml.data.FMatrixRMaj 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");
}
use of org.ejml.data.FMatrixRMaj in project BoofCV by lessthanoptimal.
the class ExampleStereoTwoViewsOneCamera method rectifyImages.
/**
* Remove lens distortion and rectify stereo images
*
* @param distortedLeft Input distorted image from left camera.
* @param distortedRight Input distorted image from right camera.
* @param leftToRight Camera motion from left to right
* @param intrinsic Intrinsic camera parameters
* @param rectifiedLeft Output rectified image for left camera.
* @param rectifiedRight Output rectified image for right camera.
* @param rectifiedK Output camera calibration matrix for rectified camera
*/
public static void rectifyImages(GrayU8 distortedLeft, GrayU8 distortedRight, Se3_F64 leftToRight, CameraPinholeRadial intrinsic, GrayU8 rectifiedLeft, GrayU8 rectifiedRight, DMatrixRMaj rectifiedK) {
RectifyCalibrated rectifyAlg = RectifyImageOps.createCalibrated();
// original camera calibration matrices
DMatrixRMaj K = PerspectiveOps.calibrationMatrix(intrinsic, (DMatrixRMaj) null);
rectifyAlg.process(K, new Se3_F64(), K, leftToRight);
// rectification matrix for each image
DMatrixRMaj rect1 = rectifyAlg.getRect1();
DMatrixRMaj rect2 = rectifyAlg.getRect2();
// New calibration matrix,
rectifiedK.set(rectifyAlg.getCalibrationMatrix());
// Adjust the rectification to make the view area more useful
RectifyImageOps.allInsideLeft(intrinsic, rect1, rect2, rectifiedK);
// undistorted and rectify images
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<GrayU8, GrayU8> distortLeft = RectifyImageOps.rectifyImage(intrinsic, rect1_F32, BorderType.SKIP, distortedLeft.getImageType());
ImageDistort<GrayU8, GrayU8> distortRight = RectifyImageOps.rectifyImage(intrinsic, rect2_F32, BorderType.SKIP, distortedRight.getImageType());
distortLeft.apply(distortedLeft, rectifiedLeft);
distortRight.apply(distortedRight, rectifiedRight);
}
use of org.ejml.data.FMatrixRMaj in project BoofCV by lessthanoptimal.
the class TestBaseDetectFiducialSquare method render.
/**
* Draws a distorted pattern onto the output
*/
public static void render(GrayU8 pattern, Quadrilateral_F64 where, GrayU8 output) {
int w = pattern.width;
int h = pattern.height;
ArrayList<AssociatedPair> associatedPairs = new ArrayList<>();
associatedPairs.add(new AssociatedPair(where.a, new Point2D_F64(0, 0)));
associatedPairs.add(new AssociatedPair(where.b, new Point2D_F64(w, 0)));
associatedPairs.add(new AssociatedPair(where.c, new Point2D_F64(w, h)));
associatedPairs.add(new AssociatedPair(where.d, new Point2D_F64(0, h)));
Estimate1ofEpipolar computeHomography = FactoryMultiView.computeHomographyDLT(true);
DMatrixRMaj H = new DMatrixRMaj(3, 3);
computeHomography.process(associatedPairs, H);
// Create the transform for distorting the image
FMatrixRMaj H32 = new FMatrixRMaj(3, 3);
ConvertMatrixData.convert(H, H32);
PointTransformHomography_F32 homography = new PointTransformHomography_F32(H32);
PixelTransform2_F32 pixelTransform = new PointToPixelTransform_F32(homography);
// Apply distortion and show the results
DistortImageOps.distortSingle(pattern, output, pixelTransform, InterpolationType.BILINEAR, BorderType.SKIP);
// ShowImages.showWindow(output, "Rendered");
// try {Thread.sleep(10000);} catch (InterruptedException e) {}
}
Aggregations