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.feature.dense.binarypattern; 031 032import org.openimaj.citation.annotation.Reference; 033import org.openimaj.citation.annotation.ReferenceType; 034import org.openimaj.image.FImage; 035import org.openimaj.image.analyser.ImageAnalyser; 036import org.openimaj.image.pixel.Pixel; 037 038/** 039 * Implementation of an extended local binary pattern which has a 040 * variable number of samples taken from a variable sized circle 041 * about a point. 042 * 043 * @author Jonathon Hare (jsh2@ecs.soton.ac.uk) 044 */ 045@Reference( 046 type = ReferenceType.Article, 047 author = { "Ojala, Timo", "Pietik\"{a}inen, Matti", "M\"{a}enp\"{a}\"{a}, Topi" }, 048 title = "Multiresolution Gray-Scale and Rotation Invariant Texture Classification with Local Binary Patterns", 049 year = "2002", 050 journal = "IEEE Trans. Pattern Anal. Mach. Intell.", 051 pages = { "971", "", "987" }, 052 url = "http://dx.doi.org/10.1109/TPAMI.2002.1017623", 053 month = "July", 054 number = "7", 055 publisher = "IEEE Computer Society", 056 volume = "24", 057 customData = { 058 "date", "July 2002", 059 "issn", "0162-8828", 060 "numpages", "17", 061 "doi", "10.1109/TPAMI.2002.1017623", 062 "acmid", "628808", 063 "address", "Washington, DC, USA" 064 } 065 ) 066public class ExtendedLocalBinaryPattern implements ImageAnalyser<FImage> { 067 protected int[][] pattern; 068 protected float radius; 069 protected int samples; 070 071 /** 072 * Construct an extended LBP extractor with the given parameters. 073 * @param radius the radius of the sampling circle 074 * @param samples the number of samples around the circle 075 */ 076 public ExtendedLocalBinaryPattern(float radius, int samples) { 077 checkParams(radius, samples); 078 this.radius = radius; 079 this.samples = samples; 080 } 081 082 /** 083 * Calculate the LBP for every pixel in the image. The returned 084 * array of LBP codes hase the same dimensions as the image. 085 * 086 * Samples taken from outside the image bounds are assumed to have 087 * the value 0. 088 * 089 * @param image the image 090 * @param radius the radius of the sampling circle 091 * @param samples the number of samples around the circle 092 * @return a 2d-array of the LBP codes for every pixel 093 */ 094 public static int[][] calculateLBP(FImage image, float radius, int samples) { 095 checkParams(radius, samples); 096 097 int [][] pattern = new int[image.height][image.width]; 098 099 for (int y=0; y<image.height; y++) { 100 for (int x=0; x<image.width; x++) { 101 pattern[y][x] = calculateLBP(image, radius, samples, x, y); 102 } 103 } 104 105 return pattern; 106 } 107 108 /** 109 * Calculate the extended LBP for a single point. The 110 * point must be within the image. 111 * 112 * @param image the image 113 * @param radius the radius of the sampling circle 114 * @param samples the number of samples around the circle 115 * @param x the x-coordinate of the point 116 * @param y the y-coordinate of the point 117 * @return the LBP code 118 */ 119 public static int calculateLBP(FImage image, float radius, int samples, int x, int y) { 120 float centre = image.pixels[y][x]; 121 int pattern = 0; 122 123 for (int i=0; i<samples; i++) { 124 double dx = -radius * Math.sin(2 * Math.PI * i / samples); 125 double dy = radius * Math.cos(2 * Math.PI * i / samples); 126 127 float pix = image.getPixelInterp(x+dx, y+dy); 128 129 if (pix - centre >= 0) { 130 pattern += Math.pow(2, i); 131 } 132 } 133 134 return pattern; 135 } 136 137 /** 138 * Calculate the extended LBP for a single point. The 139 * point must be within the image. 140 * 141 * @param image the image 142 * @param radius the radius of the sampling circle 143 * @param samples the number of samples around the circle 144 * @param point the point 145 * @return the LBP code 146 */ 147 public static int calculateLBP(FImage image, float radius, int samples, Pixel point) { 148 return calculateLBP(image, radius, samples, point.x, point.y); 149 } 150 151 private static void checkParams(float radius, int samples) { 152 if (radius <= 0) { 153 throw new IllegalArgumentException("radius must be greater than 0"); 154 } 155 if (samples <= 1 || samples > 31) { 156 throw new IllegalArgumentException("samples cannot be less than one or more than 31"); 157 } 158 } 159 160 /* (non-Javadoc) 161 * @see org.openimaj.image.analyser.ImageAnalyser#analyseImage(org.openimaj.image.Image) 162 */ 163 @Override 164 public void analyseImage(FImage image) { 165 pattern = calculateLBP(image, radius, samples); 166 } 167 168 /** 169 * Get the pattern created during the last call to 170 * {@link #analyseImage(FImage)}. 171 * 172 * @return the pattern 173 */ 174 public int[][] getPattern() { 175 return pattern; 176 } 177}