Difference between revisions of "Example Calibrate Planar Stereo"

From BoofCV
Jump to navigationJump to search
m
m
Line 31: Line 31:
<syntaxhighlight lang="java">
<syntaxhighlight lang="java">
/**
/**
  * Example of how to calibrate a single (monocular) camera given a set of observed calibration.
  * Example of how to calibrate a stereo camera system using a planar calibration grid given a set of images.
  * points using the Zhang99 algorithm. Unlike in the other examples, the image processing has
  * Intrinsic camera parameters are estimated for both cameras individually, then extrinsic parameters
  * not already been done. In theory you could add your own more exotic targets by writing
  * for the two cameras relative to each other are found  This example does not rectify the images, which is
  * custom code to detect the target, which is left as an exercise for the readerFor example,
  * required for some algorithms. See {@link ExampleRectifyCalibratedStereo}. Both square grid and chessboard targets
  * have multiple targets visible or calibrate using circles.
  * are demonstrated in this example. See calibration tutorial for a discussion of different target types and how to
* collect good calibration images.
*
* All the image processing and calibration is taken care of inside of {@link CalibrateStereoPlanar}.  The code below
* loads calibration images as inputs, calibrates, and saves results to an XML file.  See in code comments for tuning
* and implementation issues.
  *
* @see ExampleRectifyCalibratedStereo
  * @see CalibrateStereoPlanar
  *
  *
  * @author Peter Abeles
  * @author Peter Abeles
  */
  */
