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.demos; 031 032import java.io.File; 033import java.io.IOException; 034 035import org.openimaj.feature.local.list.LocalFeatureList; 036import org.openimaj.feature.local.matcher.FastBasicKeypointMatcher; 037import org.openimaj.feature.local.matcher.consistent.ConsistentLocalFeatureMatcher2d; 038import org.openimaj.image.DisplayUtilities; 039import org.openimaj.image.FImage; 040import org.openimaj.image.ImageUtilities; 041import org.openimaj.image.MBFImage; 042import org.openimaj.image.colour.Transforms; 043import org.openimaj.image.feature.local.engine.DoGSIFTEngine; 044import org.openimaj.image.feature.local.keypoints.Keypoint; 045import org.openimaj.image.processing.resize.ResizeProcessor; 046import org.openimaj.image.processing.transform.ProjectionProcessor; 047import org.openimaj.math.geometry.point.Point2d; 048import org.openimaj.math.geometry.transforms.HomographyModel; 049import org.openimaj.math.geometry.transforms.residuals.SingleImageTransferResidual2d; 050import org.openimaj.math.model.fit.RANSAC; 051 052/** 053 * Demonstration of building an image mosiac 054 * 055 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 056 * 057 */ 058public class SimpleMosaic { 059 /** 060 * Build a mosaic 061 * 062 * @param args 063 * ignored 064 * @throws IOException 065 */ 066 public static void main(String args[]) throws IOException { 067 final ResizeProcessor rp = new ResizeProcessor(800, 600); 068 final DoGSIFTEngine engine = new DoGSIFTEngine(); 069 070 final MBFImage imageMiddle = ImageUtilities.readMBF(new File("data/trento-view-1.jpg")); 071 imageMiddle.processInplace(rp); 072 final FImage workingImageMiddle = Transforms.calculateIntensityNTSC(imageMiddle); 073 final LocalFeatureList<Keypoint> middleKP = engine.findFeatures(workingImageMiddle); 074 075 final ConsistentLocalFeatureMatcher2d<Keypoint> matcher = 076 new ConsistentLocalFeatureMatcher2d<Keypoint>(new FastBasicKeypointMatcher<Keypoint>(8)); 077 final HomographyModel model = new HomographyModel(); 078 final RANSAC<Point2d, Point2d, HomographyModel> modelFitting = 079 new RANSAC<Point2d, Point2d, HomographyModel>(model, 080 new SingleImageTransferResidual2d<HomographyModel>(), 8.0, 1600, 081 new RANSAC.BestFitStoppingCondition(), true); 082 matcher.setFittingModel(modelFitting); 083 matcher.setModelFeatures(middleKP); 084 final ProjectionProcessor<Float[], MBFImage> ptp = new ProjectionProcessor<Float[], MBFImage>(); 085 imageMiddle.accumulateWith(ptp); 086 087 final MBFImage imageRight = ImageUtilities.readMBF(new File("data/trento-view-0.jpg")); 088 imageRight.processInplace(rp); 089 final FImage workingImageRight = Transforms.calculateIntensityNTSC(imageRight); 090 final LocalFeatureList<Keypoint> rightKP = engine.findFeatures(workingImageRight); 091 matcher.findMatches(rightKP); 092 ptp.setMatrix(model.getTransform()); 093 imageRight.accumulateWith(ptp); 094 095 final MBFImage imageLeft = ImageUtilities.readMBF(new File("data/trento-view-2.jpg")); 096 imageLeft.processInplace(rp); 097 final FImage workingImageLeft = Transforms.calculateIntensityNTSC(imageLeft); 098 final LocalFeatureList<Keypoint> leftKP = engine.findFeatures(workingImageLeft); 099 matcher.findMatches(leftKP); 100 ptp.setMatrix(model.getTransform()); 101 imageLeft.accumulateWith(ptp); 102 103 final MBFImage projected = ptp.performBlendedProjection( 104 (-imageMiddle.getWidth()), 105 imageMiddle.getWidth() + imageMiddle.getWidth(), 106 -imageMiddle.getHeight() / 2, 3 * imageMiddle.getHeight() / 2, (Float[]) null); 107 DisplayUtilities.display(projected); 108 ImageUtilities.write(projected, "png", new File("/Users/jsh2/Desktop/mosaic.png")); 109 } 110}