use of org.ejml.data.FMatrixRMaj 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<Planar<GrayF32>> imageType = ImageType.pl(3, GrayF32.class);
ImageDistort<Planar<GrayF32>, Planar<GrayF32>> imageDistortLeft = RectifyDistortImageOps.rectifyImage(param.getLeft(), rect1_F32, BorderType.ZERO, imageType);
// ImageDistort<Planar<GrayF32>, Planar<GrayF32>> imageDistortRight =
// RectifyDistortImageOps.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
imageDistortLeft.apply(distLeft, rectLeft);
imageDistortLeft.apply(distRight, rectRight);
// 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(() -> gui.addItem(new RectifiedPairPanel(true, outLeft, outRight), name));
}
use of org.ejml.data.FMatrixRMaj in project BoofCV by lessthanoptimal.
the class VisualizeStereoDisparity method rectifyInputImages.
/**
* Removes distortion and rectifies images.
*/
private void rectifyInputImages() {
// Check to see if the input images have already been recitified
rectifiedImages = calib.isRectified(1e-3);
// get intrinsic camera calibration matrices
DMatrixRMaj K1 = PerspectiveOps.pinholeToMatrix(calib.left, (DMatrixRMaj) null);
DMatrixRMaj K2 = PerspectiveOps.pinholeToMatrix(calib.right, (DMatrixRMaj) null);
// and could add a little bit of noise
if (rectifiedImages) {
rectLeft.setTo(inputLeft);
rectRight.setTo(inputRight);
leftRectToPixel = new DoNothing2Transform2_F64();
rectK = K1;
rectR = CommonOps_DDRM.identity(3);
return;
}
// compute rectification matrices
rectifyAlg.process(K1, new Se3_F64(), K2, calib.getRightToLeft().invert(null));
DMatrixRMaj rect1 = rectifyAlg.getUndistToRectPixels1();
DMatrixRMaj rect2 = rectifyAlg.getUndistToRectPixels2();
rectK = rectifyAlg.getCalibrationMatrix();
rectR = rectifyAlg.getRectifiedRotation();
// adjust view to maximize viewing area while not including black regions
RectifyImageOps.allInsideLeft(calib.left, rect1, rect2, rectK, null);
// compute transforms to apply rectify the images
leftRectToPixel = transformRectToPixel(calib.left, rect1);
ImageType<T> imageType = activeAlg.getInputType();
// 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<T, T> distortRect1 = RectifyDistortImageOps.rectifyImage(calib.left, rect1_F32, BorderType.EXTENDED, imageType);
ImageDistort<T, T> distortRect2 = RectifyDistortImageOps.rectifyImage(calib.right, rect2_F32, BorderType.EXTENDED, imageType);
// rectify and undo distortion
distortRect1.apply(inputLeft, rectLeft);
distortRect2.apply(inputRight, rectRight);
rectifiedImages = true;
}
use of org.ejml.data.FMatrixRMaj in project BoofCV by lessthanoptimal.
the class DisplayPinholeCalibrationPanel method setCalibration.
public void setCalibration(CameraPinholeBrown param, DMatrixRMaj rect) {
FMatrixRMaj rect_f32 = new FMatrixRMaj(3, 3);
ConvertMatrixData.convert(rect, rect_f32);
this.undoRadial = RectifyDistortImageOps.rectifyImage(param, rect_f32, BorderType.ZERO, ImageType.single(GrayF32.class));
this.remove_p_to_p = RectifyImageOps.transformPixelToRect(param, rect_f32);
}
use of org.ejml.data.FMatrixRMaj 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);
}
}
use of org.ejml.data.FMatrixRMaj in project BoofCV by lessthanoptimal.
the class DisplayPinholeCalibrationPanel method setCalibration.
public void setCalibration(CameraPinholeRadial param, DMatrixRMaj rect) {
FMatrixRMaj rect_f32 = new FMatrixRMaj(3, 3);
ConvertMatrixData.convert(rect, rect_f32);
this.undoRadial = RectifyImageOps.rectifyImage(param, rect_f32, BorderType.ZERO, ImageType.single(GrayF32.class));
this.remove_p_to_p = RectifyImageOps.transformPixelToRect(param, rect_f32);
}
Aggregations