public class ExampleCalibrateMonocularPoints {
public class ExampleCalibrateStereoPlanar {
 
// Detects the target and calibration point inside the target
PlanarCalibrationDetector detector;
 
// Description of the target's physical dimension
PlanarCalibrationTarget target;
 
// List of calibration images
List<String> left;
List<String> right;
 
// Many 3D operations assumed a right handed coordinate system with +Z pointing out of the image.
// If the image coordinate system is left handed then the y-axis needs to be flipped to meet
// that requirement.  Most of the time this is false.
boolean flipY;


/**
/**
* Given a description of the calibration target and the observed location of the calibration
* Square grid target taken by a PtGrey Bumblebee camera.
*
* @param targetDesc Describes the target's known physical structure
* @param observations Observations of the target in different images
*/
*/
public static void calibrate( PlanarCalibrationTarget targetDesc ,
public void setupBumblebeeSquare() {
  List<List<Point2D_F64>> observations ) {
// Use the wrapper below for square grid targets.
 
detector = FactoryPlanarCalibrationTarget.detectorSquareGrid(3,4);
// Assume zero skew and model lens distortion with two radial parameters
// Target physical description
CalibrationPlanarGridZhang99 zhang99 =
target = FactoryPlanarCalibrationTarget.gridSquare(3, 4, 30,30);
new CalibrationPlanarGridZhang99(targetDesc,true,2);


if( !zhang99.process(observations) )
String directory = "../data/evaluation/calibration/stereo/Bumblebee2_Square";
throw new RuntimeException("Calibration failed!");


// Get camera parameters and extrinsic target location in each image
left = BoofMiscOps.directoryList(directory, "left");
Zhang99Parameters found = zhang99.getOptimized();
right = BoofMiscOps.directoryList(directory, "right");


// Convenient function for converting from specialized Zhang99 format to generalized
flipY = false;
IntrinsicParameters param = found.convertToIntrinsic();
 
// print the results to standard out
param.print();
// save to a file using XML
BoofMiscOps.saveXML(param,"intrinsic.xml");
}
}


/**
/**
* Detects calibration points found in several images and returned as a list. Not the focus of this example.
* Chessboard target taken by a PtGrey Bumblebee camera.
*/
*/
public static List<List<Point2D_F64>> loadObservations() {
public void setupBumblebeeChess() {
// Use the wrapper below for chessboard targets.  The last parameter adjusts the size of the corner detection
// region.  TUNE THIS PARAMETER FOR OPTIMAL ACCURACY!
detector = FactoryPlanarCalibrationTarget.detectorChessboard(3, 4, 6);
// Target physical description
target = FactoryPlanarCalibrationTarget.gridChess(3, 4, 30);


String directory = "../data/evaluation/calibration/stereo/Bumblebee2_Chess";
String directory = "../data/evaluation/calibration/stereo/Bumblebee2_Chess";
List<String> imageNames = BoofMiscOps.directoryList(directory,"left");


PlanarCalibrationDetector detector = FactoryPlanarCalibrationTarget.detectorChessboard(3, 4, 6);
left = BoofMiscOps.directoryList(directory, "left");
right = BoofMiscOps.directoryList(directory, "right");


List<List<Point2D_F64>> ret = new ArrayList<List<Point2D_F64>>();
flipY = false;
}
 
/**
* Process calibration images, compute intrinsic parameters, save to a file
*/
public void process() {
// Declare and setup the calibration algorithm
CalibrateStereoPlanar calibratorAlg = new CalibrateStereoPlanar(detector, flipY);
calibratorAlg.configure(target, true, 2);


for( String n : imageNames ) {
// ensure the lists are in the same order
BufferedImage img = UtilImageIO.loadImage(n);
Collections.sort(left);
ImageFloat32 input = ConvertBufferedImage.convertFrom(img,(ImageFloat32)null);
Collections.sort(right);


if( !detector.process(input) )
for( int i = 0; i < left.size(); i++ ) {
throw new RuntimeException("Detection failed!");
BufferedImage l = UtilImageIO.loadImage(left.get(i));
BufferedImage r = UtilImageIO.loadImage(right.get(i));


ret.add(detector.getPoints());
ImageFloat32 imageLeft = ConvertBufferedImage.convertFrom(l,(ImageFloat32)null);
ImageFloat32 imageRight = ConvertBufferedImage.convertFrom(r,(ImageFloat32)null);
 
calibratorAlg.addPair(imageLeft, imageRight);
}
}


return ret;
// Process and compute calibration parameters
StereoParameters stereoCalib = calibratorAlg.process();
 
// save results to a file and print out
BoofMiscOps.saveXML(stereoCalib, "stereo.xml");
stereoCalib.print();
}
}


public static void main( String args[] ) {
public static void main( String args[] ) {
// target description and list of observations
ExampleCalibrateStereoPlanar alg = new ExampleCalibrateStereoPlanar();
PlanarCalibrationTarget desc = FactoryPlanarCalibrationTarget.gridChess(3, 4, 30);
 
List<List<Point2D_F64>> calibPts = loadObservations();
// Select which set of targets to use
alg.setupBumblebeeChess();
// alg.setupBumblebeeSquare();


// Calibrate the camera
// compute and save results
calibrate(desc,calibPts);
alg.process();
}
}
}
}
</syntaxhighlight>
</syntaxhighlight>

Revision as of 15:47, 6 November 2012

Stereo Camera Calibration with Planar Targets

This example demonstrate how to calibrate a stereo camera system using a high level interface which automatically detects calibration targets in a set of stereo images. After calibration the intrinsic parameters of each camera is found as well as their extrinsic relationship with each other. Both the square grid and chessboard patterns are supported by this example. For a full description of the calibration process and instruction on how to do it yourself see the tutorial linked to below.

Example File: ExampleCalibrateStereoPlanar.java

Calibration Tutorial: Wikipage

Concepts:

  • Camera calibration
  • Lens distortion
  • Intrinsic parameters
  • Stereo Vision

Relevant Applets:

Related Examples:

Example Code

