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.sandbox;
031
032import java.io.DataInput;
033import java.io.DataOutput;
034import java.io.File;
035import java.io.IOException;
036import java.util.ArrayList;
037import java.util.List;
038
039import javax.swing.JFrame;
040
041import org.openimaj.demos.utils.FeatureClickListener;
042import org.openimaj.image.DisplayUtilities;
043import org.openimaj.image.FImage;
044import org.openimaj.image.ImageUtilities;
045import org.openimaj.image.MBFImage;
046import org.openimaj.image.colour.RGBColour;
047import org.openimaj.image.colour.Transforms;
048import org.openimaj.image.feature.local.interest.HarrisIPD;
049import org.openimaj.image.feature.local.interest.IPDSelectionMode;
050import org.openimaj.image.feature.local.interest.InterestPointData;
051import org.openimaj.image.feature.local.interest.InterestPointVisualiser;
052import org.openimaj.io.IOUtils;
053import org.openimaj.io.wrappers.ReadWriteableListBinary;
054
055public class ImageIPD {
056        
057        static abstract class ReadWriteableIPDList<T extends InterestPointData> extends ReadWriteableListBinary<T>{
058                
059                public ReadWriteableIPDList() {
060                        super(new ArrayList<T>());
061                }
062                
063                public ReadWriteableIPDList(List<T> list) {
064                        super(list);
065                }
066
067                @Override
068                protected void writeValue(T v, DataOutput out)throws IOException {
069                        v.writeBinary(out);
070                        
071                }
072
073                @Override
074                protected T readValue(DataInput in) throws IOException {
075                        T c = createFeature();
076                        c.readBinary(in);
077                        return c;
078                }
079
080                protected abstract T createFeature() ;
081                
082        }
083        
084        @SuppressWarnings("unchecked")
085        public static void main(String[] args) throws IOException{
086                
087                MBFImage image = ImageUtilities.readMBF(new File("/Users/ss/Development/descriptors/img1.png"));
088                FImage fimage = Transforms.calculateIntensity(image).multiply(255f);
089                
090                File featureOut = new File("/Users/ss/Development/descriptors/img1-oi.features");
091                List<InterestPointData> kps  = null;
092                boolean force = true;
093                float sd = (float) 1;
094                HarrisIPD harrisIPD = new HarrisIPD(sd*0.7f,sd*1f,0.01f);
095                if(!featureOut.exists() || force){
096                        harrisIPD.findInterestPoints(fimage);
097                        kps = new IPDSelectionMode.Threshold(10000).selectPoints(harrisIPD);
098//                      kps = new IPDSelectionMode.All().selectPoints(harrisIPD);
099                        IOUtils.writeBinary(featureOut,new ReadWriteableIPDList<InterestPointData>(kps){
100                                @Override
101                                protected InterestPointData createFeature() {
102                                        return new InterestPointData();
103                                }
104                        });
105                }
106                else{
107                        kps = IOUtils.read(featureOut,ReadWriteableIPDList.class).value;
108                }
109                
110                
111                InterestPointVisualiser<Float[], MBFImage> visualiser = InterestPointVisualiser.visualiseInterestPoints(image, kps);
112                MBFImage out = visualiser.drawPatches(RGBColour.RED, RGBColour.GREEN);
113                
114                JFrame f = DisplayUtilities.display(out,String.format("Showing %d feature", kps.size()));
115                FeatureClickListener l = new FeatureClickListener();
116                l.setImage(kps, image);
117                f.getContentPane().addMouseListener(l);
118                
119        }
120}