View Javadoc

1   /**
2    * Copyright (c) 2011, The University of Southampton and the individual contributors.
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without modification,
6    * are permitted provided that the following conditions are met:
7    *
8    *   * 	Redistributions of source code must retain the above copyright notice,
9    * 	this list of conditions and the following disclaimer.
10   *
11   *   *	Redistributions in binary form must reproduce the above copyright notice,
12   * 	this list of conditions and the following disclaimer in the documentation
13   * 	and/or other materials provided with the distribution.
14   *
15   *   *	Neither the name of the University of Southampton nor the names of its
16   * 	contributors may be used to endorse or promote products derived from this
17   * 	software without specific prior written permission.
18   *
19   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21   * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
23   * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26   * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28   * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29   */
30  package org.openimaj.image.calibration;
31  
32  import java.io.IOException;
33  import java.net.MalformedURLException;
34  import java.net.URL;
35  import java.util.ArrayList;
36  import java.util.List;
37  
38  import no.uib.cipr.matrix.NotConvergedException;
39  
40  import org.openimaj.image.DisplayUtilities;
41  import org.openimaj.image.FImage;
42  import org.openimaj.image.ImageUtilities;
43  import org.openimaj.image.MBFImage;
44  import org.openimaj.image.camera.calibration.CameraCalibrationZhang;
45  import org.openimaj.image.colour.RGBColour;
46  import org.openimaj.math.geometry.point.Point2d;
47  import org.openimaj.math.geometry.point.Point2dImpl;
48  import org.openimaj.util.iterator.TextLineIterable;
49  import org.openimaj.util.pair.IndependentPair;
50  
51  public class TestHarness {
52  	public static void main(String[] args) throws IOException, NotConvergedException {
53  		final List<Point2dImpl> modelPoints = loadModelPoints();
54  		final List<List<Point2dImpl>> points = loadImagePoints();
55  
56  		final List<List<? extends IndependentPair<? extends Point2d, ? extends Point2d>>> pointMatches =
57  				new ArrayList<List<? extends IndependentPair<? extends Point2d, ? extends Point2d>>>();
58  
59  		for (int i = 0; i < points.size(); i++) {
60  			final List<IndependentPair<Point2dImpl, Point2dImpl>> data =
61  					IndependentPair.pairList(modelPoints, points.get(i));
62  			pointMatches.add(data);
63  		}
64  
65  		final CameraCalibrationZhang calib = new CameraCalibrationZhang(pointMatches, 640, 480);
66  		// final CameraCalibrationZhang calib = new
67  		// CameraCalibration(pointMatches);
68  
69  		System.out.println(calib.getIntrisics());
70  
71  		System.out.println(calib.calculateError());
72  
73  		final MBFImage img = new MBFImage(640, 480);
74  		for (int i = 0; i < pointMatches.get(0).size(); i++) {
75  			final Point2d model = pointMatches.get(0).get(i).firstObject();
76  			final Point2d observed = pointMatches.get(0).get(i).secondObject();
77  
78  			final Point2d proj = calib.getCameras().get(0).project(model);
79  			img.drawPoint(proj, RGBColour.RED, 1);
80  			img.drawPoint(observed, RGBColour.GREEN, 1);
81  		}
82  		DisplayUtilities.display(img);
83  
84  		final FImage img1 = ImageUtilities.readF(new URL(
85  				"http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/CalibIm1.gif"));
86  		DisplayUtilities.display(img1);
87  		final FImage img2 = calib.getIntrisics().undistort(img1);
88  		DisplayUtilities.display(img2);
89  	}
90  
91  	private static List<Point2dImpl> loadModelPoints() throws MalformedURLException {
92  		return loadData(new URL(
93  				"http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/Model.txt"));
94  	}
95  
96  	private static List<List<Point2dImpl>> loadImagePoints() throws MalformedURLException {
97  		final List<List<Point2dImpl>> data = new ArrayList<List<Point2dImpl>>();
98  
99  		data.add(loadData(new URL("http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/data1.txt")));
100 		data.add(loadData(new URL("http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/data2.txt")));
101 		data.add(loadData(new URL("http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/data3.txt")));
102 		data.add(loadData(new URL("http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/data4.txt")));
103 		data.add(loadData(new URL("http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/data5.txt")));
104 
105 		return data;
106 	}
107 
108 	private static List<Point2dImpl> loadData(URL url) {
109 		final List<Point2dImpl> pts = new ArrayList<Point2dImpl>();
110 
111 		for (final String line : new TextLineIterable(url))
112 		{
113 			final String[] p = line.split("[ ]+");
114 			pts.add(new Point2dImpl(Float.parseFloat(p[0]), Float.parseFloat(p[1])));
115 			pts.add(new Point2dImpl(Float.parseFloat(p[2]), Float.parseFloat(p[3])));
116 			pts.add(new Point2dImpl(Float.parseFloat(p[4]), Float.parseFloat(p[5])));
117 			pts.add(new Point2dImpl(Float.parseFloat(p[6]), Float.parseFloat(p[7])));
118 		}
119 		return pts;
120 	}
121 }