Difference between revisions of "Example Stereo Single Camera"
From BoofCV
Jump to navigationJump to searchm |
m |
||
Line 22: | Line 22: | ||
* Convert into 3D point cloud | * Convert into 3D point cloud | ||
Example File: [https://github.com/lessthanoptimal/BoofCV/blob/v0. | Example File: | ||
* [https://github.com/lessthanoptimal/BoofCV/blob/v0.19/examples/src/boofcv/examples/stereo/ExampleStereoTwoViewsOneCamera.java ExampleStereoTwoViewsOneCamera.java] | |||
Concepts: | Concepts: | ||
Line 87: | Line 88: | ||
// Rectify and remove lens distortion for stereo processing | // Rectify and remove lens distortion for stereo processing | ||
DenseMatrix64F rectifiedK = new DenseMatrix64F(3, 3); | DenseMatrix64F rectifiedK = new DenseMatrix64F(3, 3); | ||
ImageUInt8 rectifiedLeft = | ImageUInt8 rectifiedLeft = distortedLeft.createSameShape(); | ||
ImageUInt8 rectifiedRight = | ImageUInt8 rectifiedRight = distortedRight.createSameShape(); | ||
rectifyImages(distortedLeft, distortedRight, leftToRight, intrinsic, rectifiedLeft, rectifiedRight, rectifiedK); | rectifyImages(distortedLeft, distortedRight, leftToRight, intrinsic, rectifiedLeft, rectifiedRight, rectifiedK); | ||
Line 133: | Line 134: | ||
List<AssociatedPair> matchedNorm, List<AssociatedPair> inliers) | List<AssociatedPair> matchedNorm, List<AssociatedPair> inliers) | ||
{ | { | ||
ModelMatcher<Se3_F64, AssociatedPair> epipolarMotion = | ModelMatcher<Se3_F64, AssociatedPair> epipolarMotion = | ||
new | FactoryMultiViewRobust.essentialRansac(new ConfigEssential(intrinsic),new ConfigRansac(200,0.5)); | ||
if (!epipolarMotion.process(matchedNorm)) | if (!epipolarMotion.process(matchedNorm)) | ||
Line 165: | Line 151: | ||
public static List<AssociatedPair> convertToNormalizedCoordinates(List<AssociatedPair> matchedFeatures, IntrinsicParameters intrinsic) { | public static List<AssociatedPair> convertToNormalizedCoordinates(List<AssociatedPair> matchedFeatures, IntrinsicParameters intrinsic) { | ||
PointTransform_F64 | PointTransform_F64 p_to_n = LensDistortionOps.transformPoint(intrinsic).undistort_F64(true, false); | ||
List<AssociatedPair> calibratedFeatures = new ArrayList<AssociatedPair>(); | List<AssociatedPair> calibratedFeatures = new ArrayList<AssociatedPair>(); | ||
Line 172: | Line 158: | ||
AssociatedPair c = new AssociatedPair(); | AssociatedPair c = new AssociatedPair(); | ||
p_to_n.compute(p.p1.x, p.p1.y, c.p1); | |||
p_to_n.compute(p.p2.x, p.p2.y, c.p2); | |||
calibratedFeatures.add(c); | calibratedFeatures.add(c); | ||
Line 218: | Line 204: | ||
// undistorted and rectify images | // undistorted and rectify images | ||
ImageDistort<ImageUInt8,ImageUInt8> distortLeft = | ImageDistort<ImageUInt8,ImageUInt8> distortLeft = | ||
RectifyImageOps.rectifyImage(intrinsic, rect1, ImageUInt8.class); | RectifyImageOps.rectifyImage(intrinsic, rect1, BorderType.SKIP, ImageUInt8.class); | ||
ImageDistort<ImageUInt8,ImageUInt8> distortRight = | ImageDistort<ImageUInt8,ImageUInt8> distortRight = | ||
RectifyImageOps.rectifyImage(intrinsic, rect2, ImageUInt8.class); | RectifyImageOps.rectifyImage(intrinsic, rect2, BorderType.SKIP, ImageUInt8.class); | ||
distortLeft.apply(distortedLeft, rectifiedLeft); | distortLeft.apply(distortedLeft, rectifiedLeft); | ||
Line 231: | Line 217: | ||
public static void drawInliers(BufferedImage left, BufferedImage right, IntrinsicParameters intrinsic, | public static void drawInliers(BufferedImage left, BufferedImage right, IntrinsicParameters intrinsic, | ||
List<AssociatedPair> normalized) { | List<AssociatedPair> normalized) { | ||
PointTransform_F64 | PointTransform_F64 n_to_p = LensDistortionOps.transformPoint(intrinsic).distort_F64(false,true); | ||
List<AssociatedPair> pixels = new ArrayList<AssociatedPair>(); | List<AssociatedPair> pixels = new ArrayList<AssociatedPair>(); | ||
Line 238: | Line 224: | ||
AssociatedPair p = new AssociatedPair(); | AssociatedPair p = new AssociatedPair(); | ||
n_to_p.compute(n.p1.x, n.p1.y, p.p1); | |||
n_to_p.compute(n.p2.x, n.p2.y, p.p2); | |||
pixels.add(p); | pixels.add(p); |
Revision as of 20:15, 19 September 2015
Associated inlier features between two views | |
---|---|
Stereo disparity image | 3D point cloud |
A dense point cloud of an environment can be created from a single camera using two views. If the distance between the two views is not known, then the scale of the found point cloud will be arbitrary. In this example natural features are used to determine the geometric relationship between the two views. The algorithm can be summarized as follows:
- Load camera calibration and two images
- Detect, describe, and associate image features
- Compute camera motion (Essential matrix)
- Rectify image pair
- Compute dense stereo disparity
- Convert into 3D point cloud
Example File:
Concepts:
- Point feature association
- Epipolar geometry
- Rectification
- Dense stereo processing
Relevant Applets:
Related Tutorials/Example Code:
Example Code
/**
* Example demonstrating how to use to images taken from a single calibrated camera to create a stereo disparity image,
* from which a dense 3D point cloud of the scene can be computed. For this technique to work the camera's motion
* needs to be approximately tangential to the direction the camera is pointing. The code below assumes that the first
* image is to the left of the second image.
*
* @author Peter Abeles
*/
public class ExampleStereoTwoViewsOneCamera {
// Disparity calculation parameters
private static final int minDisparity = 15;
private static final int maxDisparity = 100;
public static void main(String args[]) {
// specify location of images and calibration
String calibDir = "../data/applet/calibration/mono/Sony_DSC-HX5V_Chess/";
String imageDir = "../data/applet/stereo/";
// Camera parameters
IntrinsicParameters intrinsic = UtilIO.loadXML(calibDir + "intrinsic.xml");
// 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
ImageUInt8 distortedLeft = ConvertBufferedImage.convertFrom(origLeft, (ImageUInt8) null);
ImageUInt8 distortedRight = ConvertBufferedImage.convertFrom(origRight, (ImageUInt8) 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<AssociatedPair>();
Se3_F64 leftToRight = estimateCameraMotion(intrinsic, matchedCalibrated, inliers);
drawInliers(origLeft, origRight, intrinsic, inliers);
// Rectify and remove lens distortion for stereo processing
DenseMatrix64F rectifiedK = new DenseMatrix64F(3, 3);
ImageUInt8 rectifiedLeft = distortedLeft.createSameShape();
ImageUInt8 rectifiedRight = distortedRight.createSameShape();
rectifyImages(distortedLeft, distortedRight, leftToRight, intrinsic, rectifiedLeft, rectifiedRight, rectifiedK);
// compute disparity
StereoDisparity<ImageSInt16, ImageFloat32> disparityAlg =
FactoryStereoDisparity.regionSubpixelWta(DisparityAlgorithms.RECT_FIVE,
minDisparity, maxDisparity, 5, 5, 20, 1, 0.1, ImageSInt16.class);
// Apply the Laplacian across the image to add extra resistance to changes in lighting or camera gain
ImageSInt16 derivLeft = new ImageSInt16(rectifiedLeft.width,rectifiedLeft.height);
ImageSInt16 derivRight = new ImageSInt16(rectifiedLeft.width,rectifiedLeft.height);
LaplacianEdge.process(rectifiedLeft, derivLeft);
LaplacianEdge.process(rectifiedRight,derivRight);
// process and return the results
disparityAlg.process(derivLeft, derivRight);
ImageFloat32 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());
}
/**
* Estimates the camera motion robustly using RANSAC and a set of associated points.
*
* @param intrinsic Intrinsic camera parameters
* @param matchedNorm set of matched point features in normalized image coordinates
* @param inliers OUTPUT: Set of inlier features from RANSAC
* @return Found camera motion. Note translation has an arbitrary scale
*/
public static Se3_F64 estimateCameraMotion(IntrinsicParameters intrinsic,
List<AssociatedPair> matchedNorm, List<AssociatedPair> inliers)
{
ModelMatcher<Se3_F64, AssociatedPair> epipolarMotion =
FactoryMultiViewRobust.essentialRansac(new ConfigEssential(intrinsic),new ConfigRansac(200,0.5));
if (!epipolarMotion.process(matchedNorm))
throw new RuntimeException("Motion estimation failed");
// save inlier set for debugging purposes
inliers.addAll(epipolarMotion.getMatchSet());
return epipolarMotion.getModelParameters();
}
/**
* Convert a set of associated point features from pixel coordinates into normalized image coordinates.
*/
public static List<AssociatedPair> convertToNormalizedCoordinates(List<AssociatedPair> matchedFeatures, IntrinsicParameters intrinsic) {
PointTransform_F64 p_to_n = LensDistortionOps.transformPoint(intrinsic).undistort_F64(true, false);
List<AssociatedPair> calibratedFeatures = new ArrayList<AssociatedPair>();
for (AssociatedPair p : matchedFeatures) {
AssociatedPair c = new AssociatedPair();
p_to_n.compute(p.p1.x, p.p1.y, c.p1);
p_to_n.compute(p.p2.x, p.p2.y, c.p2);
calibratedFeatures.add(c);
}
return calibratedFeatures;
}
/**
* 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(ImageUInt8 distortedLeft,
ImageUInt8 distortedRight,
Se3_F64 leftToRight,
IntrinsicParameters intrinsic,
ImageUInt8 rectifiedLeft,
ImageUInt8 rectifiedRight,
DenseMatrix64F rectifiedK) {
RectifyCalibrated rectifyAlg = RectifyImageOps.createCalibrated();
// original camera calibration matrices
DenseMatrix64F K = PerspectiveOps.calibrationMatrix(intrinsic, null);
rectifyAlg.process(K, new Se3_F64(), K, leftToRight);
// rectification matrix for each image
DenseMatrix64F rect1 = rectifyAlg.getRect1();
DenseMatrix64F 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
ImageDistort<ImageUInt8,ImageUInt8> distortLeft =
RectifyImageOps.rectifyImage(intrinsic, rect1, BorderType.SKIP, ImageUInt8.class);
ImageDistort<ImageUInt8,ImageUInt8> distortRight =
RectifyImageOps.rectifyImage(intrinsic, rect2, BorderType.SKIP, ImageUInt8.class);
distortLeft.apply(distortedLeft, rectifiedLeft);
distortRight.apply(distortedRight, rectifiedRight);
}
/**
* Draw inliers for debugging purposes. Need to convert from normalized to pixel coordinates.
*/
public static void drawInliers(BufferedImage left, BufferedImage right, IntrinsicParameters intrinsic,
List<AssociatedPair> normalized) {
PointTransform_F64 n_to_p = LensDistortionOps.transformPoint(intrinsic).distort_F64(false,true);
List<AssociatedPair> pixels = new ArrayList<AssociatedPair>();
for (AssociatedPair n : normalized) {
AssociatedPair p = new AssociatedPair();
n_to_p.compute(n.p1.x, n.p1.y, p.p1);
n_to_p.compute(n.p2.x, n.p2.y, p.p2);
pixels.add(p);
}
// display the results
AssociationPanel panel = new AssociationPanel(20);
panel.setAssociation(pixels);
panel.setImages(left, right);
ShowImages.showWindow(panel, "Inlier Features");
}
/**
* Show results as a point cloud
*/
public static void showPointCloud(ImageSingleBand disparity, BufferedImage left,
Se3_F64 motion, DenseMatrix64F rectifiedK ,
int minDisparity, int maxDisparity) {
PointCloudTiltPanel gui = new PointCloudTiltPanel();
double baseline = motion.getT().norm();
gui.configure(baseline, rectifiedK, new DoNothingTransform_F64(), minDisparity, maxDisparity);
gui.process(disparity, left);
gui.setPreferredSize(new Dimension(left.getWidth(), left.getHeight()));
ShowImages.showWindow(gui, "Point Cloud");
}
}