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.workinprogress;
031
032import org.openimaj.image.FImage;
033import org.openimaj.image.analysis.algorithm.SummedAreaTable;
034import org.openimaj.image.processing.convolution.FGaussianConvolve;
035import org.openimaj.image.processor.ImageProcessor;
036import org.openimaj.util.array.ArrayUtils;
037
038import cern.jet.random.Uniform;
039import cern.jet.random.engine.MersenneTwister;
040
041/**
042 * Implementation of the Brightness Clustering Transform.
043 * <p>
044 * FIXME: add references when available and move.
045 *
046 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk)
047 *
048 */
049public class BrightnessClusteringTransform implements ImageProcessor<FImage> {
050        float sigma = 2;
051        int maxVotes = 100000;
052        int rangeMin = 3;
053        int rangeMax = 7;
054        Uniform rng = new Uniform(new MersenneTwister());
055
056        @Override
057        public void processImage(FImage image) {
058                final SummedAreaTable sat = new SummedAreaTable(image);
059                final FImage output = new FImage(image.width, image.height);
060
061                for (int vote = 0; vote < maxVotes; vote++) {
062                        // First step
063                        int widthR = (int) Math.pow(2, rng.nextIntFromTo(rangeMin, rangeMax));
064                        int heightR = (int) Math.pow(2, rng.nextIntFromTo(rangeMin, rangeMax));
065                        int xR = rng.nextIntFromTo(0, image.width - widthR);
066                        int yR = rng.nextIntFromTo(0, image.height - heightR);
067
068                        // Second step
069                        while (widthR > 2 && heightR > 2) {
070                                final int hw = widthR / 2;
071                                final int hh = heightR / 2;
072
073                                final float[] r = {
074                                                sat.calculateArea(xR, yR, xR + hw, yR + hh),
075                                                sat.calculateArea(xR + hw, yR, xR + widthR, yR + hh),
076                                                sat.calculateArea(xR, yR + hh, xR + hw, yR + heightR),
077                                                sat.calculateArea(xR + hw, yR + hh, xR + widthR, yR + heightR),
078                                };
079
080                                final int[] f = ArrayUtils.indexSort(r);
081                                if (r[f[3]] == r[f[2]])
082                                        break;
083
084                                final int maxIdx = ArrayUtils.maxIndex(r);
085
086                                widthR = hw;
087                                heightR = hh;
088                                if (maxIdx == 1) {
089                                        xR = xR + hw;
090                                } else if (maxIdx == 2) {
091                                        yR = yR + hh;
092                                } else if (maxIdx == 3) {
093                                        xR = xR + hw;
094                                        yR = yR + hh;
095                                }
096                        }
097
098                        // Third step
099                        final int xLoc = Math.round(xR + widthR / 2);
100                        final int yLoc = Math.round(yR + heightR / 2);
101                        output.pixels[yLoc][xLoc]++;
102                }
103
104                output.processInplace(new FGaussianConvolve(sigma));
105                image.internalAssign(output.normalise());
106        }
107
108        // public static void main(String[] args) throws IOException {
109        // // FImage image = ImageUtilities.readF(new
110        // // File("/Users/jon/Pictures/Pictures/2007/02/17/IMG_1165.JPG"));
111        // // image = ResizeProcessor.halfSize(image);
112        // final FImage image = new FImage(400, 400);
113        // image.drawShapeFilled(new Ellipse(200, 200, 20, 20, 0), 1f);
114        //
115        // DisplayUtilities.display(image);
116        //
117        // final FImage result = image.process(new BrightnessClusteringTransform());
118        // DisplayUtilities.display(result);
119        // }
120}