1 /** 2 * Copyright (c) 2011, The University of Southampton and the individual contributors. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without modification, 6 * are permitted provided that the following conditions are met: 7 * 8 * * Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * * Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * * Neither the name of the University of Southampton nor the names of its 16 * contributors may be used to endorse or promote products derived from this 17 * software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 21 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON 26 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 package org.openimaj.workinprogress; 31 32 import org.openimaj.image.FImage; 33 import org.openimaj.image.analysis.algorithm.SummedAreaTable; 34 import org.openimaj.image.processing.convolution.FGaussianConvolve; 35 import org.openimaj.image.processor.ImageProcessor; 36 import org.openimaj.util.array.ArrayUtils; 37 38 import cern.jet.random.Uniform; 39 import cern.jet.random.engine.MersenneTwister; 40 41 /** 42 * Implementation of the Brightness Clustering Transform. 43 * <p> 44 * FIXME: add references when available and move. 45 * 46 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 47 * 48 */ 49 public class BrightnessClusteringTransform implements ImageProcessor<FImage> { 50 float sigma = 2; 51 int maxVotes = 100000; 52 int rangeMin = 3; 53 int rangeMax = 7; 54 Uniform rng = new Uniform(new MersenneTwister()); 55 56 @Override 57 public void processImage(FImage image) { 58 final SummedAreaTable sat = new SummedAreaTable(image); 59 final FImage output = new FImage(image.width, image.height); 60 61 for (int vote = 0; vote < maxVotes; vote++) { 62 // First step 63 int widthR = (int) Math.pow(2, rng.nextIntFromTo(rangeMin, rangeMax)); 64 int heightR = (int) Math.pow(2, rng.nextIntFromTo(rangeMin, rangeMax)); 65 int xR = rng.nextIntFromTo(0, image.width - widthR); 66 int yR = rng.nextIntFromTo(0, image.height - heightR); 67 68 // Second step 69 while (widthR > 2 && heightR > 2) { 70 final int hw = widthR / 2; 71 final int hh = heightR / 2; 72 73 final float[] r = { 74 sat.calculateArea(xR, yR, xR + hw, yR + hh), 75 sat.calculateArea(xR + hw, yR, xR + widthR, yR + hh), 76 sat.calculateArea(xR, yR + hh, xR + hw, yR + heightR), 77 sat.calculateArea(xR + hw, yR + hh, xR + widthR, yR + heightR), 78 }; 79 80 final int[] f = ArrayUtils.indexSort(r); 81 if (r[f[3]] == r[f[2]]) 82 break; 83 84 final int maxIdx = ArrayUtils.maxIndex(r); 85 86 widthR = hw; 87 heightR = hh; 88 if (maxIdx == 1) { 89 xR = xR + hw; 90 } else if (maxIdx == 2) { 91 yR = yR + hh; 92 } else if (maxIdx == 3) { 93 xR = xR + hw; 94 yR = yR + hh; 95 } 96 } 97 98 // Third step 99 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 }