use of boofcv.gui.stereo.RectifiedPairPanel 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<GrayF32> imageType = ImageType.single(GrayF32.class);
ImageDistort<GrayF32, GrayF32> imageDistortLeft = RectifyImageOps.rectifyImage(param.getLeft(), rect1_F32, BorderType.ZERO, imageType);
ImageDistort<GrayF32, GrayF32> imageDistortRight = RectifyImageOps.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
DistortImageOps.distortPL(distLeft, rectLeft, imageDistortLeft);
DistortImageOps.distortPL(distRight, rectRight, imageDistortRight);
// 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(new Runnable() {
public void run() {
gui.addItem(new RectifiedPairPanel(true, outLeft, outRight), name);
}
});
}
use of boofcv.gui.stereo.RectifiedPairPanel in project BoofCV by lessthanoptimal.
the class ExampleRectifyCalibratedStereo method main.
public static void main(String[] args) {
String dir = UtilIO.pathExample("calibration/stereo/Bumblebee2_Chess/");
StereoParameters param = CalibrationIO.load(new File(dir, "stereo.yaml"));
// load images
BufferedImage origLeft = UtilImageIO.loadImage(dir, "left05.jpg");
BufferedImage origRight = UtilImageIO.loadImage(dir, "right05.jpg");
// distorted images
Planar<GrayF32> distLeft = ConvertBufferedImage.convertFromPlanar(origLeft, null, true, GrayF32.class);
Planar<GrayF32> distRight = ConvertBufferedImage.convertFromPlanar(origRight, null, true, GrayF32.class);
// storage for undistorted + rectified images
Planar<GrayF32> rectLeft = distLeft.createSameShape();
Planar<GrayF32> rectRight = distRight.createSameShape();
// Compute rectification
RectifyCalibrated rectifyAlg = RectifyImageOps.createCalibrated();
Se3_F64 leftToRight = param.getRightToLeft().invert(null);
// original camera calibration matrices
DMatrixRMaj K1 = PerspectiveOps.calibrationMatrix(param.getLeft(), (DMatrixRMaj) null);
DMatrixRMaj K2 = PerspectiveOps.calibrationMatrix(param.getRight(), (DMatrixRMaj) null);
rectifyAlg.process(K1, new Se3_F64(), K2, leftToRight);
// rectification matrix for each image
DMatrixRMaj rect1 = rectifyAlg.getRect1();
DMatrixRMaj rect2 = rectifyAlg.getRect2();
// New calibration matrix,
// Both cameras have the same one after rectification.
DMatrixRMaj rectK = rectifyAlg.getCalibrationMatrix();
// Adjust the rectification to make the view area more useful
RectifyImageOps.fullViewLeft(param.left, rect1, rect2, rectK);
// RectifyImageOps.allInsideLeft(param.left, leftHanded, rect1, rect2, rectK);
// 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 rectifyImageLeft = RectifyImageOps.rectifyImage(param.getLeft(), rect1_F32, BorderType.SKIP, distLeft.getImageType());
ImageDistort rectifyImageRight = RectifyImageOps.rectifyImage(param.getRight(), rect2_F32, BorderType.SKIP, distRight.getImageType());
rectifyImageLeft.apply(distLeft, rectLeft);
rectifyImageRight.apply(distRight, rectRight);
// 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
ListDisplayPanel panel = new ListDisplayPanel();
panel.addItem(new RectifiedPairPanel(true, origLeft, origRight), "Original");
panel.addItem(new RectifiedPairPanel(true, outLeft, outRight), "Rectified");
ShowImages.showWindow(panel, "Stereo Rectification Calibrated", true);
}
use of boofcv.gui.stereo.RectifiedPairPanel 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 boofcv.gui.stereo.RectifiedPairPanel in project BoofCV by lessthanoptimal.
the class ShowRectifyCalibratedApp method configure.
public void configure(final BufferedImage origLeft, final BufferedImage origRight, StereoParameters param) {
this.param = param;
// distorted images
distLeft = ConvertBufferedImage.convertFromPlanar(origLeft, null, true, GrayF32.class);
distRight = ConvertBufferedImage.convertFromPlanar(origRight, null, true, GrayF32.class);
// storage for undistorted + rectified images
rectLeft = new Planar<>(GrayF32.class, distLeft.getWidth(), distLeft.getHeight(), distLeft.getNumBands());
rectRight = new Planar<>(GrayF32.class, distRight.getWidth(), distRight.getHeight(), distRight.getNumBands());
// Compute rectification
RectifyCalibrated rectifyAlg = RectifyImageOps.createCalibrated();
Se3_F64 leftToRight = param.getRightToLeft().invert(null);
// original camera calibration matrices
DMatrixRMaj K1 = PerspectiveOps.calibrationMatrix(param.getLeft(), (DMatrixRMaj) null);
DMatrixRMaj K2 = PerspectiveOps.calibrationMatrix(param.getRight(), (DMatrixRMaj) null);
rectifyAlg.process(K1, new Se3_F64(), K2, leftToRight);
// rectification matrix for each image
DMatrixRMaj rect1 = rectifyAlg.getRect1();
DMatrixRMaj rect2 = rectifyAlg.getRect2();
DMatrixRMaj rectK = rectifyAlg.getCalibrationMatrix();
// 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 RectifiedPairPanel(true, origLeft, origRight), "Original");
}
});
// add different types of adjustments
addRectified("No Adjustment", rect1, rect2);
RectifyImageOps.allInsideLeft(param.left, rect1, rect2, rectK);
addRectified("All Inside", rect1, rect2);
RectifyImageOps.fullViewLeft(param.left, rect1, rect2, rectK);
addRectified("Full View", rect1, rect2);
hasProcessed = true;
}
use of boofcv.gui.stereo.RectifiedPairPanel in project BoofCV by lessthanoptimal.
the class ExampleStereoTwoViewsOneCamera method main.
public static void main(String[] args) {
// specify location of images and calibration
String calibDir = UtilIO.pathExample("calibration/mono/Sony_DSC-HX5V_Chess/");
String imageDir = UtilIO.pathExample("stereo/");
// Camera parameters
CameraPinholeRadial intrinsic = CalibrationIO.load(new File(calibDir, "intrinsic.yaml"));
// Input images from the camera moving left to right
BufferedImage origLeft = UtilImageIO.loadImage(imageDir, "mono_wall_01.jpg");
BufferedImage origRight = UtilImageIO.loadImage(imageDir, "mono_wall_02.jpg");
// Input images with lens distortion
GrayU8 distortedLeft = ConvertBufferedImage.convertFrom(origLeft, (GrayU8) null);
GrayU8 distortedRight = ConvertBufferedImage.convertFrom(origRight, (GrayU8) null);
// matched features between the two images
List<AssociatedPair> matchedFeatures = ExampleFundamentalMatrix.computeMatches(origLeft, origRight);
// convert from pixel coordinates into normalized image coordinates
List<AssociatedPair> matchedCalibrated = convertToNormalizedCoordinates(matchedFeatures, intrinsic);
// Robustly estimate camera motion
List<AssociatedPair> inliers = new ArrayList<>();
Se3_F64 leftToRight = estimateCameraMotion(intrinsic, matchedCalibrated, inliers);
drawInliers(origLeft, origRight, intrinsic, inliers);
// Rectify and remove lens distortion for stereo processing
DMatrixRMaj rectifiedK = new DMatrixRMaj(3, 3);
GrayU8 rectifiedLeft = distortedLeft.createSameShape();
GrayU8 rectifiedRight = distortedRight.createSameShape();
rectifyImages(distortedLeft, distortedRight, leftToRight, intrinsic, rectifiedLeft, rectifiedRight, rectifiedK);
// compute disparity
StereoDisparity<GrayS16, GrayF32> disparityAlg = FactoryStereoDisparity.regionSubpixelWta(DisparityAlgorithms.RECT_FIVE, minDisparity, maxDisparity, 5, 5, 20, 1, 0.1, GrayS16.class);
// Apply the Laplacian across the image to add extra resistance to changes in lighting or camera gain
GrayS16 derivLeft = new GrayS16(rectifiedLeft.width, rectifiedLeft.height);
GrayS16 derivRight = new GrayS16(rectifiedLeft.width, rectifiedLeft.height);
LaplacianEdge.process(rectifiedLeft, derivLeft);
LaplacianEdge.process(rectifiedRight, derivRight);
// process and return the results
disparityAlg.process(derivLeft, derivRight);
GrayF32 disparity = disparityAlg.getDisparity();
// show results
BufferedImage visualized = VisualizeImageData.disparity(disparity, null, minDisparity, maxDisparity, 0);
BufferedImage outLeft = ConvertBufferedImage.convertTo(rectifiedLeft, null);
BufferedImage outRight = ConvertBufferedImage.convertTo(rectifiedRight, null);
ShowImages.showWindow(new RectifiedPairPanel(true, outLeft, outRight), "Rectification");
ShowImages.showWindow(visualized, "Disparity");
showPointCloud(disparity, outLeft, leftToRight, rectifiedK, minDisparity, maxDisparity);
System.out.println("Total found " + matchedCalibrated.size());
System.out.println("Total Inliers " + inliers.size());
}
Aggregations