/**
 * Example of how to calibrate a stereo camera system using a planar calibration grid given a set of images.
 * Intrinsic camera parameters are estimated for both cameras individually, then extrinsic parameters
 * for the two cameras relative to each other are found   This example does not rectify the images, which is
 * required for some algorithms. See {@link ExampleRectifyCalibratedStereo}. Both square grid and chessboard targets
 * are demonstrated in this example. See calibration tutorial for a discussion of different target types and how to
 * collect good calibration images.
 *
 * All the image processing and calibration is taken care of inside of {@link CalibrateStereoPlanar}.  The code below
 * loads calibration images as inputs, calibrates, and saves results to an XML file.  See in code comments for tuning
 * and implementation issues.
 *
 * @see ExampleRectifyCalibratedStereo
 * @see CalibrateStereoPlanar
 *
 * @author Peter Abeles
 */
public class ExampleCalibrateStereoPlanar {

	// Detects the target and calibration point inside the target
	PlanarCalibrationDetector detector;

	// Description of the target's physical dimension
	PlanarCalibrationTarget target;

	// List of calibration images
	List<String> left;
	List<String> right;

	// Many 3D operations assumed a right handed coordinate system with +Z pointing out of the image.
	// If the image coordinate system is left handed then the y-axis needs to be flipped to meet
	// that requirement.  Most of the time this is false.
	boolean flipY;

	/**
	 * Square grid target taken by a PtGrey Bumblebee camera.
	 */
	public void setupBumblebeeSquare() {
		// Use the wrapper below for square grid targets.
		detector = FactoryPlanarCalibrationTarget.detectorSquareGrid(3,4);
		// Target physical description
		target = FactoryPlanarCalibrationTarget.gridSquare(3, 4, 30,30);

		String directory = "../data/evaluation/calibration/stereo/Bumblebee2_Square";

		left = BoofMiscOps.directoryList(directory, "left");
		right = BoofMiscOps.directoryList(directory, "right");

		flipY = false;
	}

	/**
	 * Chessboard target taken by a PtGrey Bumblebee camera.
	 */
	public void setupBumblebeeChess() {
		// Use the wrapper below for chessboard targets.  The last parameter adjusts the size of the corner detection
		// region.  TUNE THIS PARAMETER FOR OPTIMAL ACCURACY!
		detector = FactoryPlanarCalibrationTarget.detectorChessboard(3, 4, 6);
		// Target physical description
		target = FactoryPlanarCalibrationTarget.gridChess(3, 4, 30);

		String directory = "../data/evaluation/calibration/stereo/Bumblebee2_Chess";

		left = BoofMiscOps.directoryList(directory, "left");
		right = BoofMiscOps.directoryList(directory, "right");

		flipY = false;
	}

	/**
	 * Process calibration images, compute intrinsic parameters, save to a file
	 */
	public void process() {
		// Declare and setup the calibration algorithm
		CalibrateStereoPlanar calibratorAlg = new CalibrateStereoPlanar(detector, flipY);
		calibratorAlg.configure(target, true, 2);

		// ensure the lists are in the same order
		Collections.sort(left);
		Collections.sort(right);

		for( int i = 0; i < left.size(); i++ ) {
			BufferedImage l = UtilImageIO.loadImage(left.get(i));
			BufferedImage r = UtilImageIO.loadImage(right.get(i));

			ImageFloat32 imageLeft = ConvertBufferedImage.convertFrom(l,(ImageFloat32)null);
			ImageFloat32 imageRight = ConvertBufferedImage.convertFrom(r,(ImageFloat32)null);

			calibratorAlg.addPair(imageLeft, imageRight);
		}

		// Process and compute calibration parameters
		StereoParameters stereoCalib = calibratorAlg.process();

		// save results to a file and print out
		BoofMiscOps.saveXML(stereoCalib, "stereo.xml");
		stereoCalib.print();
	}

	public static void main( String args[] ) {
		ExampleCalibrateStereoPlanar alg = new ExampleCalibrateStereoPlanar();

		// Select which set of targets to use
		alg.setupBumblebeeChess();
//		alg.setupBumblebeeSquare();

		// compute and save results
		alg.process();
	}
}