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.objectdetection.hog;
031
032import java.io.IOException;
033import java.net.URL;
034import java.util.ArrayList;
035import java.util.Collections;
036import java.util.Comparator;
037import java.util.List;
038
039import org.openimaj.feature.DoubleFVComparison;
040import org.openimaj.image.DisplayUtilities;
041import org.openimaj.image.FImage;
042import org.openimaj.image.ImageUtilities;
043import org.openimaj.image.feature.dense.gradient.HOG;
044import org.openimaj.image.feature.dense.gradient.binning.FlexibleHOGStrategy;
045import org.openimaj.math.geometry.shape.Rectangle;
046import org.openimaj.math.statistics.distribution.Histogram;
047import org.openimaj.util.pair.ObjectDoublePair;
048
049public class Test {
050        public static void main(String[] args) throws IOException {
051                final FImage img = ImageUtilities.readF(new URL(
052                                "http://www.di.ens.fr/willow/teaching/recvis10/final_project/detection/car-img1.png"));
053
054                final HOG h = new HOG(new FlexibleHOGStrategy(8, 8, 2));
055                h.analyseImage(img);
056
057                final Rectangle r = new Rectangle(47, 92, 30, 30);
058                final Histogram f = h.getFeatureVector(r).clone();
059
060                img.drawShape(r, 1f);
061                DisplayUtilities.display(img);
062
063                final FImage img2 = ImageUtilities.readF(new URL(
064                                "http://www.di.ens.fr/willow/teaching/recvis10/final_project/detection/car-img3.png"));
065                h.analyseImage(img2);
066
067                final List<ObjectDoublePair<Rectangle>> data = new ArrayList<ObjectDoublePair<Rectangle>>();
068                for (int y = 0; y < img2.height - 30; y++) {
069                        for (int x = 0; x < img2.width - 30; x++) {
070                                final Rectangle rr = new Rectangle(x, y, 30, 30);
071                                final Histogram ff = h.getFeatureVector(rr);
072
073                                final double c = DoubleFVComparison.EUCLIDEAN.compare(f, ff);
074                                data.add(ObjectDoublePair.pair(rr, c));
075                        }
076                }
077
078                Collections.sort(data, new Comparator<ObjectDoublePair<Rectangle>>() {
079                        @Override
080                        public int compare(ObjectDoublePair<Rectangle> o1, ObjectDoublePair<Rectangle> o2) {
081                                return Double.compare(o1.second, o2.second);
082                        }
083                });
084
085                for (int i = 0; i < 10; i++) {
086                        img2.drawShape(data.get(i).first, 1F);
087                }
088                DisplayUtilities.display(img2);
089        }
090}