001/** 002 * Copyright (c) 2011, The University of Southampton and the individual contributors. 003 * All rights reserved. 004 * 005 * Redistribution and use in source and binary forms, with or without modification, 006 * are permitted provided that the following conditions are met: 007 * 008 * * Redistributions of source code must retain the above copyright notice, 009 * this list of conditions and the following disclaimer. 010 * 011 * * Redistributions in binary form must reproduce the above copyright notice, 012 * this list of conditions and the following disclaimer in the documentation 013 * and/or other materials provided with the distribution. 014 * 015 * * Neither the name of the University of Southampton nor the names of its 016 * contributors may be used to endorse or promote products derived from this 017 * software without specific prior written permission. 018 * 019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 029 */ 030package org.openimaj.image.calibration; 031 032import java.io.IOException; 033import java.net.MalformedURLException; 034import java.net.URL; 035import java.util.ArrayList; 036import java.util.List; 037 038import no.uib.cipr.matrix.NotConvergedException; 039 040import org.openimaj.image.DisplayUtilities; 041import org.openimaj.image.FImage; 042import org.openimaj.image.ImageUtilities; 043import org.openimaj.image.MBFImage; 044import org.openimaj.image.camera.calibration.CameraCalibrationZhang; 045import org.openimaj.image.colour.RGBColour; 046import org.openimaj.math.geometry.point.Point2d; 047import org.openimaj.math.geometry.point.Point2dImpl; 048import org.openimaj.util.iterator.TextLineIterable; 049import org.openimaj.util.pair.IndependentPair; 050 051public class TestHarness { 052 public static void main(String[] args) throws IOException, NotConvergedException { 053 final List<Point2dImpl> modelPoints = loadModelPoints(); 054 final List<List<Point2dImpl>> points = loadImagePoints(); 055 056 final List<List<? extends IndependentPair<? extends Point2d, ? extends Point2d>>> pointMatches = 057 new ArrayList<List<? extends IndependentPair<? extends Point2d, ? extends Point2d>>>(); 058 059 for (int i = 0; i < points.size(); i++) { 060 final List<IndependentPair<Point2dImpl, Point2dImpl>> data = 061 IndependentPair.pairList(modelPoints, points.get(i)); 062 pointMatches.add(data); 063 } 064 065 final CameraCalibrationZhang calib = new CameraCalibrationZhang(pointMatches, 640, 480); 066 // final CameraCalibrationZhang calib = new 067 // CameraCalibration(pointMatches); 068 069 System.out.println(calib.getIntrisics()); 070 071 System.out.println(calib.calculateError()); 072 073 final MBFImage img = new MBFImage(640, 480); 074 for (int i = 0; i < pointMatches.get(0).size(); i++) { 075 final Point2d model = pointMatches.get(0).get(i).firstObject(); 076 final Point2d observed = pointMatches.get(0).get(i).secondObject(); 077 078 final Point2d proj = calib.getCameras().get(0).project(model); 079 img.drawPoint(proj, RGBColour.RED, 1); 080 img.drawPoint(observed, RGBColour.GREEN, 1); 081 } 082 DisplayUtilities.display(img); 083 084 final FImage img1 = ImageUtilities.readF(new URL( 085 "http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/CalibIm1.gif")); 086 DisplayUtilities.display(img1); 087 final FImage img2 = calib.getIntrisics().undistort(img1); 088 DisplayUtilities.display(img2); 089 } 090 091 private static List<Point2dImpl> loadModelPoints() throws MalformedURLException { 092 return loadData(new URL( 093 "http://research.microsoft.com/en-us/um/people/zhang/Calib/Calibration/Model.txt")); 094 } 095 096 private static List<List<Point2dImpl>> loadImagePoints() throws MalformedURLException { 097 final List<List<Point2dImpl>> data = new ArrayList<List<Point2dImpl>>(); 098 099 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}